HTML Table multi column filtering

Val

I am working on developing a multi-column multi-word filtering search and have difficulties with two problems:

  1. I am only able to partially filter the first column; partial because typing apple it shows apple and oranges in the first column.
  2. I am struggling with finding a way to make this a multi-word search over both columns.

I found a table filtering techniques on SO and adjusted them in this fiddle. But this code will only filter over all the columns with one word. Typing more than one word to narrow the result (find A and B) does not work.

The code below is my attempt to perform a multi-column multi-word search in a single box see my fiddle.

How can I achieve filtering over multiple columns that combines the search phrases?

Meaning show only rows where searchphrase-1 and searchphrase-2 are present.

JS

var $orig_rows1 = $('#table tbody tr td[class = "col1"]');
var $orig_rows2 = $('#table tbody tr td[class = "col2"]');
$('#search').keyup(function() {
   var $rows1 = $orig_rows1;
   var $rows2 = $orig_rows2;
   var val = '^(?=.*\\b' + $.trim($(this).val()).split(/\s+/).join('\\b)(?=.*\\b') + ').*$',
        reg = RegExp(val, 'i'),
        text;
    $("tr:hidden").show();
    $rows1.show().filter(function() {
        text = $(this).text().replace(/\s+/g, ' ');
        return !reg.test(text);
    }).parent("tr").hide();
});

HTML

<input type="text" id="search" placeholder="Type to search title 1">    
<table class="table-striped" id="table">
     <thead>
     <th>Title 1</th>
     <th>Title 2</th>
    </thead>
    <tbody>    
   <tr>
      <td class="col1">Apple</td>
      <td class="col2">Green</td>
   </tr>
   <tr>
      <td class="col1">Grapes</td>
      <td class="col2">Green</td>
   </tr>
   </tbody>    
   <tr>
      <td>Orange</td>
      <td>Orange</td>
   </tr>
</table>

Any advice would be great.

Bhavik

Mistake in your code </tbody> is placed wrong, it must be

<tbody>    
   <tr>
      <td class="col1">Apple</td>
      <td class="col2">Green</td>
   </tr>
   <tr>
      <td class="col1">Grapes</td>
      <td class="col2">Green</td>
   </tr>    
   <tr>
      <td>Orange</td>
      <td>Orange</td>
   </tr>
</tbody>

Not a perfect solution, suggestions would be greatly appreciated

Try this Demo Fiddle it has multiple words search capability made using Tag-it! plugin.


Update search for both AND and OR

Demo Fiddle with a check box to perform both operations

Edit: pieces of linked fiddle added

This is a part of the code from the linked fiddle

var search = function () {
    if ($('.tagit-label').length) {

        $("#table tbody tr").hide();
        var toShow = [];

        $('.tagit-label').each(function () {
            filter = $(this).text();
            $("#table tbody tr").each(function () {
                if ($(this).text().search(new RegExp(filter, "i")) > 0) {
                    toShow.push($("#table tbody tr").index(this));
                }
            });
        });

        if ($('#chkBoth').prop("checked") && $('.tagit-label').length > 1) {
            var filterShow = [];
            var outputArray = [];
            $(toShow).each(function (i, value) {
                if (($.inArray(value, outputArray)) == -1) {
                    outputArray.push(value);
                }
            });
            $(outputArray).each(function (i, value) {
                var index = toShow.indexOf(value);
                toShow.splice(index, 1);
            });
        }

        $(toShow).each(function (i, value) {
            $("#table tbody tr").eq(value).fadeIn();
        });

    } else {
        $("#table tbody tr").fadeIn();
    }
}

This is the result of my linked fiddle below:

Result of multi column filter

Hope it helps..!!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

HTML Table --> Selecting 1 Column to Filter in a Multi-Column Table

From Dev

HTML Table Filtering/Selection

From Dev

Filtering HTML table rows in Jquery

From Dev

Should I use multi column or multi table?

From Dev

Filtering data table based on condition in Column

From Dev

javascript - Slider filtering table numeric column

From Dev

Sum column in a multi table hierarchy

From Dev

disable kendo-ui grid multi column filtering

From Dev

Isotope.js filtering for html table

From Dev

Parse HTML table by column

From Dev

HTML table half column

From Dev

html table column sizing

From Dev

HTML Table Column Placement

From Dev

html table column size

From Dev

Simple Multi Column Table Using Xamarin

From Dev

How to rollapply over a multi column data table

From Dev

How to rollapply over a multi column data table

From Dev

Querying a Multi column Index Table in MySQL

From Dev

data.table column/data filtering execution order in R?

From Dev

Filtering rows in data.table while adding a column

From Dev

Table prints multiple column headers while filtering results

From Dev

Filtering on text column: full-text or separate table

From Dev

Filtering rows in data.table while adding a column

From Dev

Filtering duplicate column values from a custom table repeater in Kentico 10

From Dev

Is it possible to create multi column layout with paging in html?

From Dev

Multi-Dimensional Filtering

From Dev

Filtering list with multi criterias

From Dev

Bootstrap + Responsive: Change multi column table to single column for mobile? (with images)

From Dev

Subset data.table by the third column of a multi-column key

Related Related

HotTag

Archive