Repository navigation
Expand file tree
/
Copy pathexamples.js
More file actions
79 lines (72 loc) · 1.83 KB
/
Copy pathexamples.js
File metadata and controls
79 lines (72 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/**
* @author zhixin wen <wenzhixin2010@gmail.com>
* examples for projects
*/
const projects = {
bootstrap_table: {
data(req, res) {
const offset = +req.query.offset || 0
const limit = +req.query.limit || 0
const search = req.query.search
const name = req.query.sort
const order = req.query.order || 'asc'
const total = req.query.total || 800
const filter = JSON.parse(req.query.filter || '{}')
let i
let max = offset + limit
let rows = []
const result = {
total,
totalNotFiltered: total,
rows: []
}
for (i = 0; i < result.total; i++) {
rows.push({
id: i,
name: `Item ${i}`,
price: `$${i}`
})
}
if (search) {
rows = rows.filter(item => item.name.includes(search))
}
if (Object.keys(filter).length) {
for (const [key, value] of Object.entries(filter)) {
rows = rows.filter(item => item[key].includes(value))
}
}
if (['id', 'name', 'price'].includes(name)) {
rows = rows.sort((a, b) => {
let c = a[name]
let d = b[name]
if (name === 'price') {
c = +c.substring(1)
d = +d.substring(1)
}
if (c < d) {
return order === 'asc' ? -1 : 1
}
if (c > d) {
return order === 'asc' ? 1 : -1
}
return 0
})
}
if (max > rows.length) {
max = rows.length
}
result.total = rows.length
if (max === 0) {
result.rows = rows
} else {
for (i = offset; i < max; i++) {
result.rows.push(rows[i])
}
}
res.json(result)
}
}
}
module.exports = (req, res) => {
projects[req.params.project][req.params.func](req, res)
}