Bootstrap table filter - how to add "No matching records" line if there are no results?

I am using the following piece of code http://jsfiddle.net/giorgitbs/52aK9/1/ :

$(document).ready(function () {

(function ($) {

    $('#filter').keyup(function () {

       var rex = new RegExp($(this).val(), 'i');
       $('.searchable tr').hide();
       $('.searchable tr').filter(function () {
          return rex.test($(this).text());
       }).show();

  })

}(jQuery));
});

      

This works great. However, how can I display a row containing the text "No matching records" when there are no results instead of an empty table?

+3


source to share


2 answers


I would do something like this. First, I add a row (hidden by default) to the table to show the data. Then in jQuery check the number of rows visible. If it is 0, show the hidden row.



$(document).ready(function () {

    (function ($) {

        $('#filter').keyup(function () {
            var rex = new RegExp($(this).val(), 'i');
            $('.searchable tr').hide();
            $('.searchable tr').filter(function () {
                return rex.test($(this).text());
            }).show();
            $('.no-data').hide();
            if($('.searchable tr:visible').length == 0)
            {
                $('.no-data').show();
            }

        })

    }(jQuery));

});
      

.no-data {
    display: none;
    text-align: center;
}
      

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="input-group"> <span class="input-group-addon">Filter</span>

    <input id="filter" type="text" class="form-control" placeholder="Type here...">
</div>
<table class="table table-striped">
    <thead>
        <tr>
            <th>Code</th>
            <th>Name</th>
            <th>Default</th>
            <th>Status</th>
        </tr>
    </thead>
    <tbody class="searchable">
        <tr class="no-data">
            <td colspan="4">No data</td>
        </tr>
        <tr>
            <td>EUR</td>
            <td>EURO</td>
            <td></td>
            <td>Active</td>
        </tr>
        <tr>
            <td>GBP</td>
            <td>Pound</td>
            <td></td>
            <td>Active</td>
        </tr>
        <tr>
            <td>GEL</td>
            <td>Georgian Lari</td>
            <td><span class="glyphicon glyphicon-ok"></span>
            </td>
            <td>Active</td>
        </tr>
        <tr>
            <td>USD</td>
            <td>US Dollar</td>
            <td></td>
            <td>Active</td>
        </tr>
    </tbody>
</table>
      

Run codeHide result


+3


source


You can add the number of records found using the below code in the bootstrap:

<tr class="no-data">
    <td colspan="14">No data available in table</td>
</tr>

      



This "14"

indicates the number of columns present in the header row.

0


source







All Articles