选择一列的值后筛选HTML表

时间:2017-09-19 19:43:25

标签: javascript jquery html filter

我在为第一列选择值后尝试过滤HTML表格行。它可以工作,但它也会搜索第二列中的值:



$('.platform_filter').keyup(function(){
 var val=$(this).val();    
        $('table tbody tr').hide();
         var trs=$('table tbody tr').filter(function(d){
         return $(this).text().toLowerCase().indexOf(val)!=-1;
         });
         console.log(trs);
         trs.show();   
});

@import url('https://fonts.googleapis.com/css?family=Roboto');

body { 
    margin: 0;
    color:#fff;
    font-family: Roboto; }
.row {
    display: table;
    width: 100%;
    height: 241px; 
    background-color:#454545;
}
.row > .col-lg-6 {
    display: table-cell;
    vertical-align: middle;
}
.container {
  /*display: flex;*/
  flex-wrap: wrap;
}
.container > div {
  padding: 15px;
  margin: 5px;
  flex: 0 0 calc(100% - 20px);
  text-align: left;
}

/*img {
    padding-left: 7%;
    max-height:55px;
    width:auto;
}*/
td{
  padding: 2px 2px;
  text-align: center;
  margin: 6px 0;
  border: none;
}
table{
  width: 100%;
  background-color:#454545;
  font-weight:500;
  border-collapse: separate;
  border-spacing:0.3em 1.1em;
  color: #fff;
  border: 0;
}
tr{
    font-size: 1.5em;
    text-transform:capitalize;
}
th {
    color: #CCC;
    font-size: 0.8em;
 	}
	
#one,#two,#three,#four{
    padding-top:2%;
 	}
#platform {
    background-image: url('/css/searchicon.png'); /* Add a search icon to input */
    background-position: 10px 12px; /* Position the search icon */
    background-repeat: no-repeat; /* Do not repeat the icon image */
    padding: 2px 2px;
    text-align: center;
    margin: 6px 0;
    border: none;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>


<table>
    <thead>
       <tr><input type="text" class="platform_filter"/><th>Num Heading</th></tr>
        </thead>
    <tbody>
       <tr><td>ABC</td><td>1</td></tr>
       <tr><td>DEF</td><td>2</td></tr>
       <tr><td>ABC</td><td>3</td></tr>
       <tr><td>apolo</td><td>4</td></tr>
   </tbody>
</table>
&#13;
&#13;
&#13;

如果我实现了这一点,我想用下拉列表替换输入。我在下面尝试了一些没有成功的事情:

&#13;
&#13;
$('.filter').change(function () {
    var values = [];

     $('.filter').each(function () {
        var colIdx = $(this).data('col');

         $(this).find('option:selected').each(function () {
             if ($(this).val() != "") values.push( {
                text: $(this).text(),
                colId : colIdx
             });
        });
    });
    
    filter('table > tbody > tr > td', values);
});

function filter(selector, values) { 
    $(selector).each(function () {
        var sel = $(this);
        var tokens = sel.text().trim().split('\n');
        var toknesObj = [], i;
        for(i=0;i<tokens.length;i++){
            toknesObj[i] = {
               text:tokens[i].trim(), 
               found:false
            };
        }

        var show = false;
        $.each(values, function (i, val) {                

       if (toknesObj[val.colId].text.search(new RegExp("\\b"+val.text+"\\b")) >= 0) {
     
           toknesObj[val].found = true;
          }

        });          
        console.log(tokens);
        var count = 0;
         $.each(toknesObj, function (i, val) {
             if (val.found){
                 count+=1;
             }
         });
        show = (count === values.length);        
        show ? sel.show() : sel.hide();
    });
};
&#13;
@import url('https://fonts.googleapis.com/css?family=Roboto');

body { 
    margin: 0;
    color:#fff;
    font-family: Roboto; }
.row {
    display: table;
    width: 100%;
    height: 241px; 
    background-color:#454545;
}
.row > .col-lg-6 {
    display: table-cell;
    vertical-align: middle;
}
.container {
  /*display: flex;*/
  flex-wrap: wrap;
}
.container > div {
  padding: 15px;
  margin: 5px;
  flex: 0 0 calc(100% - 20px);
  text-align: left;
}

/*img {
    padding-left: 7%;
    max-height:55px;
    width:auto;
}*/
td{
  padding: 2px 2px;
  text-align: center;
  margin: 6px 0;
  border: none;
}
table{
  width: 100%;
  background-color:#454545;
  font-weight:500;
  border-collapse: separate;
  border-spacing:0.3em 1.1em;
  color: #fff;
  border: 0;
}
tr{
    font-size: 1.5em;
    text-transform:capitalize;
}
th {
    color: #CCC;
    font-size: 0.8em;
 	}
	
#one,#two,#three,#four{
    padding-top:2%;
 	}
#platform {
    background-image: url('/css/searchicon.png'); /* Add a search icon to input */
    background-position: 10px 12px; /* Position the search icon */
    background-repeat: no-repeat; /* Do not repeat the icon image */
    padding: 2px 2px;
    text-align: center;
    margin: 6px 0;
    border: none;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/jquery-1.10.1.js"></script>

