Book Image

Instant jQuery 2.0 Table Manipulation How-to

By : Charlie Griefer
Book Image

Instant jQuery 2.0 Table Manipulation How-to

By: Charlie Griefer

Overview of this book

When jQuery was introduced, it took the JavaScript world by storm. Years later, it shows no sign of slowing down. Its powerful yet concise syntax helps to simplify tasks that might otherwise be difficult or complex. Whether you're a JavaScript novice or an expert, jQuery is a valuable addition to your toolbox.Instant jQuery 2.0 Table Manipulation How-to shows you how to quickly and easily add style and functionality to your HTML tables. You'll be amazed to see how easy it is. Just don't let your customers know!HTML tables can be boring. Sure, we can make them more stylish with CSS, but users want interactivity. They want to be able to sort columns, see totals, filter data, and page through information in easily digestible chunks. Starting off with a plain HTML table, and an ID and a few class attributes, you will see the transformations occur with just a few lines of jQuery. Instant jQuery 2.0 Table Manipulation How-to progresses to showing you how to highlight specific table cells and filter table data, and even sort columns or page through data. Regardless of your skill level with JavaScript or jQuery, this book will walk you through the deceptively simple steps needed to add functionality and interactivity to your HTML tables.
Table of Contents (7 chapters)

Filtering (Become an expert)


Paging and sorting a table adds interactivity to your site, and can help your users locate specific bits of information. But if they know what they're looking for, rather than having to page or sort, it would be efficient to give them a filter field.

Getting ready

We've come to love it, so stick with the table of movies. This time, underneath the table, add a form field.

How to do it...

  1. You should be able to bang out this particular table code in your sleep, so we'll forego displaying that code. But underneath the table, add a form field as follows:

    <div>
      Release Year: <input type="text" name="filter" id="filter" />
    </div>
  2. The script will let users filter the release year. As they type into the form field, the keyup event will trigger the jQuery function, which will hide any rows that don't contain the text input by the user:

    <script type="text/javascript">
    $( document ).ready( function() {
     
      $( "input#filter" ).keyup( function() {
        var filtertext = $( this ).val();
    
        $( "#movies tbody tr" ).each( function( rowindex ) {
          $( this ).children( "td" ).eq( 2 ).each( function( cellindex ) {
            if ( $( this ).text().indexOf( filtertext ) < 0 ) {
              $( this ).parents( "tr" ).hide();
            } else {
              $( this ).parents( "tr" ).show();
            }
          });
        });
      })
    
    });
    </script>

How it works...

Start off by listening for a keyup event this time, rather than the click event that has been used in previous recipes. It's implemented the same way:

$( "input#filter" ).keyup( function() {

The first thing to do is to store the input in a local variable:

var filtertext = $( this ).val();

Now compare that text with the text in the Release Year column. To accomplish this, use two .each() loops.

The first loop will iterate over all of the rows in the tbody:

$( "#movies tbody tr" ).each( function( rowindex ) {

With each loop over the table rows, then loop over each td at index position 2. Say it with me, indexes start at 0, so position 2 is column 3.

$( this ).children( "td" ).eq( 2 ).each( function( cellindex ) {

With each iteration of the inner loop, compare the text in the td element to the text in the filter input. As long as the text in the td contains a substring of the text in the filter input, display it. For example, 193 would display 1932 and 1939, but would hide 1940, 1941, and so on. Do this with JavaScript's indexOf() function. indexOf() checks for the existence of a substring within a string. If the substring is not found, it returns -1. Otherwise, it returns the starting position of the first instance of the substring. For now, know that if the value returned is less than 0, the substring was not found, so hide the row.

if ( $( this ).text().indexOf( filtertext ) < 0 ) {
  $( this ).parents( "tr" ).hide();
} else {
   $( this ).parents( "tr" ).show();
}

Beyond just hiding rows that don't meet the criteria, use .show() to display rows that do, as they may have been hidden before, but now meet the filter criteria.

Filter functions can be very powerful. This was just a basic example of how to demonstrate the concept. You could have iterated over every table cell, or given the user a drop-down menu to select the column on which they want to filter. You also could have provided a submit button that filtered only when the button was clicked, rather than on each keyup.