@@ -20,8 +20,7 @@ const ModelProperty = {
2020 PAGE : "page" ,
2121 PAGE_SIZE : "page_size" ,
2222 ROW_COUNT : "row_count" ,
23- SORT_ASCENDING : "sort_ascending" ,
24- SORT_COLUMNS : "sort_columns" ,
23+ SORT_CONTEXT : "sort_context" ,
2524 TABLE_HTML : "table_html" ,
2625} ;
2726
@@ -141,8 +140,10 @@ function render({ model, el }) {
141140
142141 // Get sortable columns from backend
143142 const sortableColumns = model . get ( ModelProperty . ORDERABLE_COLUMNS ) ;
144- const currentSortColumns = model . get ( ModelProperty . SORT_COLUMNS ) ;
145- const currentSortAscending = model . get ( ModelProperty . SORT_ASCENDING ) ;
143+ const currentSortContext = model . get ( ModelProperty . SORT_CONTEXT ) || [ ] ;
144+
145+ const getSortIndex = ( colName ) =>
146+ currentSortContext . findIndex ( ( item ) => item . column === colName ) ;
146147
147148 // Add click handlers to column headers for sorting
148149 const headers = tableContainer . querySelectorAll ( "th" ) ;
@@ -161,9 +162,10 @@ function render({ model, el }) {
161162
162163 // Determine sort indicator and initial visibility
163164 let indicator = "●" ; // Default: unsorted (dot)
164- const sortIndex = currentSortColumns . indexOf ( columnName ) ;
165+ const sortIndex = getSortIndex ( columnName ) ;
166+
165167 if ( sortIndex !== - 1 ) {
166- const isAscending = currentSortAscending [ sortIndex ] ;
168+ const isAscending = currentSortContext [ sortIndex ] . ascending ;
167169 indicator = isAscending ? "▲" : "▼" ;
168170 indicatorSpan . style . visibility = "visible" ; // Sorted arrows always visible
169171 } else {
@@ -180,57 +182,58 @@ function render({ model, el }) {
180182
181183 // Add hover effects for unsorted columns only
182184 header . addEventListener ( "mouseover" , ( ) => {
183- if ( currentSortColumns . indexOf ( columnName ) === - 1 ) {
185+ if ( getSortIndex ( columnName ) === - 1 ) {
184186 indicatorSpan . style . visibility = "visible" ;
185187 }
186188 } ) ;
187189 header . addEventListener ( "mouseout" , ( ) => {
188- if ( currentSortColumns . indexOf ( columnName ) === - 1 ) {
190+ if ( getSortIndex ( columnName ) === - 1 ) {
189191 indicatorSpan . style . visibility = "hidden" ;
190192 }
191193 } ) ;
192194
193195 // Add click handler for three-state toggle
194196 header . addEventListener ( Event . CLICK , ( event ) => {
195- const sortIndex = currentSortColumns . indexOf ( columnName ) ;
196- let newColumns = [ ...currentSortColumns ] ;
197- let newAscending = [ ...currentSortAscending ] ;
197+ const sortIndex = getSortIndex ( columnName ) ;
198+ let newContext = [ ...currentSortContext ] ;
198199
199200 if ( event . shiftKey ) {
200201 if ( sortIndex !== - 1 ) {
201202 // Already sorted. Toggle or Remove.
202- if ( newAscending [ sortIndex ] ) {
203+ if ( newContext [ sortIndex ] . ascending ) {
203204 // Asc -> Desc
204- newAscending [ sortIndex ] = false ;
205+ // Clone object to avoid mutation issues
206+ newContext [ sortIndex ] = {
207+ ...newContext [ sortIndex ] ,
208+ ascending : false ,
209+ } ;
205210 } else {
206211 // Desc -> Remove
207- newColumns . splice ( sortIndex , 1 ) ;
208- newAscending . splice ( sortIndex , 1 ) ;
212+ newContext . splice ( sortIndex , 1 ) ;
209213 }
210214 } else {
211215 // Not sorted -> Append Asc
212- newColumns . push ( columnName ) ;
213- newAscending . push ( true ) ;
216+ newContext . push ( { column : columnName , ascending : true } ) ;
214217 }
215218 } else {
216219 // No shift key. Single column mode.
217- if ( sortIndex !== - 1 && newColumns . length === 1 ) {
220+ if ( sortIndex !== - 1 && newContext . length === 1 ) {
218221 // Already only this column. Toggle or Remove.
219- if ( newAscending [ sortIndex ] ) {
220- newAscending [ sortIndex ] = false ;
222+ if ( newContext [ sortIndex ] . ascending ) {
223+ newContext [ sortIndex ] = {
224+ ...newContext [ sortIndex ] ,
225+ ascending : false ,
226+ } ;
221227 } else {
222- newColumns = [ ] ;
223- newAscending = [ ] ;
228+ newContext = [ ] ;
224229 }
225230 } else {
226231 // Start fresh with this column
227- newColumns = [ columnName ] ;
228- newAscending = [ true ] ;
232+ newContext = [ { column : columnName , ascending : true } ] ;
229233 }
230234 }
231235
232- model . set ( ModelProperty . SORT_COLUMNS , newColumns ) ;
233- model . set ( ModelProperty . SORT_ASCENDING , newAscending ) ;
236+ model . set ( ModelProperty . SORT_CONTEXT , newContext ) ;
234237 model . save_changes ( ) ;
235238 } ) ;
236239 }
0 commit comments