<div id="one"><div class="row"><div class="col-lg-6" style="background-color: #e90649; width: 117px;">&nbsp;</div><div class="col-lg-6" style="max-width: 100px; padding-left: 10px; font-size: 2vw;">Objc1<br><br></div><div class="col-lg-6"><div class="container"><select class="filter" data-col="0"><option value="">None</option><option value="a">plat1</option><option value="b">plat2</option></select><table><thead><tr><th>Platform</th><th>Channel</th><th>Objective</th><th>Num1</th><th>Num2</th></tr></thead><tbody><tr><td>plat1</td><td>chan1</td><td>Objc1</td><td>40</td><td>34</td></tr><tr><td>plat2</td><td>chan1</td><td>Objc1</td><td>26</td><td>22</td></tr></tbody></table></div></div></div></div>
&#13;
&#13;
&#13;

该脚本工作正常(已应用过滤器,但仅适用于第一列 - &gt;我希望在装配完第一列中的值后显示整行)。有效的初始脚本是:

   if (toknesObj[val.colId].text.search(new RegExp("\\b"+val.text+"\\b")) >= 0) {

       toknesObj[val.colId].found = true;
      }

我改为:

   if (toknesObj[val.colId].text.search(new RegExp("\\b"+val.text+"\\b")) >= 0) {

       toknesObj[val].found = true;
      }

2 个答案:

答案 0 :(得分:1)

如果你想显示每个tr文本是否与tr first td text或second td text匹配,那么你可以查看这段代码。

$('.platform_filter').keyup(function(){
      var val=$(this).val();    
        $('table tbody tr').hide();
         var trs=$('table tbody tr').filter(function(index,d){

         return $(d).find("td:first").text().toLowerCase().indexOf(val)!=-1 || $(d).find("td:last").text().toLowerCase().indexOf(val)!=-1;
         });
         console.log(trs);
         trs.show();   
});

答案 1 :(得分:1)

我建议改变这一行:

filter('table > tbody > tr > td', values);

为:

filter('table > tbody > tr', values);

因此,您的过滤器功能可以简化为:

function filter(selector, values) {
    $(selector).each(function () {
        var sel = $(this);
        var txt = sel.find('td:eq(0)').text().trim();
        var hwMatches = values.filter(function(ele, idx) {
            return ele.text == txt;
        }).length;
        sel.toggle(hwMatches > 0 || values.length == 0);
    });
};

$('.filter').change(function () {
    var values = [];

    $('.filter').each(function () {
        var colIdx = $(this).data('col');

        $(this).find('option:selected').each(function () {
            if ($(this).val() != "") values.push({
                text: $(this).text(),
                colId: colIdx
            });
        });
    });

    filter('table > tbody > tr', values);
});

function filter(selector, values) {
    $(selector).each(function () {
        var sel = $(this);
        var txt = sel.find('td:eq(0)').text().trim();
        var hwMatches = values.filter(function(ele, idx) {
            return ele.text == txt;
        }).length;
        sel.toggle(hwMatches > 0 || values.length == 0);
    });
};
@import url('https://fonts.googleapis.com/css?family=Roboto');

body {
    margin: 0;
    color: #fff;
    font-family: Roboto;
}

.row {
    display: table;
    width: 100%;
    height: 241px;
    background-color: #454545;
}

.row > .col-lg-6 {
    display: table-cell;
    vertical-align: middle;
}

.container {
    /*display: flex;*/
    flex-wrap: wrap;
}

.container > div {
    padding: 15px;
    margin: 5px;
    flex: 0 0 calc(100% - 20px);
    text-align: left;
}

/*img {
    padding-left: 7%;
    max-height:55px;
    width:auto;
}*/
td {
    padding: 2px 2px;
    text-align: center;
    margin: 6px 0;
    border: none;
}

table {
    width: 100%;
    background-color: #454545;
    font-weight: 500;
    border-collapse: separate;
    border-spacing: 0.3em 1.1em;
    color: #fff;
    border: 0;
}

tr {
    font-size: 1.5em;
    text-transform: capitalize;
}

th {
    color: #CCC;
    font-size: 0.8em;
}

#one, #two, #three, #four {
    padding-top: 2%;
}

#platform {
    background-image: url('/css/searchicon.png'); /* Add a search icon to input */
    background-position: 10px 12px; /* Position the search icon */
    background-repeat: no-repeat; /* Do not repeat the icon image */
    padding: 2px 2px;
    text-align: center;
    margin: 6px 0;
    border: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="one">
    <div class="row">
        <div class="col-lg-6" style="background-color: #e90649; width: 117px;">&nbsp;</div>
        <div class="col-lg-6" style="max-width: 100px; padding-left: 10px; font-size: 2vw;">Objc1<br><br></div>
        <div class="col-lg-6">
            <div class="container">
                <select class="filter" data-col="0" multiple>
                    <option value="">None</option>
                    <option value="a">plat1</option>
                    <option value="b">plat2</option>
                </select>
                <table>
                    <thead>
                    <tr>
                        <th>Platform</th>
                        <th>Channel</th>
                        <th>Objective</th>
                        <th>Num1</th>
                        <th>Num2</th>
                    </tr>
                    </thead>
                    <tbody>
                    <tr>
                        <td>plat1</td>
                        <td>chan1</td>
                        <td>Objc1</td>
                        <td>40</td>
                        <td>34</td>
                    </tr>
                    <tr>
                        <td>plat2</td>
                        <td>chan1</td>
                        <td>Objc1</td>
                        <td>26</td>
                        <td>22</td>
                    </tr>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>