Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML table sort

Tags:

html

sorting

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>
like image 322
h3tr1ck Avatar asked May 21 '12 10:05

h3tr1ck


People also ask

How do you sort a table by column in HTML?

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.

How do you sort a table in ascending order in HTML?

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).


2 Answers

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.

like image 134
verisimilitude Avatar answered Sep 23 '22 17:09

verisimilitude


Here is another library.

Changes required are -

  1. Add sorttable js

  2. 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>
like image 20
smilyface Avatar answered Sep 21 '22 17:09

smilyface