Search HTML table with JS and jQuery

让人想犯罪 __ 提交于 2019-11-28 18:29:35

I have put the part of your code that matters and wrote a working fiddle

http://jsfiddle.net/9hGym/602/

this is now the search engin:

    var searchText = $(this).val().toLowerCase();
    $.each($("#table tbody tr"), function() {
        if($(this).text().toLowerCase().indexOf(searchText) === -1)
           $(this).hide();
        else
           $(this).show();                
    });

you can also use http://www.datatables.net/ for such things ;)

I found that the above solution was fine in theory (although it didn't work), but I found this to work better:

$('#search-field').on('keyup', function(e) {
    if ('' != this.value) {
        var reg = new RegExp(this.value, 'i'); // case-insesitive

        $('.table tbody').find('tr').each(function() {
            var $me = $(this);
            if (!$me.children('td:first').text().match(reg)) {
                $me.hide();
            } else {
                $me.show();
            }
        });
    } else {
        $('.table tbody').find('tr').show();
    }
});

If you want to search more than one column then just change:

if (!$me.children('td:first').text().match(reg)) {

To:

if (!$me.children('td').text().match(reg)) {

The ways posted were a little slow for my table. I came up with a different solution that seems to be much faster.

If you want to search through every cell you can add an attribute to the cell (I used data-name), ex:<td data-name="john smith">John Smith</td>. Then you can use this javascript code:

$("#search").keyup(function() {
  var val = this.value.trim().toLowerCase();
  if ('' != val) {
    var split = val.split(/\s+/);
    var selector = 'td';
    for(var i=0;i<split.length;i++){
      selector = selector+'[data-name*='+split[i]+']';
    }
    $('tr').hide();
    $(selector).closest('tr').show();
  } else {
    $('tr').show();
  }
});

If you just want to search a rows for a single attribute you can just add the attribute to the row like <tr data-name="john smith"><td>John Smith</td><td>...</td></tr> and use the following:

$("#search").keyup(function() {
  var val = this.value.trim().toLowerCase();
  if ('' != val) {
    var split = val.split(/\s+/);
    var selector = 'tr';
    for(var i=0;i<split.length;i++){
      selector = selector+'[data-name*='+split[i]+']';
    }
    $('tr').hide();
    $(selector).show();
  } else {
    $('tr').show();
  }
});

Hope that helps!

You can use this code. It is working excellent.

$('#search').keydown(function () {
      var searchitem = $('#search').val();
      if (searchitem == '' || searchitem == null || searchitem == undefined)           {
          $('#table tbody tr').show();
      }
      else {
          searchitem = searchitem.toUpperCase();
          $('#table tbody tr').hide();
          $('#table tbody tr').each(function () {
              if ($(this).text().indexOf(searchitem) > -1) {
                  $(this).show();
              }
          });
      }
  });
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!