Skip to content

Commit

Permalink
Merge pull request #198 from jmlee2k/custom-sort-attributes
Browse files Browse the repository at this point in the history
Adds ability to sort via a custom attribute
  • Loading branch information
tristen committed Oct 30, 2021
2 parents efdb520 + 6aa16dc commit 1a1bcda
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 11 deletions.
7 changes: 7 additions & 0 deletions demo/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ Sometimes text inside cells is not normalized. Using a `data-sort` attribute you
</tr>
{% endhighlight %}

You can use a custom attribute (instead of `data-sort`) using the `sortAttribute` option:

{% highlight js %}
var table = document.getElementById('table-id');
var sort = new Tablesort(table, { sortAttribute: 'data-custom-sort-val'});
{% endhighlight %}

### Specify the sort method for a column

By adding a `data-sort-method` attribute to a table heading you can force Tablesort to use a specific sorting method rather than guessing it. The value of `data-sort-method` corresponds to the name of a sort function.
Expand Down
2 changes: 1 addition & 1 deletion dist/sorts/tablesort.date.min.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*!
* tablesort v5.2.1 (2020-06-02)
* tablesort v5.2.1 (2020-07-06)
* http://tristen.ca/tablesort/demo/
* Copyright (c) 2020 ; Licensed MIT
*/
Expand Down
2 changes: 1 addition & 1 deletion dist/sorts/tablesort.dotsep.min.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*!
* tablesort v5.2.1 (2020-06-02)
* tablesort v5.2.1 (2020-07-06)
* http://tristen.ca/tablesort/demo/
* Copyright (c) 2020 ; Licensed MIT
*/
Expand Down
2 changes: 1 addition & 1 deletion dist/sorts/tablesort.filesize.min.js

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

2 changes: 1 addition & 1 deletion dist/sorts/tablesort.monthname.min.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*!
* tablesort v5.2.1 (2020-06-02)
* tablesort v5.2.1 (2020-07-06)
* http://tristen.ca/tablesort/demo/
* Copyright (c) 2020 ; Licensed MIT
*/
Expand Down
2 changes: 1 addition & 1 deletion dist/sorts/tablesort.number.min.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*!
* tablesort v5.2.1 (2020-06-02)
* tablesort v5.2.1 (2020-07-06)
* http://tristen.ca/tablesort/demo/
* Copyright (c) 2020 ; Licensed MIT
*/
Expand Down
4 changes: 2 additions & 2 deletions dist/tablesort.min.js

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

8 changes: 4 additions & 4 deletions src/tablesort.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
return evt;
};

var getInnerText = function(el) {
return el.getAttribute('data-sort') || el.textContent || el.innerText || '';
var getInnerText = function(el,options) {
return el.getAttribute(options.sortAttribute || 'data-sort') || el.textContent || el.innerText || '';
};

// Default sort method if no better sort method is found
Expand Down Expand Up @@ -172,7 +172,7 @@
}

// Treat missing cells as empty cells
item = cell ? getInnerText(cell) : "";
item = cell ? getInnerText(cell,that.options) : "";

item = item.trim();

Expand Down Expand Up @@ -228,7 +228,7 @@
// Save the index for stable sorting
newRows.push({
tr: item,
td: cell ? getInnerText(cell) : '',
td: cell ? getInnerText(cell,that.options) : '',
index: totalRows
});
}
Expand Down
24 changes: 24 additions & 0 deletions test/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,28 @@
</tbody>
</table>

<table id="sort-custom-attribute">
<thead>
<tr>
<th>Heros</th>
</tr>
</thead>
<tbody>
<tr>
<td data-realname="Tony Stark">Iron Man (Tony Stark)</td>
</tr>
<tr>
<td data-realname="Peter Parker">Spider-Man (Peter Parker)</td>
</tr>
<tr>
<td data-realname="Natasha Romanoff">Black Widow (Natasha Romanoff)</td>
</tr>
<tr>
<td data-realname="Steve Rogers">Captain America (Steve Rogers)</td>
</tr>
</tbody>
</table>

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

<script src='../src/tablesort.js'></script>
Expand All @@ -221,6 +243,7 @@
tableSortRowSet = document.getElementById('sort-row-set');
tableSortRowAuto = document.getElementById('sort-row-auto');
tableSortColumnKeys = document.getElementById('sort-column-keys');
tableSortCustomAttr = document.getElementById('sort-custom-attribute');
new Tablesort(table);
new Tablesort(tableDescend, { descending: true });
new Tablesort(tableExclude);
Expand All @@ -229,6 +252,7 @@
new Tablesort(tableSortRowSet);
new Tablesort(tableSortRowAuto);
new Tablesort(tableSortColumnKeys);
new Tablesort(tableSortCustomAttr, { sortAttribute: "data-realname" });
var refresh = new Tablesort(tableRefresh);

var rowCount = tableRefresh.rows.length;
Expand Down
15 changes: 15 additions & 0 deletions test/spec/tablesort.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,3 +193,18 @@ tape('sorts with column keys', function(t) {
t.equal(tableSortColumnKeys.rows[3].cells[1].innerHTML, '3', 'was 3');
t.end();
})

tape('uses custom sort attribute', function(t) {
var tbl = tableSortCustomAttr;
var h = tbl.querySelector('th');

var clickEvent = document.createEvent('HTMLEvents');
clickEvent.initEvent('click', true, false);
h.dispatchEvent(clickEvent);

t.equal(tbl.rows[1].cells[0].innerHTML, 'Black Widow (Natasha Romanoff)');
t.equal(tbl.rows[2].cells[0].innerHTML, 'Spider-Man (Peter Parker)');
t.equal(tbl.rows[3].cells[0].innerHTML, 'Captain America (Steve Rogers)');
t.equal(tbl.rows[4].cells[0].innerHTML, 'Iron Man (Tony Stark)');
t.end();
})

0 comments on commit 1a1bcda

Please sign in to comment.