Thursday, August 11, 2022

Sortable HTML Table with simple jQuery script

   
Sortable Table

Using a simple jQuery script I create the table with a sorting option. Here you can sort the data inside the table in ascending and descending orders. On the first page loading the table data display in ascending order you want to sort to descending order just click the table header to rearrange the data to descending.

I used HTML, CSS, and jQuery for this sorting table. And I used the SVG icons for the arrow in the header. Just copy the below code and past it on your webpage, you can use the code.

HTML:

<table class="sortable">
  <thead>
    <tr>
      <th><span>Name 
        <svg viewBox="0 0 24 24">
    <path fill="currentColor" d="M7.41,8.58L12,13.17L16.59,8.58L18,10L12,16L6,10L7.41,8.58Z" />
</svg>
        </span></th>
      <th><span>Position
        <svg viewBox="0 0 24 24">
    <path fill="currentColor" d="M7.41,8.58L12,13.17L16.59,8.58L18,10L12,16L6,10L7.41,8.58Z" />
</svg>
        </span></th>
      <th><span>Office
        <svg viewBox="0 0 24 24">
    <path fill="currentColor" d="M7.41,8.58L12,13.17L16.59,8.58L18,10L12,16L6,10L7.41,8.58Z" />
</svg>
        </span></th>
      <th><span>Age
        <svg viewBox="0 0 24 24">
    <path fill="currentColor" d="M7.41,8.58L12,13.17L16.59,8.58L18,10L12,16L6,10L7.41,8.58Z" />
</svg>
        </span></th>
      <th><span>Start date
        <svg viewBox="0 0 24 24">
    <path fill="currentColor" d="M7.41,8.58L12,13.17L16.59,8.58L18,10L12,16L6,10L7.41,8.58Z" />
</svg>
        </span></th>
      <th><span>Salary
        <svg viewBox="0 0 24 24">
    <path fill="currentColor" d="M7.41,8.58L12,13.17L16.59,8.58L18,10L12,16L6,10L7.41,8.58Z" />
</svg>
        </span></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Tiger Nixon</td>
      <td>System Architect</td>
      <td>Edinburgh</td>
      <td>61</td>
      <td>2011/04/25</td>
      <td>$320,800</td>
    </tr>
    <tr>
      <td>Garrett Winters</td>
      <td>Accountant</td>
      <td>Tokyo</td>
      <td>63</td>
      <td>2011/07/25</td>
      <td>$170,750</td>
    </tr>
    <tr>
      <td>Ashton Cox</td>
      <td>Junior Technical Author</td>
      <td>San Francisco</td>
      <td>66</td>
      <td>2009/01/12</td>
      <td>$86,000</td>
    </tr>
    <tr>
      <td>Cedric Kelly</td>
      <td>Senior Javascript Developer</td>
      <td>Edinburgh</td>
      <td>22</td>
      <td>2012/03/29</td>
      <td>$433,060</td>
    </tr>
    <tr>
      <td>Airi Satou</td>
      <td>Accountant</td>
      <td>Tokyo</td>
      <td>33</td>
      <td>2008/11/28</td>
      <td>$162,700</td>
    </tr>
    <tr>
      <td>Brielle Williamson</td>
      <td>Integration Specialist</td>
      <td>New York</td>
      <td>61</td>
      <td>2012/12/02</td>
      <td>$372,000</td>
    </tr>
    <tr>
      <td>Herrod Chandler</td>
      <td>Sales Assistant</td>
      <td>San Francisco</td>
      <td>59</td>
      <td>2012/08/06</td>
      <td>$137,500</td>
    </tr>
    <tr>
      <td>Rhona Davidson</td>
      <td>Integration Specialist</td>
      <td>Tokyo</td>
      <td>55</td>
      <td>2010/10/14</td>
      <td>$327,900</td>
    </tr>
    <tr>
      <td>Colleen Hurst</td>
      <td>Javascript Developer</td>
      <td>San Francisco</td>
      <td>39</td>
      <td>2009/09/15</td>
      <td>$205,500</td>
    </tr>
    <tr>
      <td>Sonya Frost</td>
      <td>Software Engineer</td>
      <td>Edinburgh</td>
      <td>23</td>
      <td>2008/12/13</td>
      <td>$103,600</td>
    </tr>
    <tr>
      <td>Jena Gaines</td>
      <td>Office Manager</td>
      <td>London</td>
      <td>30</td>
      <td>2008/12/19</td>
      <td>$90,560</td>
    </tr>
    <tr>
      <td>Quinn Flynn</td>
      <td>Support Lead</td>
      <td>Edinburgh</td>
      <td>22</td>
      <td>2013/03/03</td>
      <td>$342,000</td>
    </tr>
    <tr>
      <td>Charde Marshall</td>
      <td>Regional Director</td>
      <td>San Francisco</td>
      <td>36</td>
      <td>2008/10/16</td>
      <td>$470,600</td>
    </tr>
    <tr>
      <td>Haley Kennedy</td>
      <td>Senior Marketing Designer</td>
      <td>London</td>
      <td>43</td>
      <td>2012/12/18</td>
      <td>$313,500</td>
    </tr>
    <tr>
      <td>Tatyana Fitzpatrick</td>
      <td>Regional Director</td>
      <td>London</td>
      <td>19</td>
      <td>2010/03/17</td>
      <td>$385,750</td>
    </tr>
    <tr>
      <td>Michael Silva</td>
      <td>Marketing Designer</td>
      <td>London</td>
      <td>66</td>
      <td>2012/11/27</td>
      <td>$198,500</td>
    </tr>
    <tr>
      <td>Paul Byrd</td>
      <td>Chief Financial Officer (CFO)</td>
      <td>New York</td>
      <td>64</td>
      <td>2010/06/09</td>
      <td>$725,000</td>
    </tr>
  </tbody>
