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)

Highlighting cells (Must know)


Use built-in jQuery traversal methods and selectors to parse the contents of each cell in a table and apply a particular style (for example, a yellow background or a red border) to all cells that meet a specified set of criteria.

Getting ready

Borrowing some data from Tiobe (http://www.tiobe.com/index.php/content/paperinfo/tpci/index.html), create a table of the top five programming languages for 2012. To make it "pop" a bit more, each <td> in the Ratings column that's over 10 percent will be highlighted in yellow, and each <td> in the Delta column that's less than zero will be highlighted in red. Each <td> in the Ratings column should have a class of ratings, and each <td> in the Delta column should have a class of delta.

Additionally, set up two CSS classes for the highlights as follows:

.highlight { background-color: #FFFF00; }      /* yellow */
.highlight-negative { background-color: #FF0000; }  /* red */

Initially, the table should look as follows:

How to do it...

  1. Once again, give the table an id attribute (but by now, you knew that), as shown in the following code snippet:

    <table border="1" id="tiobeTable">
    <thead>
    <tr>
      <th>Position<br />Dec 2012</th>
      <th>Position<br />Dec 2011</th>
      <th>Programming Language</th>
      <th>Ratings<br />Dec 2012</th>
      <th>Delta<br />Dec 2011</th>
    </tr>
    </thead>
  2. Apply the appropriate class names to the last two columns in each table row within the <tbody>, as shown in the following code snippet:

    <tbody>
    <tr>
      <td>1</td>
      <td>2</td>
      <td>C</td>
      <td class="ratings">18.696%</td>
      <td class="delta">+1.64%</td>
    </tr>
  3. With the table in place and properly marked up with the appropriate class names, write the script to apply the highlights as follows:

    <script type="text/javascript">
    $( document ).ready( function() {
      $( "#tiobeTable tbody tr td.ratings" ).each( function( index ) {
        if ( parseFloat( $( this ).text() ) > 10 ) {
          $( this ).addClass( "highlight" );
        }
      });
      
      $( "#tiobeTable tbody tr td.delta" ).each( function( index ) {
        if ( parseFloat( $( this ).text() ) < 0 ) {
          $( this ).addClass( "highlight-negative" );
        }
      });
    });
    </script>
  4. Now, you will see a much more interesting table with multiple visual cues:

How it works...

The selector for the Ratings column is almost identical to the selector used in the Sum columns (Must know) recipe. Select the <td> elements within the tbody tag's table rows that have a class of ratings. As with the code in the Sum columns (Must know) recipe, use jQuery's each() function to iterate over the collection.

For each iteration of the loop, test whether or not the value (text) of the <td> is greater than 10. Because the values in <td> contain non-numeric characters (in this case, % signs), we use JavaScript's parseFloat() to convert the text to actual numbers:

parseFloat( $( this ).text() )

Much of that should be review. $( this ) is a reference to the element in question. text() retrieves the text from the element. parseFloat() ensures that the value is numeric so that it can be accurately compared to the value 10.

If the condition is met, use addClass() to apply the highlight class to <td>. Do the same thing for the Delta column. The only difference is in checking to see if the text is less than zero. If it is, apply the class highlight-negative.

The end result makes it much easier to identify specific data within the table.