forked from quasarframework/quasar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtable-sort.js
More file actions
85 lines (75 loc) · 1.97 KB
/
table-sort.js
File metadata and controls
85 lines (75 loc) · 1.97 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
80
81
82
83
84
85
import { sortDate } from '../../utils/sort'
import { isNumber, isDate } from '../../utils/is'
export default {
props: {
sortMethod: {
type: Function,
default (data, sortBy, descending) {
const col = this.computedCols.find(def => def.name === sortBy)
if (col === null || col.field === void 0) {
return data
}
const
dir = descending ? -1 : 1,
val = typeof col.field === 'function'
? v => col.field(v)
: v => v[col.field]
return data.sort((a, b) => {
let
A = val(a),
B = val(b)
if (A === null || A === void 0) {
return -1 * dir
}
if (B === null || B === void 0) {
return 1 * dir
}
if (col.sort) {
return col.sort(A, B) * dir
}
if (isNumber(A) && isNumber(B)) {
return (A - B) * dir
}
if (isDate(A) && isDate(B)) {
return sortDate(A, B) * dir
}
if (typeof A === 'boolean' && typeof B === 'boolean') {
return (a - b) * dir
}
[A, B] = [A, B].map(s => (s + '').toLowerCase())
return A < B
? -1 * dir
: (A === B ? 0 : dir)
})
}
}
},
computed: {
columnToSort () {
const { sortBy } = this.computedPagination
if (sortBy) {
const col = this.computedCols.find(def => def.name === sortBy)
return col || null
}
}
},
methods: {
sort (col /* String(col name) or Object(col definition) */) {
if (col === Object(col)) {
col = col.name
}
let { sortBy, descending } = this.computedPagination
if (sortBy !== col) {
sortBy = col
descending = false
}
else if (descending) {
sortBy = null
}
else {
descending = true
}
this.setPagination({ sortBy, descending, page: 1 })
}
}
}