Skip to content

Commit 9463e25

Browse files
Fix class inferance when using colspans. (#114)
1 parent 4c62e43 commit 9463e25

File tree

2 files changed

+34
-22
lines changed

2 files changed

+34
-22
lines changed

public/index.html

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -87,18 +87,20 @@ <h2>Testing table containing colspan and data-sort and multiple tbodies</h2>
8787
<th class="show_name">Show</th>
8888
<th colspan="2" class="data-sort">Overall</th>
8989
<th colspan="2" class="data-sort">On Our Dates</th>
90+
<th >On Our Dates</th>
9091
<th>First Sold Out</th>
9192
</tr>
9293
<tr>
9394
</thead>
9495
<td class="tags"></td>
9596
<td class="category">Comedy</td>
96-
<td class="show_name">Show 1</td>
97+
<td class="show_name">1</td>
9798
<td class="ratio all" data-sort="72">18/25</td>
9899
<td class="pct all">72%</td>
99100
<td class="ratio ours" data-sort="75">3/4</td>
100101
<td class="pct ours">75%</td>
101-
<td>1999-07-30</td>
102+
<td class="pct ours">2</td>
103+
<td>1999-7-30</td>
102104
</tr>
103105
<tr>
104106
<td class="tags"></td>
@@ -107,8 +109,9 @@ <h2>Testing table containing colspan and data-sort and multiple tbodies</h2>
107109
<td class="ratio all" data-sort="60">6/10</td>
108110
<td class="pct all">60%</td>
109111
<td class="ratio ours" data-sort="75">3/4</td>
112+
<td class="pct ours">8</td>
110113
<td class="pct ours">75%</td>
111-
<td>1999-08-04</td>
114+
<td>1999-8-04</td>
112115
</tr>
113116
<tr>
114117
<td class="tags"></td>
@@ -118,47 +121,52 @@ <h2>Testing table containing colspan and data-sort and multiple tbodies</h2>
118121
<td class="pct all">47%</td>
119122
<td class="ratio ours" data-sort="75">3/4</td>
120123
<td class="pct ours">75%</td>
121-
<td>1999-07-19</td>
124+
<td class="pct ours">2.12</td>
125+
<td>1999-7-19</td>
122126
</tr>
123127
<tr>
124128
<td class="tags"></td>
125129
<td class="category">Comedy</td>
126-
<td class="show_name">Show 4</td>
130+
<td class="show_name">4</td>
127131
<td class="ratio all" data-sort="67">10/15</td>
128132
<td class="pct all">67%</td>
129133
<td class="ratio ours" data-sort="67">2/3</td>
130134
<td class="pct ours">67%</td>
131-
<td>1999-07-19</td>
135+
<td class="pct ours">2.13</td>
136+
<td>1999-7-19</td>
132137
</tr>
133138
<tr>
134139
<td class="tags"></td>
135140
<td class="category">Comedy</td>
136-
<td class="show_name">Show 5</td>
141+
<td class="show_name">5</td>
137142
<td class="ratio all" data-sort="75">9/12</td>
138143
<td class="pct all">75%</td>
139144
<td class="ratio ours" data-sort="50">1/2</td>
140145
<td class="pct ours">50%</td>
141-
<td>1999-07-29</td>
146+
<td class="pct ours">2.83</td>
147+
<td>1999-7-29</td>
142148
</tr>
143149
<tr>
144150
<td class="tags"></td>
145151
<td class="category">Comedy</td>
146-
<td class="show_name">Show 6</td>
152+
<td class="show_name">6</td>
147153
<td class="ratio all" data-sort="67">16/24</td>
148154
<td class="pct all">67%</td>
149155
<td class="ratio ours" data-sort="50">2/4</td>
150156
<td class="pct ours">50%</td>
151-
<td>1999-07-26</td>
157+
<td class="pct ours">2.83</td>
158+
<td>1999-7-26</td>
152159
</tr>
153160
<tr>
154161
<td class="tags"></td>
155162
<td class="category">Comedy</td>
156-
<td class="show_name">Show 7</td>
163+
<td class="show_name">7</td>
157164
<td class="ratio all" data-sort="62">16/26</td>
158165
<td class="pct all">62%</td>
159166
<td class="ratio ours" data-sort="50">2/4</td>
160167
<td class="pct ours">50%</td>
161-
<td>2022-07-31</td>
168+
<td class="pct ours">2.03</td>
169+
<td>2022-7-31</td>
162170
</tr>
163171
<thead>
164172
<tr>

public/table-sort.js

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
5454
}
5555
}
5656

57-
function inferSortClasses(tableRows, columnIndex, th) {
57+
function inferSortClasses(tableRows, columnIndex, column, th) {
5858
const runtimeRegex = /^(\d+h)?\s?(\d+m)?\s?(\d+s)?$/i;
5959
const fileSizeRegex = /^([.0-9]+)\s?(B|KB|KiB|MB|MiB|GB|GiB|TB|TiB)/i;
6060
// Doesn't infer dates with delimiter "."; as could capture semantic version numbers.
@@ -75,7 +75,13 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
7575
if (regexNotFoundCount >= threshold) {
7676
break;
7777
}
78-
const tableColumn = tr.querySelectorAll("td").item(columnIndex);
78+
const tableColumn = tr
79+
.querySelectorAll("td")
80+
.item(
81+
column.span[columnIndex] === 1
82+
? column.spanSum[columnIndex] - 1
83+
: column.spanSum[columnIndex] - column.span[columnIndex]
84+
);
7985
let foundMatch = false;
8086
for (let key of Object.keys(inferableClasses)) {
8187
let classRegexp = inferableClasses[key].regexp;
@@ -129,11 +135,13 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
129135
headerIndex++
130136
) {
131137
let columnIndexesClicked = [];
138+
const column = { span: {}, spanSum: {} };
139+
getColSpanData(table.headers[headerIndex], column);
132140
for (let [columnIndex, th] of table.headers[headerIndex].entries()) {
133141
if (!th.classList.contains("disable-sort")) {
134142
th.style.cursor = "pointer";
135143
if (!table.hasClass.noClassInfer) {
136-
inferSortClasses(table.rows[headerIndex], columnIndex, th);
144+
inferSortClasses(table.rows[headerIndex], columnIndex, column, th);
137145
}
138146
makeEachColumnSortable(
139147
th,
@@ -416,13 +424,9 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
416424
template.innerHTML = columnIndexAndTableRow[column.toBeSorted[i]];
417425
tr = template.content.firstChild;
418426
}
419-
let getColumnTd = column.getColumn(
420-
tr,
421-
column.spanSum,
422-
column.span
423-
);
424-
let fileSizeInBytesHTML = getColumnTd.outerHTML
425-
const fileSizeInBytesText = getColumnTd.textContent
427+
let getColumnTd = column.getColumn(tr, column.spanSum, column.span);
428+
let fileSizeInBytesHTML = getColumnTd.outerHTML;
429+
const fileSizeInBytesText = getColumnTd.textContent;
426430
const fileSize = column.toBeSorted[i].replace(/#[0-9]*/, "");
427431
let prefixes = ["", "Ki", "Mi", "Gi", "Ti", "Pi"];
428432
let replaced = false;

0 commit comments

Comments
 (0)