Skip to Main Content

Do-It-Yourself LibGuide Enhancements: Sortable Tables

A companion to a presentation given at the 2019 Hawaii Library Association Annual Conference by librarian Ralph Toyama, on how libraries can add features to their LibGuides environment using custom CSS and JavaScript.

Creating Sortable Tables

In our Course Reserves page, we have a list of materials on reserve that can be sorted by course, instructor, or item title. This uses JavaScript from W3Schools.com.

To use this code, you could add it to your Custom JS/CSS box, but because it is so long and complex, it would be neater to save it as a text file with a .js extension and upload it to LibGuides using their Upload Customization Files function (or any other web server you have access to).

When you create your table, for the columns you want to be able to sort on, include in the table header <th> tags these attributes: onclick="sortTable(0)" and onkeypress="sortTable(0)"

Use the number 0 for the table's first column, 1 for the second column, etc.

Below your table (either in the same box or in another box lower in the page), add a link to your script page. For example, to link to an uploaded file, it would look like this:

<script type="text/javascript" src="//libapps.s3.amazonaws.com/sites/5173/include/tablesorter.js"></script>

Table Sorting Code

The JavaScript code from W3Schools:

function sortTable(n) {
  var table, rows, switching, i, x, y, shouldSwitch, dir, switchcount = 0;
  table = document.getElementById("myTable");
  switching = true;
  //Set the sorting direction to ascending:
  dir = "asc"; 
  /*Make a loop that will continue until
  no switching has been done:*/
  while (switching) {
    //start by saying: no switching is done:
    switching = false;
    rows = table.getElementsByTagName("TR");
    /*Loop through all table rows (except the
    first, which contains table headers):*/
    for (i = 1; i < (rows.length - 1); i++) {
      //start by saying there should be no switching:
      shouldSwitch = false;
      /*Get the two elements you want to compare,
      one from current row and one from the next:*/
      x = rows[i].getElementsByTagName("TD")[n];
      y = rows[i + 1].getElementsByTagName("TD")[n];
      /*check if the two rows should switch place,
      based on the direction, asc or desc:*/
      if (dir == "asc") {
        if (x.innerHTML.toLowerCase() > y.innerHTML.toLowerCase()) {
          //if so, mark as a switch and break the loop:
          shouldSwitch= true;
          break;
        }
      } else if (dir == "desc") {
        if (x.innerHTML.toLowerCase() < y.innerHTML.toLowerCase()) {
          //if so, mark as a switch and break the loop:
          shouldSwitch= true;
          break;
        }
      }
    }
    if (shouldSwitch) {
      /*If a switch has been marked, make the switch
      and mark that a switch has been done:*/
      rows[i].parentNode.insertBefore(rows[i + 1], rows[i]);
      switching = true;
      //Each time a switch is done, increase this count by 1:
      switchcount ++;      
    } else {
      /*If no switching has been done AND the direction is "asc",
      set the direction to "desc" and run the while loop again.*/
      if (switchcount == 0 && dir == "asc") {
        dir = "desc";
        switching = true;
      }
    }
  }
}