0

I have an HTML table that looks like below:

The actual table is really long, and I'd like to be able to search through the entries, and return just the matching results.

I'm currently using the following code for the search (from here):

var $rows = $('#table tr');
$('#search').keyup(function() {

    var val = '^(?=.*\\b' + $.trim($(this).val()).split(/\s+/).join('\\b)(?=.*\\b') + ').*$',
        reg = RegExp(val, 'i'),
        text;

    $rows.show().filter(function() {
        text = $(this).text().replace(/\s+/g, ' ');
        return !reg.test(text);
    }).hide();
});

But there's a problem. When I search for Foo Bar, I want it to return:

Right now, it returns the following:

How can I fix this? Can this be done by adjusting the regex? I'm out of ideas.

Live Demo

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Joseph John
  • 510
  • 1
  • 5
  • 21

2 Answers2

4

Organize your table into multiple tbody groups (Yes, it's valid) and search within them.

var $rows = $('#table tbody');
$('#search').keyup(function () {

    var val = '^(?=.*\\b' + $.trim($(this).val()).split(/\s+/).join('\\b)(?=.*\\b') + ').*$',
        reg = RegExp(val, 'i'),
        text;

    $rows.show().filter(function () {
        text = $(this).text().replace(/\s+/g, ' ');
        return !reg.test(text);
    }).hide();
});
body {
    padding: 20px;
}
input {
    margin-bottom: 5px;
    padding: 2px 3px;
    width: 209px;
}
td {
    padding: 4px;
    border: 1px #CCC solid;
    width: 100px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<input type="text" id="search" placeholder="Type to search">
<table id="table" style="width: 100%" cellpadding="10">
    <tbody>
        <tr>
            <td rowspan="3">Foo Bar</td>
            <td>Roll No:</td>
            <td>something</td>
        </tr>
        <tr>
            <td>something</td>
            <td>stuff</td>
        </tr>
        <tr>
            <td>something</td>
            <td>stuff</td>
        </tr>
    </tbody>
    <tbody>
        <tr>
            <td rowspan="3">Baz Bak</td>
            <td>Roll No:</td>
            <td>something</td>
        </tr>
        <tr>
            <td>something</td>
            <td>stuff</td>
        </tr>
        <tr>
            <td>something</td>
            <td>stuff</td>
        </tr>
    </tbody>
    <tbody>
        <tr>
            <td rowspan="3">Baz Bar</td>
            <td>Roll No:</td>
            <td>something</td>
        </tr>
        <tr>
            <td>something</td>
            <td>stuff</td>
        </tr>
        <tr>
            <td>something</td>
            <td>stuff</td>
        </tr>
    </tbody>
</table>

(If you prefer an updated version of your Fiddle: http://jsfiddle.net/9y89dcsp/1/)

Community
  • 1
  • 1
Mooseman
  • 18,763
  • 14
  • 70
  • 93
0

This is happening because you actually have a single row with Foo Bar in it. The table appears fine because rowspan takes care of it in the UI. But in DOM , only one row contains the "Foo Bar".

So what you can do is change your algorithm to search. You can call a function for every tr. Inside that function,

  1. check your string
  2. if it matches with the search string, store that row in a dummy table
  3. go to next iteration
  4. check if no of <td>s are 2 or less. If yes, append it in the dummy table
  5. if the no of <td>s are 3, meaning you have reached next legitimate line. Stop this operation and display the dummy table in UI
DAB
  • 93
  • 1
  • 7
KaustubhSV
  • 328
  • 4
  • 15