Filtering Table rows using Jquery

前端 未结 9 1273
悲&欢浪女
悲&欢浪女 2020-11-30 19:19

I found a Jquery script that does the table filtering stuff based on input field. This script basically takes the filter, splits each word and filters table rows with each w

相关标签:
9条回答
  • 2020-11-30 19:51

    i have a very simple function:

    function busca(busca){
        $("#listagem tr:not(contains('"+busca+"'))").css("display", "none");
        $("#listagem tr:contains('"+busca+"')").css("display", "");
    }
    
    0 讨论(0)
  • 2020-11-30 19:53

    I used Beetroot-Betroot's solution, but instead of using contains, I used containsNC, which makes it case insensitive.

    $.extend($.expr[":"], {
    "containsNC": function(elem, i, match, array) {
    return (elem.textContent || elem.innerText ||
    "").toLowerCase().indexOf((match[3] || "").toLowerCase()) >= 0;
    }
    });
    
    0 讨论(0)
  • 2020-11-30 19:55

    based on @CanalDoMestre's answer. I added support for the blank filter case, fixed a typo and prevented hiding the rows so I can still see the column headers.

        $("#filterby").on('keyup', function() {
            if (this.value.length < 1) {
                $("#list tr").css("display", "");
            } else {
                $("#list tbody tr:not(:contains('"+this.value+"'))").css("display", "none");
                $("#list tbody tr:contains('"+this.value+"')").css("display", "");
            }
        });
    
    0 讨论(0)
  • 2020-11-30 19:58

    Adding one more approach :

    value = $(this).val().toLowerCase();
    $("#product-search-result tr").filter(function () {
    $(this).toggle($(this).text().toLowerCase().indexOf(value) > -1)
    });
    
    0 讨论(0)
  • 2020-11-30 19:59

    I chose @nrodic's answer (thanks, by the way), but it has several drawbacks:

    1) If you have rows containing "cat", "dog", "mouse", "cat dog", "cat dog mouse" (each on separate row), then when you search explicitly for "cat dog mouse", you'll be displayed "cat", "dog", "mouse", "cat dog", "cat dog mouse" rows.

    2) .toLowerCase() was not implemented, that is, when you enter lower case string, rows with matching upper case text will not be showed.

    So I came up with a fork of @nrodic's code, where

    var data = this.value; //plain text, not an array
    

    and

    jo.filter(function (i, v) {
        var $t = $(this);
        var stringsFromRowNodes = $t.children("td:nth-child(n)")
        .text().toLowerCase();
        var searchText = data.toLowerCase();
        if (stringsFromRowNodes.contains(searchText)) {
            return true;
            }
        return false;
    })
    //show the rows that match.
    .show();
    

    Here goes the full code: http://jsfiddle.net/jumasheff/081qyf3s/

    0 讨论(0)
  • 2020-11-30 20:10

    Have a look at this jsfiddle.

    The idea is to filter rows with function which will loop through words.

    jo.filter(function (i, v) {
        var $t = $(this);
        for (var d = 0; d < data.length; ++d) {
            if ($t.is(":contains('" + data[d] + "')")) {
                return true;
            }
        }
        return false;
    })
    //show the rows that match.
    .show();
    

    EDIT: Note that case insensitive filtering cannot be achieved using :contains() selector but luckily there's text() function so filter string should be uppercased and condition changed to if ($t.text().toUpperCase().indexOf(data[d]) > -1). Look at this jsfiddle.

    0 讨论(0)
提交回复
热议问题