Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow selection of row to trigger sorting #106

Merged
merged 1 commit into from
Jul 23, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/tablesort.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,15 @@

if (el.rows && el.rows.length > 0) {
if (el.tHead && el.tHead.rows.length > 0) {
firstRow = el.tHead.rows[el.tHead.rows.length - 1];
for (i = 0; i < el.tHead.rows.length; i++) {
if (el.tHead.rows[i].classList.contains("sort-row")) {
firstRow = el.tHead.rows[i];
break;
}
}
if (!firstRow) {
firstRow = el.tHead.rows[el.tHead.rows.length - 1];
}
that.thead = true;
} else {
firstRow = el.rows[0];
Expand Down
4 changes: 2 additions & 2 deletions tablesort.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions test/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,30 @@
</tbody>
</table>

<table id="sort-row-set">
<thead>
<tr class="sort-row"><th>Sort Row</th></tr>
<tr><th>Not Sort Row</th></tr>
</thead>
<tbody>
<tr><td>2</td></tr>
<tr><td>1</td></tr>
<tr><td>3</td></tr>
</tbody>
</table>

<table id="sort-row-auto">
<thead>
<tr><th>Not Sort Row</th></tr>
<tr><th>Sort Row</th></tr>
</thead>
<tbody>
<tr><td>2</td></tr>
<tr><td>1</td></tr>
<tr><td>3</td></tr>
</tbody>
</table>

<script src='tape.js'></script>

<script src='../src/tablesort.js'></script>
Expand All @@ -167,11 +191,15 @@
tableDefault = document.getElementById('sort-default');
tableRefresh = document.getElementById('sort-refresh');
tableMulti = document.getElementById('sort-multi');
tableSortRowSet = document.getElementById('sort-row-set');
tableSortRowAuto = document.getElementById('sort-row-auto');
new Tablesort(table);
new Tablesort(tableDescend, { descending: true });
new Tablesort(tableExclude);
new Tablesort(tableDefault);
new Tablesort(tableMulti);
new Tablesort(tableSortRowSet);
new Tablesort(tableSortRowAuto);
var refresh = new Tablesort(tableRefresh);

var rowCount = tableRefresh.rows.length;
Expand Down
10 changes: 10 additions & 0 deletions test/spec/tablesort.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,13 @@ tape('Appended row is sorted on refresh', function(t) {

t.end();
});

tape('sort row is first', function(t) {
t.equal(tableSortRowSet.querySelector(".sort-header").innerHTML, 'Sort Row');
t.end();
});

tape('sort row is last', function(t) {
t.equal(tableSortRowAuto.querySelector(".sort-header").innerHTML, 'Sort Row');
t.end();
});