</table>

CSS:

body{
  font-family:arial;
}
.sortable {
  width: 100%;
}

.sortable th {
  background-color: #f2f2f2;
  padding: 8px;
  cursor:pointer;
}

.sortable td {
  border-bottom: 1px solid #ddd;
  padding: 8px;
}

.sortable .focus {
  background-color: #e7e7e7;
}

.sortable .selected {
  background-color: #e7e7e7;
}

.sortable .ascending svg {
  transform: rotate(0deg);
}

.sortable .descending svg{
  transform: rotate(180deg);
}

.sortable th span {
  padding:0 25px 0 0;
  position:relative;
}

.sortable th span svg {
  width: 20px;
  position: absolute;
  top: -1px;
  right: 3px;
}

jQuery:

$('.sortable th').each(function(column) {
    $(this).hover(
      function() {
        $(this).addClass('focus');
      },
      function() {
        $(this).removeClass('focus');
      }
    );
    $(this).click(function() {
      if ($(this).is('.ascending')) {
        $(this).removeClass('ascending');
        $(this).addClass('desc selected');
        sortOrder = -1;
      } else {
        $(this).addClass('ascending selected');
        $(this).removeClass('descending');
        sortOrder = 1;
      }
      $(this).siblings().removeClass('ascending selected');
      $(this).siblings().removeClass('descending selected');
      var arrData = $(this).parent().parent().parent().find('tbody >tr:has(td)').get();

      arrData.sort(function(first, second) {
        var val1 = $(first).children('td').eq(column).text().toUpperCase();
        var val2 = $(second).children('td').eq(column).text().toUpperCase();
        if ($.isNumeric(val1) && $.isNumeric(val2))
          return sortOrder == 1 ? val1 - val2 : val2 - val1;
        else
          return (val1 < val2) ? -sortOrder : (val1 > val2) ? sortOrder : 0;
      });
      $.each(arrData, function(index, row) {
        $(this).parent().parent().parent().find('tbody').append(row);
      });
    });
  });

OUTPUT

No comments:

Post a Comment