@@ -57,10 +57,12 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
5757
5858 const tableHead = sortableTable . querySelector ( "thead" ) ;
5959 const tableHeadHeaders = tableHead . querySelectorAll ( "th" ) ;
60- tableHead . style . cursor = "pointer" ;
6160
6261 for ( let [ columnIndex , th ] of tableHeadHeaders . entries ( ) ) {
63- makeEachColumnSortable ( th , columnIndex , tableBody , sortableTable ) ;
62+ if ( ! th . classList . contains ( "disable-sort" ) ) {
63+ th . style . cursor = "pointer" ;
64+ makeEachColumnSortable ( th , columnIndex , tableBody , sortableTable ) ;
65+ }
6466 }
6567 }
6668
@@ -86,6 +88,7 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
8688 }
8789
8890 function sortFileSize ( tableRows , columnData ) {
91+ // Handle filesize sorting (e.g KB, MB, GB, TB) - Turns data into KiB.
8992 const numberWithUnitType =
9093 / [ . 0 - 9 ] + ( \s ? B | \s ? K B | \s ? K i B | \s ? M B | \s ? M i B | \s ? G B | \s ? G i B | T \s ? B | \s ? T i B ) / i;
9194 const unitType =
@@ -152,6 +155,8 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
152155 let columnIndexesClicked = [ ] ;
153156
154157 function rememberSort ( timesClickedColumn , columnIndexesClicked ) {
158+ // Check if user has clicked different column from the first column if
159+ // yes reset times clicked.
155160 columnIndexesClicked . push ( columnIndex ) ;
156161 if ( timesClickedColumn === 1 && columnIndexesClicked . length > 1 ) {
157162 const lastColumnClicked =
@@ -165,12 +170,32 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
165170 }
166171 }
167172
168- function getTableData ( tableRows , columnData , isFileSize , isDataAttribute ) {
173+ function getColSpanData ( sortableTable , colSpanData , colSpanSum ) {
174+ sortableTable . querySelectorAll ( "th" ) . forEach ( ( th , index ) => {
175+ colSpanData [ index ] = th . colSpan ;
176+ if ( index === 0 ) colSpanSum [ index ] = th . colSpan ;
177+ else colSpanSum [ index ] = colSpanSum [ index - 1 ] + th . colSpan ;
178+ } ) ;
179+ }
180+
181+ function getTableData (
182+ tableRows ,
183+ columnData ,
184+ isFileSize ,
185+ isDataAttribute ,
186+ colSpanData ,
187+ colSpanSum
188+ ) {
169189 for ( let [ i , tr ] of tableRows . entries ( ) ) {
170190 // inner text for column we click on
171191 let tdTextContent = tr
172192 . querySelectorAll ( "td" )
173- . item ( columnIndex ) . textContent ;
193+ . item (
194+ colSpanData [ columnIndex ] === 1
195+ ? colSpanSum [ columnIndex ] - 1
196+ : colSpanSum [ columnIndex ] - colSpanData [ columnIndex ]
197+ ) . textContent ;
198+
174199 if ( tdTextContent . length === 0 ) {
175200 tdTextContent = "" ;
176201 }
@@ -328,7 +353,9 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
328353
329354 th . addEventListener ( "click" , function ( ) {
330355 const columnData = [ ] ;
331- // To make it work even if there is a tr with display: none; in the table, only the tr that is currently displayed is subject to sorting.
356+ const colSpanData = { } ;
357+ const colSpanSum = { } ;
358+
332359 const visibleTableRows = Array . prototype . filter . call (
333360 tableBody . querySelectorAll ( "tr" ) ,
334361 ( tr ) => {
@@ -337,28 +364,32 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
337364 ) ;
338365
339366 let isDataAttribute = th . classList . contains ( "data-sort" ) ;
340- // Check if using data-sort attribute; if so sort by value of data-sort
341- // attribute.
342367 if ( isDataAttribute ) {
343368 sortDataAttributes ( visibleTableRows , columnData ) ;
344369 }
345370
346371 let isFileSize = th . classList . contains ( "file-size" ) ;
347- // Handle filesize sorting (e.g KB, MB, GB, TB) - Turns data into KiB.
348372 if ( isFileSize ) {
349373 sortFileSize ( visibleTableRows , columnData ) ;
350374 }
351375
352- // Checking if user has clicked different column from the first column if
353- // yes reset times clicked.
354376 let isRememberSort = sortableTable . classList . contains ( "remember-sort" ) ;
355377 if ( ! isRememberSort ) {
356378 rememberSort ( timesClickedColumn , columnIndexesClicked ) ;
357379 }
358380
359381 timesClickedColumn += 1 ;
360382
361- getTableData ( visibleTableRows , columnData , isFileSize , isDataAttribute ) ;
383+ getColSpanData ( sortableTable , colSpanData , colSpanSum ) ;
384+ // TODO: refactor function to take object.
385+ getTableData (
386+ visibleTableRows ,
387+ columnData ,
388+ isFileSize ,
389+ isDataAttribute ,
390+ colSpanData ,
391+ colSpanSum
392+ ) ;
362393 updateTable ( visibleTableRows , columnData , isFileSize ) ;
363394 } ) ;
364395 }
0 commit comments