Skip to content

Commit 4aa9483

Browse files
committed
bs-table sordesc operatoon
1 parent e4b28c2 commit 4aa9483

File tree

3 files changed

+26
-27
lines changed

3 files changed

+26
-27
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
.idea
22
node_modules
3+
.editorconfig

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "bootstrap-vue-wrapper",
3-
"version": "1.9.12",
3+
"version": "1.10.0",
44
"description": "Bootstrap 5 components in Vue3 warapper.",
55
"author": "Gabor Zemko <[email protected]>",
66
"homepage": "https://github.com/zemkogabor/bootstrap-vue-wrapper",

src/components/bs-table/BsTable.vue

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
>
1313
<slot name="tr">
1414
{{ field.label }}
15-
<i v-if="isActiveOrderBy(field.key) && isSortDescDefined()" :class="getSortIconClass()" />
15+
<i v-if="isActiveOrderBy(field.key) && this.sortDesc !== null" :class="getSortIconClass()" />
1616
</slot>
1717
</th>
1818
</template>
@@ -104,6 +104,13 @@ export default {
104104
type: String,
105105
default: null,
106106
},
107+
/**
108+
* Sort is descending or ascending
109+
*/
110+
sortDesc: {
111+
type: Boolean,
112+
default: null,
113+
},
107114
/**
108115
* th element css lass
109116
*/
@@ -120,14 +127,6 @@ export default {
120127
},
121128
},
122129
emits: ['orderChanged'],
123-
data() {
124-
return {
125-
/**
126-
* Sort is desc or not. Default not defined.
127-
*/
128-
sortDesc: null,
129-
}
130-
},
131130
methods: {
132131
/**
133132
* Is order by active by field?
@@ -147,32 +146,33 @@ export default {
147146
isSortableField(field) {
148147
return field.sort === undefined || field.sort
149148
},
150-
/**
151-
* Is sort defined?
152-
*
153-
* @returns {boolean}
154-
*/
155-
isSortDescDefined() {
156-
return this.sortDesc !== null
157-
},
158149
/**
159150
* Sort icon class.
160151
*
161152
* @returns {string}
162153
*/
163154
getSortIconClass() {
155+
if (this.sortDesc === null) {
156+
throw new Error('Sort desc value is null, cannot calculate the sort icon!')
157+
}
158+
164159
return this.sortDesc ? 'bi bi-caret-up-fill' : 'bi bi-caret-down-fill'
165160
},
166161
/**
167-
* Set sort desc.
162+
* Calcuate sort desc value on click
163+
* Returns null if there is no sortDesc value.
168164
*/
169-
setSortDesc(field) {
170-
if (this.isSortDescDefined() && !this.isOrderByChanged(field)) {
171-
this.sortDesc = !this.sortDesc
172-
return
165+
calcSortDesc(field) {
166+
if (this.sortDesc === null) {
167+
return null
168+
}
169+
170+
if (!this.isOrderByChanged(field)) {
171+
// if the given order is already active, the sort will be the opposite
172+
return !this.sortDesc
173173
}
174174
175-
this.sortDesc = false // default (first click) sort is ASC
175+
return false // default (first click) sort is ASC
176176
},
177177
/**
178178
* Is order by changed?
@@ -188,9 +188,7 @@ export default {
188188
return
189189
}
190190
191-
this.setSortDesc(field.key)
192-
193-
this.$emit('orderChanged', { sortDesc: this.sortDesc, orderBy: field.key })
191+
this.$emit('orderChanged', { sortDesc: this.calcSortDesc(field.key), orderBy: field.key })
194192
},
195193
},
196194
}

0 commit comments

Comments
 (0)