So basically I am running a mysql query that fetches data from my database and displays it in an easy to read layout for my users.
Name-----Address----Sales
Here is another library.
Changes required are -
Add sorttable js
Add class name sortable
to table.
Click the table headers to sort the table accordingly:
<script src="https://www.kryogenix.org/code/browser/sorttable/sorttable.js"></script>
<table class="sortable">
<tr>
<th>Name</th>
<th>Address</th>
<th>Sales Person</th>
</tr>
<tr class="item">
<td>user:0001</td>
<td>UK</td>
<td>Melissa</td>
</tr>
<tr class="item">
<td>user:0002</td>
<td>France</td>
<td>Justin</td>
</tr>
<tr class="item">
<td>user:0003</td>
<td>San Francisco</td>
<td>Judy</td>
</tr>
<tr class="item">
<td>user:0004</td>
<td>Canada</td>
<td>Skipper</td>
</tr>
<tr class="item">
<td>user:0005</td>
<td>Christchurch</td>
<td>Alex</td>
</tr>
</table>
Another approach to sort HTML table. (based on W3.JS HTML Sort)
let tid = "#usersTable";
let headers = document.querySelectorAll(tid + " th");
// Sort the table element when clicking on the table headers
headers.forEach(function(element, i) {
element.addEventListener("click", function() {
w3.sortHTML(tid, ".item", "td:nth-child(" + (i + 1) + ")");
});
});
th {
cursor: pointer;
background-color: coral;
}
<script src="https://www.w3schools.com/lib/w3.js"></script>
<link href="https://www.w3schools.com/w3css/4/w3.css" rel="stylesheet" />
<p>Click the <strong>table headers</strong> to sort the table accordingly:</p>
<table id="usersTable" class="w3-table-all">
<!--
<tr>
<th onclick="w3.sortHTML('#usersTable', '.item', 'td:nth-child(1)')">Name</th>
<th onclick="w3.sortHTML('#usersTable', '.item', 'td:nth-child(2)')">Address</th>
<th onclick="w3.sortHTML('#usersTable', '.item', 'td:nth-child(3)')">Sales Person</th>
</tr>
-->
<tr>
<th>Name</th>
<th>Address</th>
<th>Sales Person</th>
</tr>
<tr class="item">
<td>user:2911002</td>
<td>UK</td>
<td>Melissa</td>
</tr>
<tr class="item">
<td>user:2201002</td>
<td>France</td>
<td>Justin</td>
</tr>
<tr class="item">
<td>user:2901092</td>
<td>San Francisco</td>
<td>Judy</td>
</tr>
<tr class="item">
<td>user:2801002</td>
<td>Canada</td>
<td>Skipper</td>
</tr>
<tr class="item">
<td>user:2901009</td>
<td>Christchurch</td>
<td>Alex</td>
</tr>
</table>
Check if you could go with any of the below mentioned JQuery plugins. Simply awesome and provide wide range of options to work through, and less pains to integrate. :)
https://github.com/paulopmx/Flexigrid - Flexgrid
http://datatables.net/index - Data tables.
https://github.com/tonytomov/jqGrid
If not, you need to have a link to those table headers that calls a server-side script to invoke the sort.
Flexbox-based tables can easily be sorted by using flexbox property "order".
Here's an example:
function sortTable() {
let table = document.querySelector("#table")
let children = [...table.children]
let sortedArr = children.map(e => e.innerText).sort((a, b) => a.localeCompare(b));
children.forEach(child => {
child.style.order = sortedArr.indexOf(child.innerText)
})
}
document.querySelector("#sort").addEventListener("click", sortTable)
#table {
display: flex;
flex-direction: column
}
<div id="table">
<div>Melissa</div>
<div>Justin</div>
<div>Judy</div>
<div>Skipper</div>
<div>Alex</div>
</div>
<button id="sort"> sort </button>
Explanation
The sortTable
function extracts the data of the table into an array, which is then sorted in alphabetic order. After that we loop through the table items and assign the CSS property order
equal to index of an item's data in our sorted array.
The way I have sorted HTML tables in the browser uses plain, unadorned Javascript.
The basic process is:
The table should, of course, be nice HTML. Something like this...
<table>
<thead>
<tr><th>Name</th><th>Age</th></tr>
</thead>
<tbody>
<tr><td>Sioned</td><td>62</td></tr>
<tr><td>Dylan</td><td>37</td></tr>
...etc...
</tbody>
</table>
So, first adding the click handlers...
const table = document.querySelector('table'); //get the table to be sorted
table.querySelectorAll('th') // get all the table header elements
.forEach((element, columnNo)=>{ // add a click handler for each
element.addEventListener('click', event => {
sortTable(table, columnNo); //call a function which sorts the table by a given column number
})
})
This won't work right now because the sortTable
function which is called in the event handler doesn't exist.
Lets write it...
function sortTable(table, sortColumn){
// get the data from the table cells
const tableBody = table.querySelector('tbody')
const tableData = table2data(tableBody);
// sort the extracted data
tableData.sort((a, b)=>{
if(a[sortColumn] > b[sortColumn]){
return 1;
}
return -1;
})
// put the sorted data back into the table
data2table(tableBody, tableData);
}
So now we get to the meat of the problem, we need to make the functions table2data
to get data out of the table, and data2table
to put it back in once sorted.
Here they are ...
// this function gets data from the rows and cells
// within an html tbody element
function table2data(tableBody){
const tableData = []; // create the array that'll hold the data rows
tableBody.querySelectorAll('tr')
.forEach(row=>{ // for each table row...
const rowData = []; // make an array for that row
row.querySelectorAll('td') // for each cell in that row
.forEach(cell=>{
rowData.push(cell.innerText); // add it to the row data
})
tableData.push(rowData); // add the full row to the table data
});
return tableData;
}
// this function puts data into an html tbody element
function data2table(tableBody, tableData){
tableBody.querySelectorAll('tr') // for each table row...
.forEach((row, i)=>{
const rowData = tableData[i]; // get the array for the row data
row.querySelectorAll('td') // for each table cell ...
.forEach((cell, j)=>{
cell.innerText = rowData[j]; // put the appropriate array element into the cell
})
tableData.push(rowData);
});
}
And that should do it.
A couple of things that you may wish to add (or reasons why you may wish to use an off the shelf solution): An option to change the direction and type of sort i.e. you may wish to sort some columns numerically ("10" > "2"
is false because they're strings, probably not what you want). The ability to mark a column as sorted. Some kind of data validation.