逐行隐藏表格

时间:2014-07-03 08:59:40

标签: javascript html

我有2个下拉菜单(用户名和性别),我有3列(用户名,姓名和性别)的表格。
我想根据下拉值过滤表格。我必须做什么 ?

以下是代码:

<select class="form-control selectpicker">
    <option value="">Username</option>
    <option value="">user1</option>
    <option value="">user2</option>
    <option value="">user3</option>
</select>

<select class="form-control selectpicker">
    <option value="">Gender</option>
    <option value="">M</option>
    <option value="">F</option>

</select>
</div>

<table class="dynamicTable tableTools table table-striped table-primary">
    <!-- Table heading -->
        <thead>
            <tr>
                <th>Username</th>
                <th>Name</th>
                <th>Gender</th>
            </tr>
        </thead>

    <tbody>
        <tr>
            <td>user1</td>
            <td>Jane</td>
            <td>F</td>
        </tr>
        <tr>
            <td>user2</td>
            <td>John</td>
            <td>M</td>
        </tr>
        <tr>
            <td>user3</td>
            <td>Jack</td>
            <td>M</td>
        </tr>

    </tbody>
    <!-- // Table body END -->
</table>
<!-- // Table END -->

2 个答案:

答案 0 :(得分:3)

我创造了这个解决方案。

<强> HTML

<select id="username" class="form-control selectpicker">
    <option value="">Username</option>
    <option value="">user1</option>
    <option value="">user2</option>
    <option value="">user3</option>
</select>

<select id="gender" class="form-control selectpicker">
    <option value="">Gender</option>
    <option value="">M</option>
    <option value="">F</option>

</select>

<table class="dynamicTable tableTools table table-striped table-primary">
    <!-- Table heading -->
        <thead>
            <tr>
                <th>Username</th>
                <th>Name</th>
                <th>Gender</th>
            </tr>
        </thead>

    <tbody>
        <tr>
            <td>user1</td>
            <td>Jane</td>
            <td>F</td>
        </tr>
        <tr>
            <td>user2</td>
            <td>John</td>
            <td>M</td>
        </tr>
        <tr>
            <td>user3</td>
            <td>Jack</td>
            <td>M</td>
        </tr>

    </tbody>
    <!-- // Table body END -->
</table>

<强> JS

$("#username").on("change",
               function(){
                   var a = $(this).find("option:selected").html();

                   $("table tr td:first-child").each(
                       function(){
                           if($(this).html() != a){
                               $(this).parent().hide();
                           }
                           else{
                               $(this).parent().show();
                           }
                       });
               });

$("#gender").on("change",
               function(){
                   var a = $(this).find("option:selected").html();

                   $("table tr td").each(
                       function(){
                           if($(this).html() != a){
                               $(this).parent().hide();
                           }
                           else{
                               $(this).parent().show();
                           }
                       });
               });

fiddle

答案 1 :(得分:2)

我用jQuery创建了一个简单的脚本(你必须包含jquery库)。

<强> HTML:

<select name="username" class="form-control selectpicker">
    <option value="">Username</option>
    <option value="">user1</option>
    <option value="">user2</option>
    <option value="">user3</option>
</select>

<select name="gender" class="form-control selectpicker">
    <option value="">Gender</option>
    <option value="">M</option>
    <option value="">F</option>

</select>
</div>

<table id="tbl" class="dynamicTable tableTools table table-striped table-primary">
    <!-- Table heading -->
        <thead>
            <tr>
                <th>Username</th>
                <th>Name</th>
                <th>Gender</th>
            </tr>
        </thead>

    <tbody>
        <tr>
            <td>user1</td>
            <td>Jane</td>
            <td>F</td>
        </tr>
        <tr>
            <td>user2</td>
            <td>John</td>
            <td>M</td>
        </tr>
        <tr>
            <td>user3</td>
            <td>Jack</td>
            <td>M</td>
        </tr>

    </tbody>
    <!-- // Table body END -->
</table>
<!-- // Table END -->

Javascript(jQuery):

$(document).ready(function() {
    function calculate() {
        $('#tbl tbody tr').show();
        var sel_username = $('select[name="username"] option:selected').text();
        var sel_gender = $('select[name="gender"] option:selected').text();

        if(sel_username == 'Username' && sel_gender == 'Gender') {
            return;
        }

        $('#tbl tbody tr').each(function() {
            var col_username = $(this).find('td').first();
            var col_gender = $(this).find('td').last();

            if(col_username.text() !== sel_username && sel_username !== 'Username') {
                $(this).hide();
            }

            if(col_gender.text() !== sel_gender && sel_gender !== 'Gender') {
                if($(this).is(':visible')) {
                    $(this).hide();
                }
            }
        });
    }

    $('select[name="username"]').change(function() {
        calculate();
    });

    $('select[name="gender"]').change(function() {
        calculate();
    });
});

<强> JSFIDDLE