简体   繁体   English

使用jquery / javascript的单选按钮过滤器

[英]Radio Button filter using jquery/javascript

There is an HTML table which contains two column(Source and Type). 有一个HTML表,其中包含两列(“源”和“类型”)。 To filter the data, there are two radio buttons. 要过滤数据,有两个单选按钮。 I am able to filter the data for each radio button but I am struggling to combine the two radio button filter functions. 我能够过滤每个单选按钮的数据,但是我很难将两个单选按钮过滤器功能结合在一起。

HTML Code HTML代码

<form action="" id="Details">
<fieldset>
<label><input type="radio" name="Option1" value="Critical" >Critical</label>
<label><input type="radio" name="Option1" value="Non Critical" >Non Critical</label>
<label><input type="radio" name="Option1" value="A" checked="checked" >All</label>
    <br>
    <br>
<label><input type="radio" name="Option2" value="Internal" >Internal</label>
<label><input type="radio" name="Option2" value="External" >External</label>
<label><input type="radio" name="Option2" value="A" checked="checked" >All</label>
</fieldset>
</form>
<br>
    <br>
<div>
        <table id="DataTable"  border="1">
        <tr>
        <th>Source</th>
        <th>Type</th>
        </tr>            
        <tr>
        <td class="Source">Internal</td>
        <td  class="Type">Critical</td>
        </tr>
        <tr>
        <td  class="Source">Internal</td>
        <td class="Type">Non Critical</td>
        </tr>
        <tr>
        <td class="Source">External</td>
        <td class="Type">Critical</td>
        </tr>
        <tr>
        <td class="Source">External</td>
        <td class="Type">Non Critical</td>
        </tr>       
        </table>
</div>
 <button id='btnFilter'>Go</button>       

Script 脚本

$('#btnFilter').click(function() {

    var val1 = $('[name=Option1]:checked').val();
    var val2 = $('[name=Option2]:checked').val();


    $('tr').hide();

    $('tr td.Source').each(function() {

        if ($(this).text() == val2 )
        {

            $(this).parent().show();
        }
    });
    alert("ok");
    $('tr').hide();
        $('tr td.Type').each(function() {

        if ($(this).text() == val1 )
        {

            $(this).parent().show();
        }
    });

});

Expected Behavior User will select both radio button values(if not selected then default values will be considered) and then HTML table should be filtered considered both the selected valued.If user changes value of one radio button, the HTML table should be filtered accordingly. 预期的行为用户将选择两个单选按钮值(如果未选中,则将考虑默认值),然后应考虑所选的两个值来过滤HTML表。如果用户更改一个单选按钮的值,则应相应地过滤HTML表。 In my code, each filter logic works fine but I am not able to combine both the logics ie If am applying the filter for second radio button then it should take data which is filtered by first radio button and apply show/hide on top of that but when i do it considers whole data. 在我的代码中,每个过滤器逻辑都​​能正常工作,但是我无法将这两种逻辑结合在一起,即,如果将过滤器应用于第二个单选按钮,则它应该获取被第一个单选按钮过滤的数据,并在其上方应用显示/隐藏但是当我这样做时,它会考虑整个数据。

Current logic is Fiddle 当前的逻辑是小提琴

Also, When users selects all from any radio buttons then it means all data should be considered wrt to that radio button. 同样,当用户从任何单选按钮中选择全部时,则意味着应将所有数据视为该单选按钮。 If both All are selected then whole HTML table data. 如果全部选中,则整个HTML表数据。

There are a couple problems here. 这里有几个问题。 First, you only want to hide the non heading rows, so you can add a <thead> and <tbody> tag to let you be more specific. 首先,您只想隐藏非标题行,因此可以添加<thead><tbody>标记,以使其更加具体。 Second, you should be testing whether the value is "A" as well, so that when you select "All" the rows will still be displayed. 其次,您应该测试该值是否也为“ A”,以便在选择“全部”时仍将显示行。 And third, when you do the secondary matching on the Type you should change the selector to only grab the currently visible tables rows. 第三,在Type上进行辅助匹配时,应更改选择器以仅获取当前可见的表行。

jsfiddle 的jsfiddle

$('#btnFilter').click(function() {
    var val1 = $('input[name=Option1]:checked').val();
    var val2 = $('input[name=Option2]:checked').val();

    $('tbody tr').hide();

    $('tbody td.Source').each(function() {
        var value = $(this).text();

        if (value == val2 || val2 == "A") {
            $(this).parent().show();
        }
    });

    $('tbody tr:visible td.Type').each(function() {
        var value = $(this).text();

        if (value == val1 || val1 == "A") {
            $(this).parent().show();
        } else {
            $(this).parent().hide();
        }
    });

});

Here is a fiddle 这是一个小提琴

http://jsfiddle.net/afrievalt/zmGNU/ http://jsfiddle.net/afrievalt/zmGNU/

Use jQuery's show and hide function. 使用jQuery的显示和隐藏功能。
1. Add classes to the row elm representing the data it contains. 1.将类添加到表示其包含的数据的行elm。
2. Change the value of your radio buttons to selectors you want to filter out.: (.c .e .i & .n) 2.将单选按钮的值更改为要过滤的选择器。:(.c .e .i&.n)
3. Use the values of your radio buttons to hide the unwanted rows. 3.使用单选按钮的值隐藏不需要的行。

$('#btnFilter').click(function() {
   var val1 = $('[name=Option1]:checked').val();
   var val2 = $('[name=Option2]:checked').val();
   $("tr").show();
   $(val1).hide();
   $(val2).hide();
});
$('#btnFilter').click(function() {

    var val1 = $('[name=Option1]:checked').val();
    var val2 = $('[name=Option2]:checked').val();
    $('tr').each(function() {
        var src = $(this).find('.Source').text();
        var typ = $(this).find('.Type').text();

        if ((typ == val1 || val1 == 'A' ) && (src == val2 || val2 == 'A' )) {
            $(this).show();
        } else {
            $(this).hide();
        }
    });
});

http://jsfiddle.net/effone/ZxUUA/4/ http://jsfiddle.net/effone/ZxUUA/4/

This will ensure the bonding of the row cells. 这将确保行单元的绑定。 I believe this is what you want. 我相信这就是您想要的。

Edit: 编辑:

@Charles & @user2223335: @Charles和@ user2223335:

No need to add additional markups or classes only changing the else clause to something like: 无需添加其他标记或类,只需将else子句更改为:

    else if (typ !='' && src != '')

will ensure the header (tcat) not to hide. 将确保标题(tcat)不隐藏。

http://jsfiddle.net/effone/ZxUUA/5/ http://jsfiddle.net/effone/ZxUUA/5/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM