UNPKG

quasar

Version:

Build high-performance VueJS user interfaces (SPA, PWA, SSR, Mobile and Desktop) in record time

90 lines (77 loc) 2.17 kB
import { sortDate } from '../../utils/sort.js' import { isNumber, isDate } 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) }) } } }, 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) */) { if (col === Object(col)) { col = col.name } let { sortBy, descending } = this.computedPagination if (sortBy !== col) { sortBy = col descending = false } else if (this.binaryStateSort === true) { descending = !descending } else if (descending === true) { sortBy = null } else { descending = true } this.setPagination({ sortBy, descending, page: 1 }) } } }