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 Person
You get the gist. And now I want to let the user sort the html table by let's say sales person. How would I easily do that using a drop down menu?
<div class='menu'> <ul> <li><a href='#'><span>Sales Person</span></a> <ul> <li><a href='#'><span>Melissa</span></a></li> <li><a href='#'><span>Justin</span></a></li> <li><a href='#'><span>Judy</span></a></li> <li><a href='#'><span>Skipper</span></a></li> <li><a href='#'><span>Alex</span></a></li> </ul> </li> </ul> </div>
How to Make Sortable Tables. Adding the "sortable" class to a <table> element provides support for sorting by column value. Clicking the column headers will sort the table rows by that column's value.
Sort Table by Clicking the Headers. Click the headers to sort the table. Click "Name" to sort by names, and "Country" to sort by country. The first time you click, the sorting direction is ascending (A to Z).
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.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With