Skip to content

Commit 8e1b835

Browse files
committed
Fix wenzhixin#27, fix wenzhixin#39: Added server side
1 parent d394c3d commit 8e1b835

File tree

5 files changed

+111
-16
lines changed

5 files changed

+111
-16
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ bootstrap
44
*.iml
55
.idea/misc.xml
66
*.xml
7+
node_modules

README.md

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,16 @@ bootstrap-table-examples
77

88
clone & init
99

10-
```
11-
git clone [email protected]:wenzhixin/bootstrap-table-examples.git
10+
```sh
11+
git clone https://github.com/wenzhixin/bootstrap-table-examples.git
12+
cd bootstrap-table-examples
1213
git submodule update --init
13-
```
1414

15-
build
16-
17-
```
18-
node examples-list
15+
npm install
1916
```
2017

21-
## Notes
18+
## run
2219

23-
Because the API: `/examples/bootstrap_table/data` is base on node server with the code: https://github.com/wenzhixin/blog/blob/master/routes/examples.js.
24-
25-
You need to do:
26-
27-
* write a same server code
28-
* or use this example: http://issues.wenzhixin.net.cn/bootstrap-table/#152.html
29-
* or set -disable-web-security to the browser(for example chrome) to support cross domain
30-
* or use web server to add an http proxy(for example nginx)
20+
```sh
21+
node app
22+
```

app.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
var express = require('express');
2+
var app = express();
3+
4+
app.use(express.static('.'));
5+
app.get('/examples/:project/:func', require('./examples'));
6+
7+
var server = app.listen(3000, function () {
8+
9+
var host = server.address().address;
10+
var port = server.address().port;
11+
12+
console.log('Examples app listening at http://%s:%s', host, port);
13+
});

examples.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/**
2+
* @author zhixin wen <[email protected]>
3+
* examples for projects
4+
*/
5+
6+
var projects = {
7+
bootstrap_table: {
8+
data: function(req, res) {
9+
var offset = +req.query.offset || 0,
10+
limit = +req.query.limit || 20,
11+
search = req.query.search,
12+
name = req.query.sort,
13+
order = req.query.order || 'asc',
14+
15+
i,
16+
max = offset + limit,
17+
rows = [],
18+
result = {
19+
total: 800,
20+
rows: []
21+
};
22+
23+
for (i = 0; i < result.total; i++) {
24+
rows.push({
25+
id: i,
26+
name: 'Item ' + i,
27+
price: '$' + i
28+
});
29+
}
30+
if (search) {
31+
rows = rows.filter(function(item) {
32+
return item.name.indexOf(search) !== -1;
33+
});
34+
}
35+
if (['id', 'name', 'price'].indexOf(name) !== -1) {
36+
rows = rows.sort(function(a, b) {
37+
var c = a[name],
38+
d = b[name];
39+
40+
if (name === 'price') {
41+
c = +c.substring(1);
42+
d = +d.substring(1);
43+
}
44+
if (c < d) {
45+
return order === 'asc' ? -1 : 1;
46+
}
47+
if (c > d) {
48+
return order === 'asc' ? 1 : -1;
49+
}
50+
return 0;
51+
});
52+
}
53+
54+
if (max > rows.length) {
55+
max = rows.length;
56+
}
57+
58+
result.total = rows.length;
59+
for (i = offset; i < max; i++) {
60+
result.rows.push(rows[i]);
61+
}
62+
res.json(result);
63+
}
64+
}
65+
};
66+
67+
module.exports = function(req, res) {
68+
res.header('Access-Control-Allow-Origin', '*');
69+
res.header('Access-Control-Allow-Headers', 'X-Requested-With');
70+
projects[req.params.project][req.params.func](req, res);
71+
};

package.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "bootstrap-table-examples",
3+
"version": "1.0.0",
4+
"description": "bootstrap-table-examples ======================",
5+
"repository": {
6+
"type": "git",
7+
"url": "https://github.com/wenzhixin/bootstrap-table-examples.git"
8+
},
9+
"author": "wenzhixin <[email protected]> (http://wenzhixin.net.cn/)",
10+
"license": "MIT",
11+
"bugs": {
12+
"url": "https://github.com/wenzhixin/bootstrap-table-examples/issues"
13+
},
14+
"homepage": "https://github.com/wenzhixin/bootstrap-table-examples",
15+
"dependencies": {
16+
"express": "^4.12.4"
17+
}
18+
}

0 commit comments

Comments
 (0)