Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / HTML

JavaScript Filter / Grouping - Similar to Excel

4.50/5 (2 votes)
13 Mar 2015CPOL1 min read 17.8K   307  
Using this script, you will automatically add a filtering dropdownlist similar to Excel file filtering at the top, which will filter table based on your selection

Image 1

Introduction

Whenever you have a table displaying average amount of rows ~100, using this script, you can easily add a dropdownlist filter on the top of the column, where you can filter the whole table using it, and it will give you something similar to what you have in Excel.

Background

A jQuery background would be ideal to understand the code, but a basic knowledge in JavaScript is enough. Some simple steps are required.

Using the Code

Using this code is simple, first you need a reference for both SJFIlter.js and Jquery.js.

HTML
<script type="text/javascript" src="SJFilter.js"></script>
<script type="text/javascript" src="jquery-1.9.1.js"></script>

First, we need to mark the table that we going to apply the filter on, we just need to give it any id:

HTML
<table id="Table2bFiltered" cellpadding="5" cellspacing="0">

As this will be filtering the rows based on the values that exists on a specific column, we need to determince which column is that which simply can be done by adding the following CSS classes on the column that we going to filter in the header row as below:

HTML
<td class="SJFilter GradeFilter">Grade</td>

SJFilter class is reserved class name, which will tell this script that this column will be used as a filter, while GradeFilter is just a name that we will use in the next step.

On every td that contains Grade values, e.g., First Grade, Second Grade, ... we add the same name defined before e.g.

HTML
<td class="GradeFilter">Second Grade</td>
HTML
<td class="GradeFilter">First Grade</td>
HTML
<td class="GradeFilter">Third Grade</td>

Now by adding the initialize line passing the table ID and an optional CSS class to give a style for alternative row color...

HTML
<script type="text/javascript">initFiltering('Table2bFiltered', 'gray');</script>

...the code now gets the class name from the header row, and splits it to get the defined name GradeFilter loops on and fill a dropdownlist with distinct values of the content of this column.

Below is the full sample:

HTML
//
// Any source code blocks look like this
//
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
<!-- Javascript references -->
     <script type="text/javascript" src="SJFilter.js"></script>
    <script type="text/javascript" src="jquery-1.9.1.js"></script>
       <style type="text/css">
        .gray, .gray td
        {
            background-color: #eee !important;
        }
        body{font-family:Tahoma;}
           </style>
</head>
<body>
<-- ID on the table -->
    <table id="Table2bFiltered" 
    cellpadding="5" cellspacing="0">
        <tr class="gray">
            <td>Student name</td>
<-- add reserved class name and custom definde name -->
            <td class="SJFilter GradeFilter">Grade</td>
            <td>Age</td>
            <td>Total Scrore</td>
        </tr>
        <tr>
            <td>John</td>
<-- class on data td -->
            <td class="GradeFilter">Second Grade</td>
            <td>7</td>
            <td>90</td>
        </tr>
        <tr class="gray">
            <td>John</td>
<-- class on data td as defined before -->
            <td class="GradeFilter">First Grade</td>
            <td>6</td>
            <td>90</td>
        </tr>
        <tr>
            <td>Mary</td>
<-- class on data td as defined before -->
            <td class="GradeFilter">First Grade</td>
            <td>6</td>
            <td>90</td>
        </tr>
        <tr class="gray">
            <td>Su</td>
<-- class on data td as defined before -->
            <td class="GradeFilter">First Grade</td>
            <td>6</td>
            <td>90</td>
        </tr>
        <tr>
            <td>Mike</td>
<-- class on data td as defined before -->
            <td class="GradeFilter">Third Grade</td>
            <td>8</td>
            <td>90</td>
        </tr>
    </table>
    
<-- initialize script-->
    <script type="text/javascript">initFiltering
    ('Table2bFiltered', 'gray');</script>
</body>
</html>

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)