簡體   English   中英

根據jQuery的多個復選框選擇隱藏一些表行

[英]hide some table row according to multiple checkbox selection with jquery

嗨,我有2個這樣的表格,其中的某些復選框用於過濾表格:

<label><input type="checkbox" name="chk_filter_Grocery_1" id="chk_filter_Grocery_1_1" value="1;">Alamond</label>
<label><input type="checkbox" name="chk_filter_Grocery_1" id="chk_filter_Grocery_1_2" value="14;">Apple</label>
<label><input type="checkbox" name="chk_filter_Grocery_1" id="chk_filter_Grocery_1_3" value="5;6;">Lemon & Orange</label>
<label><input type="checkbox" name="chk_filter_Grocery_1" id="chk_filter_Grocery_1_4" value="17;">Coconut</label>

<table width="620px" id="Grocery-NA768">
    <tbody>
        <tr class="14">
            <td width="185" height="35" align="left">Apple</td>
            <td width="65" height="35" align="center" valign="middle">3 Kg</td>
            <td width="80" height="35" align="center" valign="middle">28/07/2011</td>
        </tr>
        <tr class="5">
            <td height="35" align="left">Lemon</td>
            <td height="35" align="center" valign="middle">5 Kg</td>
            <td height="35" align="center" valign="middle">28/07/2011&nbsp;</td>
        </tr>
        <tr class="17">
            <td height="35" align="left">Coconut</td>
            <td height="35" align="center" valign="middle">4 Kg</td>
            <td height="35" align="center" valign="middle">28/07/2011&nbsp;</td>
        </tr>
        <tr class="14">
            <td height="35" align="left">Apple</td>
            <td height="35" align="center" valign="middle">2 Kg</td>
            <td height="35" align="center" valign="middle">27/04/2011&nbsp;</td>
        </tr>
        <tr class="1">
            <td height="35" align="left">Almond</td>
            <td height="35" align="center" valign="middle">3 Kg</td>
            <td height="35" align="center" valign="middle">27/04/2011&nbsp;</td>
        </tr>
        <tr class="6">
            <td height="35" align="left">Orange</td>
            <td height="35" align="center" valign="middle">3 kg</td>
            <td height="35" align="center" valign="middle">27/04/2011&nbsp;</td>
        </tr>
    </tbody>
</table>

<label><input type="checkbox" name="chk_filter_Wine_1" id="chk_filter_Wine_1_1" value="51;">Brunello di Montalcino</label>
<label><input type="checkbox" name="chk_filter_Wine_1" id="chk_filter_Wine_1_2" value="4;">Dolcetto</label>
<label><input type="checkbox" name="chk_filter_Wine_1" id="chk_filter_Wine_1_3" value="35;64;">Pinot noir & Pinot blanc</label>
<label><input type="checkbox" name="chk_filter_Wine_1" id="chk_filter_Wine_1_4" value="72;">Shiraz </label>
<table width="620px" id="Wine-NA768">
    <tbody>
        <tr class="4">
            <td width="185" height="35" align="left">Dolcetto</td>
            <td width="65" height="35" align="center" valign="middle">3 b</td>
            <td width="80" height="35" align="center" valign="middle">28/07/2011</td>
        </tr>
        <tr class="35">
            <td height="35" align="left">Pinot blanc</td>
            <td height="35" align="center" valign="middle">5 b.</td>
            <td height="35" align="center" valign="middle">28/07/2011&nbsp;</td>
        </tr>
        <tr class="72">
            <td height="35" align="left">Shiraz</td>
            <td height="35" align="center" valign="middle">4 b.</td>
            <td height="35" align="center" valign="middle">28/07/2011&nbsp;</td>
        </tr>
        <tr class="14">
            <td height="35" align="left">Dolcetto</td>
            <td height="35" align="center" valign="middle">2 b.</td>
            <td height="35" align="center" valign="middle">27/04/2011&nbsp;</td>
        </tr>
        <tr class="51">
            <td height="35" align="left">Brunello di Montalcino</td>
            <td height="35" align="center" valign="middle">3 b.</td>
            <td height="35" align="center" valign="middle">27/04/2011&nbsp;</td>
        </tr>
        <tr class="64">
            <td height="35" align="left">Pinot noir</td>
            <td height="35" align="center" valign="middle">3 b.</td>
            <td height="35" align="center" valign="middle">27/04/2011&nbsp;</td>
        </tr>
    </tbody>
</table>

這兩個表具有不同的ID(Grocery-NA768)和(Wine-NA768),並且每個復選框都有一個或多個(最大2)值(1; 12;),並且每個表都有對應的數字作為類。

我想過濾表格,單擊復選框。 即,當我單擊“蘋果”復選框(值14)時,我只想看到表中的蘋果(第14類),然后如果單擊“柑橘”(值1; 12;)復選框,我將看到蘋果(第14類),檸檬(5類)和橙色。 如果取消選中所有復選框,則將看到整個列表。 酒桌也一樣。 我找到了新的jquery,發現如何單擊復選框來顯示或隱藏表行,但與我的需求沒有任何相似之處。

在此先感謝Michele

工作示例: http : //jsfiddle.net/petersendidit/6dNKA/3/

查看代碼中的注釋:

// Define some config objects and loop over them to get things set up
$.each([{
    table: $("#Grocery-NA768"),
    inputs: $("input[name='chk_filter_Grocery_1']")
},{
    table: $("#Wine-NA768"),
    inputs: $("input[name='chk_filter_Wine_1']")
}],function(i, obj){
    var list = [];
    obj.inputs.click(function() {
        var that = $(this),
            value = that.val().match(/\d+/g),
            rows = obj.table.find("tr");

        // If its checked then add it to the list
        if ( that.prop("checked") ) {
            list = $.merge(list, value);
        } else {
            // if its not then remove the items from the list
            list = $.map( list, function( x ) {
                return ( $.inArray( x, value ) > -1 ) ? null : x;
            });
        }
        // If the list has items
        if ( list.length ) {
            rows.hide() // hide all rows
                .filter("."+list.join(",.")) // find the ones we care about
                .show(); // and show them
        } else {
            // If no items in the list
            rows.show(); // show every row
        }
    });
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM