stationer / SortTable

A pure JavaScript (no dependencies) solution to make HTML Tables sortable
MIT License
37 stars 12 forks source link

Sorting Table on page load #4

Closed brmenezewe closed 4 years ago

brmenezewe commented 4 years ago

Hi, is it possible to set a specifc table column to be sorted by default on page load?

tyleruebele commented 4 years ago

Yes. You can simply call sortTable in a document load/ready event specifying the column number and direction. So long as you attach the event after you include SortTable, it should be ready to run. For example:

function _initialSort() {
    sortTable(document.getElementById('demo3'), 1, -1);
}
window.addEventListener
    ? window.addEventListener('load', _initialSort, false)
    : window.attachEvent && window.attachEvent('onload', _initialSort)
    ;
brmenezewe commented 4 years ago

It works, thank you!