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
117 lines (102 loc) · 2.76 KB
/
table-sort.js
File metadata and controls
117 lines (102 loc) · 2.76 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import { sortDate } from '../../utils/sort.js'
import { isNumber, isDate, isObject } from '../../utils/is.js'
export default {
props: {
sortMethod: {
type: Function,
default (data, sortBy, descending) {
const col = this.colList.find(def => def.name === sortBy)
if (col === void 0 || col.field === void 0) {
return data
}
const
dir = descending === true ? -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 !== void 0) {
return col.sort(A, B, a, b) * dir
}
if (isNumber(A) === true && isNumber(B) === true) {
return (A - B) * dir
}
if (isDate(A) === true && isDate(B) === true) {
return sortDate(A, B) * dir
}
if (typeof A === 'boolean' && typeof B === 'boolean') {
return (A - B) * dir
}
[A, B] = [A, B].map(s => (s + '').toLocaleString().toLowerCase())
return A < B
? -1 * dir
: (A === B ? 0 : dir)
})
}
},
columnSortOrder: {
type: String,
validator: v => v === 'ad' || v === 'da',
default: 'ad'
}
},
computed: {
columnToSort () {
const { sortBy } = this.computedPagination
if (sortBy) {
return this.colList.find(def => def.name === sortBy) || null
}
}
},
methods: {
sort (col /* String(col name) or Object(col definition) */) {
let sortOrder = this.columnSortOrder
if (isObject(col) === true) {
if (col.sortOrder) {
sortOrder = col.sortOrder
}
col = col.name
}
else {
const def = this.colList.find(def => def.name === col)
if (def !== void 0 && def.sortOrder) {
sortOrder = def.sortOrder
}
}
let { sortBy, descending } = this.computedPagination
if (sortBy !== col) {
sortBy = col
descending = sortOrder === 'da'
}
else if (this.binaryStateSort === true) {
descending = !descending
}
else if (descending === true) {
if (sortOrder === 'ad') {
sortBy = null
}
else {
descending = false
}
}
else { // ascending
if (sortOrder === 'ad') {
descending = true
}
else {
sortBy = null
}
}
this.setPagination({ sortBy, descending, page: 1 })
}
}
}