Skip to content

Commit 935e50d

Browse files
committed
feat(QTable): new prop (column-sort-order) and new "columns" definition prop (sortOrder) quasarframework#8527
1 parent b7e66ad commit 935e50d

File tree

4 files changed

+51
-5
lines changed

4 files changed

+51
-5
lines changed

docs/src/pages/vue-components/table.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ columns: [ // array of Objects
6262
// * is 0 then leave a and b unchanged with respect to each other, but sorted with respect to all different elements
6363
// * is greater than 0 then sort b to an index lower than a, i.e. b comes first
6464

65+
// (optional; requires Quasar v1.15.11+) override 'column-sort-order' prop;
66+
// sets column sort order: 'ad' (ascending-descending) or 'da' (descending-ascending)
67+
sortOrder: 'ad', // or 'da'
68+
6569
// (optional) you can format the data with a function
6670
format: (val, row) => `${val}%`,
6771
// one more format example:

ui/dev/src/pages/components/data-table-part4.vue

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
:hide-no-data="hideNoData"
2121
:hide-pagination="hidePagination"
2222
no-hover
23+
column-sort-order="da"
2324
>
2425
<template v-slot:body="props">
2526
<q-tr :props="props">
@@ -181,7 +182,7 @@ export default {
181182
sortable: true
182183
},
183184
{ name: 'calories', align: 'center', label: 'Calories', field: 'calories', sortable: true },
184-
{ name: 'fat', label: 'Fat (g)', field: 'fat', sortable: true, style: 'width: 10px' },
185+
{ name: 'fat', sortOrder: 'ad', label: 'Fat (g)', field: 'fat', sortable: true, style: 'width: 10px' },
185186
{ name: 'carbs', label: 'Carbs (g)', field: 'carbs' },
186187
{ name: 'protein', label: 'Protein (g)', field: 'protein' },
187188
{ name: 'sodium', label: 'Sodium (mg)', field: 'sodium' },

ui/src/components/table/QTable.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,13 @@
208208
"examples": [ "-1", "0", "1" ]
209209
}
210210
},
211+
"sortOrder": {
212+
"type": "String",
213+
"desc": "Set column sort order: 'ad' (ascending-descending) or 'da' (descending-ascending); Overrides the 'column-sort-order' prop",
214+
"values": [ "ad", "da" ],
215+
"default": "ad",
216+
"addedIn": "v1.15.11"
217+
},
211218
"format": {
212219
"type": "Function",
213220
"desc": "Function you can apply to format your data",
@@ -348,6 +355,15 @@
348355
"category": "sorting"
349356
},
350357

358+
"column-sort-order": {
359+
"type": "String",
360+
"desc": "Set column sort order: 'ad' (ascending-descending) or 'da' (descending-ascending); It gets applied to all columns unless a column has its own sortOrder specified in the 'columns' definition prop",
361+
"values": [ "ad", "da" ],
362+
"default": "ad",
363+
"category": "sorting",
364+
"addedIn": "v1.15.11"
365+
},
366+
351367
"no-data-label": {
352368
"type": "String",
353369
"desc": "Override default text to display when no data is available",

ui/src/components/table/table-sort.js

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ export default {
4848
: (A === B ? 0 : dir)
4949
})
5050
}
51+
},
52+
53+
columnSortOrder: {
54+
type: String,
55+
validator: v => v === 'ad' || v === 'da',
56+
default: 'ad'
5157
}
5258
},
5359

@@ -63,24 +69,43 @@ export default {
6369

6470
methods: {
6571
sort (col /* String(col name) or Object(col definition) */) {
72+
let sortOrder = this.columnSortOrder
73+
6674
if (col === Object(col)) {
75+
if (col.sortOrder) {
76+
sortOrder = col.sortOrder
77+
}
78+
6779
col = col.name
6880
}
81+
else if (this.columns[col].sortOrder) {
82+
sortOrder = this.columns[col].sortOrder
83+
}
6984

7085
let { sortBy, descending } = this.computedPagination
7186

7287
if (sortBy !== col) {
7388
sortBy = col
74-
descending = false
89+
descending = sortOrder === 'da'
7590
}
7691
else if (this.binaryStateSort === true) {
7792
descending = !descending
7893
}
7994
else if (descending === true) {
80-
sortBy = null
95+
if (sortOrder === 'ad') {
96+
sortBy = null
97+
}
98+
else {
99+
descending = false
100+
}
81101
}
82-
else {
83-
descending = true
102+
else { // ascending
103+
if (sortOrder === 'ad') {
104+
descending = true
105+
}
106+
else {
107+
sortBy = null
108+
}
84109
}
85110

86111
this.setPagination({ sortBy, descending, page: 1 })

0 commit comments

Comments
 (0)