@@ -20,7 +20,8 @@ const ModelProperty = {
2020 PAGE : 'page' ,
2121 PAGE_SIZE : 'page_size' ,
2222 ROW_COUNT : 'row_count' ,
23- SORT_CONTEXT : 'sort_context' ,
23+ SORT_COLUMNS : 'sort_columns' ,
24+ SORT_ASCENDING : 'sort_ascending' ,
2425 TABLE_HTML : 'table_html' ,
2526 MAX_COLUMNS : 'max_columns' ,
2627} ;
@@ -192,10 +193,10 @@ function render({ model, el }) {
192193 } , 0 ) ;
193194
194195 const sortableColumns = model . get ( ModelProperty . ORDERABLE_COLUMNS ) ;
195- const currentSortContext = model . get ( ModelProperty . SORT_CONTEXT ) || [ ] ;
196+ const currentSortColumns = model . get ( ModelProperty . SORT_COLUMNS ) || [ ] ;
197+ const currentSortAscending = model . get ( ModelProperty . SORT_ASCENDING ) || [ ] ;
196198
197- const getSortIndex = ( colName ) =>
198- currentSortContext . findIndex ( ( item ) => item . column === colName ) ;
199+ const getSortIndex = ( colName ) => currentSortColumns . indexOf ( colName ) ;
199200
200201 const headers = tableContainer . querySelectorAll ( 'th' ) ;
201202 headers . forEach ( ( header ) => {
@@ -214,7 +215,7 @@ function render({ model, el }) {
214215 const sortIndex = getSortIndex ( columnName ) ;
215216
216217 if ( sortIndex !== - 1 ) {
217- const isAscending = currentSortContext [ sortIndex ] . ascending ;
218+ const isAscending = currentSortAscending [ sortIndex ] ;
218219 indicator = isAscending ? '▲' : '▼' ;
219220 indicatorSpan . style . visibility = 'visible' ; // Sorted arrows always visible
220221 } else {
@@ -239,48 +240,46 @@ function render({ model, el }) {
239240 }
240241 } ) ;
241242
242- // Add click handler for three-state toggle
243243 header . addEventListener ( Event . CLICK , ( event ) => {
244244 const sortIndex = getSortIndex ( columnName ) ;
245- let newContext = [ ...currentSortContext ] ;
245+ let newSortColumns = [ ...currentSortColumns ] ;
246+ let newSortAscending = [ ...currentSortAscending ] ;
246247
247248 if ( event . shiftKey ) {
248249 if ( sortIndex !== - 1 ) {
249250 // Already sorted. Toggle or Remove.
250- if ( newContext [ sortIndex ] . ascending ) {
251+ if ( newSortAscending [ sortIndex ] ) {
251252 // Asc -> Desc
252- // Clone object to avoid mutation issues
253- newContext [ sortIndex ] = {
254- ...newContext [ sortIndex ] ,
255- ascending : false ,
256- } ;
253+ newSortAscending [ sortIndex ] = false ;
257254 } else {
258255 // Desc -> Remove
259- newContext . splice ( sortIndex , 1 ) ;
256+ newSortColumns . splice ( sortIndex , 1 ) ;
257+ newSortAscending . splice ( sortIndex , 1 ) ;
260258 }
261259 } else {
262260 // Not sorted -> Append Asc
263- newContext . push ( { column : columnName , ascending : true } ) ;
261+ newSortColumns . push ( columnName ) ;
262+ newSortAscending . push ( true ) ;
264263 }
265264 } else {
266265 // No shift key. Single column mode.
267- if ( sortIndex !== - 1 && newContext . length === 1 ) {
266+ if ( sortIndex !== - 1 && newSortColumns . length === 1 ) {
268267 // Already only this column. Toggle or Remove.
269- if ( newContext [ sortIndex ] . ascending ) {
270- newContext [ sortIndex ] = {
271- ...newContext [ sortIndex ] ,
272- ascending : false ,
273- } ;
268+ if ( newSortAscending [ sortIndex ] ) {
269+ newSortAscending [ sortIndex ] = false ;
274270 } else {
275- newContext = [ ] ;
271+ newSortColumns = [ ] ;
272+ newSortAscending = [ ] ;
276273 }
277274 } else {
278275 // Start fresh with this column
279- newContext = [ { column : columnName , ascending : true } ] ;
276+ newSortColumns = [ columnName ] ;
277+ newSortAscending = [ true ] ;
280278 }
281279 }
282280
283- model . set ( ModelProperty . SORT_CONTEXT , newContext ) ;
281+ model . set ( ModelProperty . SORT_ASCENDING , newSortAscending ) ;
282+ model . set ( ModelProperty . SORT_COLUMNS , newSortColumns ) ;
284283 model . save_changes ( ) ;
285284 } ) ;
286285 }
0 commit comments