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)

Plugins (Should know)


I know what you've been thinking all this time. "Charlie, why do I need to know how to do these things when there are so many plugins available for jQuery?" That's a very good question.

There are numerous plugins available for jQuery, and specifically for table manipulation. Sometimes the plugins are overkill, though. Maybe a particular plugin doesn't work exactly the way you'd like it to. Perhaps your employer is shy about using third-party code (except, of course, for jQuery itself).

Plugins are great, and they are a significant part of the reason that jQuery is so popular. It's always good to know how to do something on your own, though. Of course once you know how, it never hurts to work smarter, and not harder.

There are two table-related plugins that I generally use when the need arises: tablesorter (http://tablesorter.com/docs/) and DataTables (http://www.datatables.net/).

Getting ready

For both tablesorter and DataTables, download the plugins to your server and call them from your pages. You still need to include jQuery, as the plugins assume you have jQuery set up properly.

After downloading the .js files from the URLs listed above, add appropriate link's paths to your page's <head> section. For example:

<script type="text/javascript" src="/path/to/jquery.tablesorter.min.js"></script>
<script type="text/javascript" src="/path/to/jquery.datatables.min.js"></script>

Some plugins also offer .css files, which will be needed to achieve a particular look and feel. We'll forego the aesthetics and dive right into functional implementation.

How to do it...

  1. Both tablesorter and DataTables can be applied to any well-marked up HTML table. As per usual, assume a table with an ID of movies.

  2. To apply tablesorter to the table, add the following script to the bottom of the page:

    <script type="text/javascript">
      $( document ).ready( function() {
        $( "#movies" ).tablesorter();
      });
    </script>
  3. To apply DataTables to the table, add the following script to the bottom of the page:

    <script type="text/javascript">
      $( document ).ready( function() {
        $( "#movies" ).dataTable();
      });
    </script>
  4. It may be worth pointing out that you would apply one plugin or the other to your #movies table, but not both!

How it works...

For basic functionality, there's very little magic. Simply include the scripts as shown and the rest just works. That's the beauty of the extensible jQuery plugin architecture.

You might not notice any significant changes to the appearance of your table with either plugin. Although with DataTables you'll see some extra goodies around the table, such as a dropdown to display "n" records, a filter form field, and Previous/Next links for pagination. See the following screenshot for reference:

Both tablesorter and DataTables include CSS files with their downloads. Include them into the page in much the same way that you included the .js files, and you will have tables that are not only functional but also aesthetically pleasing.

Add the following line to your tablesorter page:

<link rel="stylesheet" href="tablesorter.css" type="text/css" media="print, projection, screen" />

The preceding code yields the following table:

Likewise, add the following line of code to your DataTables page:

<link rel="stylesheet" href="dataTables.css" type="text/css" media="print, projection, screen" />

The preceding code yields the following table:

Both downloads include additional stylesheets as well as images, such as up/down arrows for column headers to indicate sorting. Additionally, you can tweak the styles as you see fit.

As impressive as the out-of-the-box functionality may be, both plugins also offer a number of options.

Tablesorter shows a number of examples at http://tablesorter.com/docs/#Examples. They include forcing a default sort order, appending table data with AJAX, and sorting fields that contain markup (for example, ignoring the markup and sorting on the text).

DataTables examples are located at http://www.datatables.net/examples/. While by default both tablesorter and DataTables can be utilized in a single line of code, and while they both have various options, DataTables is insanely configurable.

No disrespect to tablesorter. It essentially does one thing, and it does it well. If table sorting is all that you need, DataTables may be overkill. But for a Swiss army knife of table-related functionality, DataTables is impressive.