简体   繁体   English

jQuery - 选中复选框时的颜色表行,否则删除

[英]jQuery - Color table rows when checkbox is checked and remove when not

I'm a little stuck, can't get my head around this problem. 我有点卡住,无法理解这个问题。 I have setup some jQuery that identifies the values in radio inputs, which align with values within the table. 我已经设置了一些jQuery来识别无线电输入中的值,这些值与表中的值对齐。 This is the start to doing some filtering on a table. 这是对表进行一些过滤的开始。

I want the following three things to happen: 我希望发生以下三件事:

  • Whenever you click on a filter and the cell has the same value, it's row gets highlighted 无论何时单击过滤器并且单元格具有相同的值,它的行都会突出显示
  • Whenever you check that option off, it gets removed 每当您检查该选项时,它都会被删除
  • If the page loads and an option is already checked, the row gets highlighted on load 如果页面加载并且已选中某个选项,则该行将在加载时突出显示

jQuery Code: jQuery代码:

$(function () {
    // On Change of Radio Buttons
    $('input').on('change', function () {
        var filter = $('input:checkbox[name=filter]:checked').val();

        // Edit the Rows
        $(".gameStatus").filter(function () {
            return $(this).text() == filter;
        }).parent('tr').addClass('filtered');
    });
});

jsfiddle: http://jsfiddle.net/darcyvoutt/8xgd51mg/ jsfiddle: http//jsfiddle.net/darcyvoutt/8xgd51mg/

$(function () {
    var filters = [];
    var edit_rows = function () {
        filters = [];
        $('input:checkbox[name=filter]:checked').each(function () {
            filters.push(this.value);
        });

        // Edit the Rows

        $(".gameStatus").each(function () {
            $(this).parent('tr')
              .toggleClass('filtered',
                filters.indexOf($(this).text()) > -1
              );
        });
    }
    edit_rows();

    // On Change of Radio Buttons
    $('input').on('change', edit_rows);
});

jsfiddle 的jsfiddle

edit: added functionality so it gets invoked on page load 编辑:添加的功能,以便在页面加载时调用它

Here's alternate solution ... 这是替代解决方案......

 $(function () { function highlight() { var filter = $(this).val(); var checked = this.checked; // Edit the Rows var affectedRows = $(".gameStatus").filter(function () { return $(this).text() == filter; }); if (checked) { affectedRows.parent('tr').addClass('filtered'); } else { affectedRows.parent('tr').removeClass('filtered'); } } $('input').each(highlight); // On Change of Radio Buttons $('input').on('change', highlight); }); 
 .filtered { background: tomato; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="content"> <div class="filters"> <input type="checkbox" name="filter" id="draft" value="Draft" checked /> <label for="draft">Draft</label> <input type="checkbox" name="filter" id="active" value="Active" /> <label for="active">Active</label> <input type="checkbox" name="filter" id="complete" value="Complete" /> <label for="complete">Complete</label> <input type="checkbox" name="filter" id="acrhived" value="Archived" /> <label for="acrhived">Archived</label> </div> <table id="userManager"> <tbody> <tr> <th>Game</th> <th>Teams</th> <th>Status</th> </tr> <tr> <td>Another Game</td> <td>New Team, Project Lucrum</td> <td class="gameStatus">Active</td> </tr> <tr> <td>New Game</td> <td>No teams selected</td> <td class="gameStatus">Draft</td> </tr> <tr> <td>New Game</td> <td>New Team, Just in Case</td> <td class="gameStatus">Active</td> </tr> </tbody> </table> <div class="test"></div> </div> 

try this. 尝试这个。 :) :)

$(function () {
    var highlight = function(el){
        if(el.is(':checked')){
            $(".gameStatus:contains("+el.val()+")")
            .parent('tr').addClass('filtered');
        }else{
            $(".gameStatus:contains("+el.val()+")")
            .parent('tr').removeClass('filtered');
        }
    }

    //On Load
    highlight($('input[type="checkbox"][name=filter]:checked'));

    // On Change of Radio Buttons
    $('input').on('change', function () {
        highlight($(this));    
    });
});

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

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