代码之家  ›  专栏  ›  技术社区  ›  Jimbo

jQuery表按列筛选行

  •  2
  • Jimbo  · 技术社区  · 15 年前

    我的表中有5列。在每个表的顶部有一个下拉列表或文本框,用户可以用它过滤表数据(基本上隐藏不适用的行)

    jQuery有很多表过滤插件,但没有一个是这样工作的,这是一个复杂的部分:|

    4 回复  |  直到 8 年前
        1
  •  12
  •   Jeff Treuting Charlie Martin    9 年前

    http://jsfiddle.net/urf6P/3/

    它使用jquery选择器:contains('some text')和:not(:contains('some text'))来决定是显示还是隐藏每一行。这可能会让你朝着一个方向前进。

    编辑以包含来自jsfiddle的HTML和javascript:

    $(function() {    
        $('#filter1').change(function() { 
            $("#table td.col1:contains('" + $(this).val() + "')").parent().show();
            $("#table td.col1:not(:contains('" + $(this).val() + "'))").parent().hide();
        });
    
    });
    
        2
  •  2
  •   Fr0zenFyr    7 年前

    accepted solution 过帐人 杰夫·特鲁廷 a solution posted on a different SO post Highway of Life .

    // Define a custom selector icontains instead of overriding the existing expression contains
    // A global js asset file will be a good place to put this code
    $.expr[':'].icontains = function(a, i, m) {
      return $(a).text().toUpperCase()
          .indexOf(m[3].toUpperCase()) >= 0;
    };
    
    // Now perform the filtering as suggested by @jeff
    $(function() {    
        $('#filter1').on('keyup', function() {  // changed 'change' event to 'keyup'. Add a delay if you prefer
            $("#table td.col1:icontains('" + $(this).val() + "')").parent().show();  // Use our new selector icontains
            $("#table td.col1:not(:icontains('" + $(this).val() + "'))").parent().hide();  // Use our new selector icontains
        });
    
    });
    
        3
  •  1
  •   Nate Pinchot    15 年前

    这可能不是最好的方法,我也不确定性能如何,但一个选项是用一个id标记每一列(每一行中),id以列标识符开始,然后是一个唯一的数字,如记录标识符。

    例如,如果您有一个列PRODUCT Name,并且记录ID为763,我将执行以下操作:

    ​​<table id="table1">
        <thead>
            <tr>
                <th>Artist</th>
                <th>Album</th>
                <th>Genre</th>
                <th>Price</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td id="artist-127">Red Hot Chili Peppers</td>
                <td id="album-195">Californication</td>
                <td id="genre-1">Rock</td>
                <td id="price-195">$8.99</td>
            </tr>
            <tr>
                <td id="artist-59">Santana</td>
                <td id="album-198">Santana Live</td>
                <td id="genre-1">Rock</td>
                <td id="price-198">$8.99</td>
            </tr>
            <tr>
                <td id="artist-120">Pink Floyd</td>
                <td id="album-183">Dark Side Of The Moon</td>
                <td id="genre-1">Rock</td>
                <td id="price-183">$8.99</td>
            </tr>
        </tbody>
    </table>
    

    id .

    例如,如果要按艺术家列筛选:

    var regex = /Hot/;
    $('#table1').find('tbody').find('[id^=artist]').each(function() {
        if (!regex.test(this.innerHTML)) {
            this.parentNode.style.backgroundColor = '#ff0000';
        }
    });
    
        4
  •  0
  •   solanki...    9 年前

    <input type="text" id="myInput" onkeyup="myFunction()" placeholder="Search for names..">
    
    <table id="myTable">
      <tr class="header">
        <th style="width:60%;">Name</th>
        <th style="width:40%;">Country</th>
      </tr>
      <tr>
        <td>Alfreds Futterkiste</td>
        <td>Germany</td>
      </tr>
      <tr>
        <td>Berglunds snabbkop</td>
        <td>Sweden</td>
      </tr>
      <tr>
        <td>Island Trading</td>
        <td>UK</td>
      </tr>
      <tr>
        <td>Koniglich Essen</td>
        <td>Germany</td>
      </tr>
    </table>
    

    step:2 在.js文件中编写以下内容

    function myFunction() {
      // Declare variables 
      var input, filter, table, tr, td, i;
      input = document.getElementById("myInput");
      filter = input.value.toUpperCase();
      table = document.getElementById("myTable");
      tr = table.getElementsByTagName("tr");
    
      // Loop through all table rows, and hide those who don't match the search query
      for (i = 0; i < tr.length; i++) {
        td = tr[i].getElementsByTagName("td")[0];
        if (td) {
          if (td.innerHTML.toUpperCase().indexOf(filter) > -1) {
            tr[i].style.display = "";
          } else {
            tr[i].style.display = "none";
          }
        } 
      }
    }