Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying Instant jQuery 2.0 Table Manipulation How-to
  • Table Of Contents Toc
Instant jQuery 2.0 Table Manipulation How-to

Instant jQuery 2.0 Table Manipulation How-to

By : Charlie Griefer
4.3 (7)
close
close
Instant jQuery 2.0 Table Manipulation How-to

Instant jQuery 2.0 Table Manipulation How-to

4.3 (7)
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)
close
close

Show/hide rows (Must know)


Click a link to trigger hiding or displaying of table rows.

Getting ready

Once again, start off with an HTML table. This one is not quite as simple a table as in previous recipes. You'll need to create a few <td> tags that span the entire table, as well as provide some specific classes to certain elements.

How to do it...

  1. Again, give the table an id attribute. Each of the rows that represent a department, specifically the rows that span the entire table, should have a class attribute value of dept.

    <table border="1" id="employeeTable">
    <thead>
      <tr>
        <th>Last Name</th>
        <th>First Name</th>
        <th>Phone</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td colspan="3" class="dept">
        </td>
      </tr>
  2. Each of the department names should be links where the <a> elements have a class of rowToggler.

    <a href="#" class="rowToggler">Accounting</a>
  3. Each table row that contains employee data should have a class attribute value that corresponds to its department.

    Note

    Note that class names cannot contain spaces. So in the case of the Information Technology department, the class names should be InformationTechnology without a space. The issue of the space will be addressed later.

    <tr class="Accounting">
      <td>Frang</td>
      <td>Corey</td>
      <td>555-1111</td>
    </tr>
  4. The following script makes use of the class names to create a table whose rows can be easily hidden or shown by clicking a link:

    <script type="text/javascript">
    $( document ).ready( function() {
      $( "a.rowToggler" ).click( function( e ) {
        e.preventDefault();
        var dept = $( this ).text().replace( /\s/g, "" );
        $( "tr[class=" + dept + "]" ).toggle();
      })
    });
    </script>
  5. With the jQuery implemented, departments are "collapsed", and will only reveal the employees when the link is clicked.

How it works...

The jQuery will "listen" for a click event on any <a> element that has a class of rowToggler. In this case, capture a reference to the event that triggered the action by passing e to the click handler function.

$( "a.rowToggler" ).click( function( e )

In this case, e is simply a variable name. It can be any valid variable name, but e is a standard convention. The important thing is that jQuery has a reference to the event. Why? Because in this case, the event was that an <a> was clicked. The browser's default behavior is to follow a link. This default behavior needs to be prevented.

As luck would have it, jQuery has a built-in function called preventDefault(). The first line of the function makes use of this by way of the following:

e.preventDefault();

Now that you've safely prevented the browser from leaving or reloading the page, set a variable with a value that corresponds to the name of the department that was just clicked.

var dept = $( this ).text().replace( /\s/g, "" );

Most of the preceding line should look familiar. $( this ) is a reference to the element that was clicked, and text() is something you've already used. You're getting the text of the <a> tag that was clicked. This will be the name of the department.

But there's one small issue. If the department name contains a space, such as "Information Technology", then this space needs to be removed.

.replace( /\s/g, "" )

replace() is a standard JavaScript function that uses a regular expression to replace spaces with an empty string. This turns "Information Technology" into "InformationTechnology", which is a valid class name.

The final step is to either show or hide any table row with a class that matches the department name that was clicked.

Ordinarily, the selector would look similar to the following:

$( "tr.InformationTechnology" )

Because the class name is a variable value, an alternate syntax is necessary.

jQuery provides a way to select an element using any attribute name and value. The selector above can also be represented as follows:

$( "tr[class=InformationTechnology]" )

The entire selector is a literal string, as indicated by the fact that it's enclosed in quotes. But the department name is stored in a variable. So concatenate the literal string with the variable value:

$( "tr[class=" + dept + "]" )

With the desired elements selected, either hide them if they're displayed, or display them if they're hidden. jQuery makes this very easy with its built-in toggle() method.

CONTINUE READING
83
Tech Concepts
36
Programming languages
73
Tech Tools
Icon Unlimited access to the largest independent learning library in tech of over 8,000 expert-authored tech books and videos.
Icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Icon 50+ new titles added per month and exclusive early access to books as they are being written.
Instant jQuery 2.0 Table Manipulation How-to
notes
bookmark Notes and Bookmarks search Search in title playlist Add to playlist font-size Font size

Change the font size

margin-width Margin width

Change margin width

day-mode Day/Sepia/Night Modes

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY

Submit Your Feedback

Modal Close icon
Modal Close icon
Modal Close icon