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