首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用jquery添加多个过滤器

使用jquery添加多个过滤器
EN

Stack Overflow用户
提问于 2015-08-13 22:40:59
回答 2查看 74关注 0票数 1

我在html中使用了一个带有过滤器的表,但我仍然有问题:

  1. 我试图让多个过滤器同时工作,我试图让它显示任务类型,就好像已经完成或没有完成一样。
  2. 我无法使已完成的任务筛选器工作,我添加了一个复选框筛选器,根据是否勾选了该框,将一个类添加到行中,它是否添加了类:选中时已完成,未选中时为notCompleted,但现在它不会对它们进行筛选。

我的小提琴如果你需要的话,这里

以下是jquery的代码:

代码语言:javascript
复制
    // Filter Row Script Type of Quest
// ........................................
$('.filterMenu a').on('click', function (e) {
    e.preventDefault();
    var c = $(this).data('qtype');

    //get all trs from tbody
    var trs = $("#questTable").find("tbody tr");
    trs.hide();

    //Filter all trs from tbody
    trs.filter(function (i, v) {
        if ($(this).data("qtype") == c) {
            return true;
        }
        if (c == "all") {
            return true;
        }
        return false;
    })
    //just show the row if it fits the criteria
    .show();

});

// Filter Row Script Quest Completed or Not
// ........................................
$('.filterMenuCompleted a').on('click', function (e) {
    e.preventDefault();
    var c = $(this).attr('class');

    //get all trs from tbody
    var trs = $("#questTable").find("tbody tr");
    trs.hide();

    //Filter all trs from tbody
    trs.filter(function (i, v) {
        if ($(this).attr("class") == c) {
            return true;
        }
        if (c == "all") {
            return true;
        }
        return false;
    })
    //just show the row if it fits the criteria
    .show();

});

在这里我的html:

代码语言:javascript
复制
<body>
    <div id="content">
        <div class="filterMenuCompleted">
                <ul>
                    <li><a href="#" class="all">All</a></li>
                    <li><a href="#" class="completed">Completed</a></li>
                    <li><a href="#" class="notCompleted">Not Completed</a></li>
                </ul>
            </div>
        <div class="filterMenu">
            <ul>
                <li><a href="#" data-qtype="all">All</a></li>
                <li><a href="#" data-qtype="mcq">Main Scenario</a></li>
                <li><a href="#" data-qtype="sq">Side Quest</a></li>
            </ul>
        </div>
        <table id="questTable" style="max-width: 800px;" class="all">
            <thead>
                <tr>
                    <th class="table-header">#</th>
                    <th class="table-header">Type</th>
                    <th class="table-header">Icon</th>
                    <th class="table-header">LvL</th>
                    <th class="table-header">Name</th>
                    <th class="table-header">Issuing NPC</th>
                    <th class="table-header">Location</th>
                    <th class="table-header">Done It?</th>
                </tr>
            </thead>
            <tbody>
                <tr id="line1" class="table-row" data-qtype="mcq">
                    <td class="shortTd">MC-1</td>
                    <td class="shortTd">Main Scenario</td>
                    <td class="shortTd">
                        <img src="./images/Quests/Main_Scenario/mc.png" alt="" />
                    </td>
                    <td class="shortTd">1</td>
                    <td> <a href="#"> mcq 1</a>

                    </td>
                    <td>Name 1</td>
                    <td>Area 1</td>
                    <td class="shortTd">
                        <input class="completion" type="checkbox" id="checkbox1">
                    </td>
                </tr>
                <tr id="line2" class="table-row" data-qtype="mcq">
                    <td class="shortTd">Mc-2</td>
                    <td class="shortTd">Main Scenario</td>
                    <td class="shortTd">
                        <img src="./images/Quests/Main_Scenario/mc.png" alt="" />
                    </td>
                    <td class="shortTd">1</td>
                    <td> <a href="#">mcq 2</a>

                    </td>
                    <td>Name 2</td>
                    <td>Area 2</td>
                    <td class="shortTd">
                        <input class="completion" type="checkbox" id="checkbox2">
                    </td>
                </tr>
                <tr id="line2" class="table-row" data-qtype="sq">
                    <td class="shortTd">Sq-1</td>
                    <td class="shortTd">Side Quest</td>
                    <td class="shortTd">
                        <img src="./images/Quests/Main_Scenario/mc.png" alt="" />
                    </td>
                    <td class="shortTd">1</td>
                    <td> <a href="#">Side quest</a>

                    </td>
                    <td>Name 3</td>
                    <td>Area 3</td>
                    <td class="shortTd">
                        <input class="completion" type="checkbox" id="checkbox2">
                    </td>
                </tr>
            </tbody>
        </table>
    </div>
    <div id="footer">Brought to you by Vesper Tia @ Moogle
        <br/>&copy; 2010 - 2015 SQUARE ENIX CO., LTD. All Rights Reserved.</div>
</body>

当然,我的css:

代码语言:javascript
复制
/*Global Resets*/

/*....................................*/
 * {
    margin:0;
    padding:0
}
html {
    font-family:Arial, sans-serif;
    font-size:1em
}
h1, h2, h3 {
    margin:0 auto 1em;
    text-align:center
}
h1 {
    margin-top:1em;
    margin-bottom:0
}
h2 {
    color:#e8d19e
}
h3 {
    color:#5a9dd1
}
h4 {
    margin-bottom:.5em;
    padding-bottom:20px;
    text-align:center
}
a {
    color:#CC9900;
    text-decoration:none;
    font-weight:700
}
p {
    margin-bottom:1em;
    text-align:justify;
    line-height:1.3em
}
img {
    max-width:100%
}
table {
    margin:1em auto 3em;
    border-collapse:collapse
}
table th {
    padding:.5em 1em;
    height:2em;
    font-weight:700;
    color:#9ba3b6;
    text-shadow:0 0 5px #000;
    border-bottom:solid 1px #222;
}
table td {
    padding:.5em 1em;
    height:2em;
    background-color:#282828;
    border-bottom:solid 1px #666;
    line-height:1.4em;
    text-align:left
}
#content {
    padding:1em;
    color:#DDD
}

.filterMenuCompleted ul{height: 20px;float: left; margin-right: 50px;}
.filterMenuCompleted ul li{
    display: block;
    float: left;
    margin-right: 5px;
}

.filterMenu ul{height: 20px;float: left; margin-right: 50px;}
.filterMenu ul li{
    display: block;
    float: left;
    clear: right;
    margin-right: 5px;
}
/*Responsive Settings*/

/*...............................................*/
 @media all and (max-width: 500px) {
    table {
        font-size:.8em
    }
    .npcFullCardName, .npcIcon img {
        display:none
    }
    .cardLocation {
        padding:15px;
        width:300px;
        background-size:300px;
        left:-85px;
        font-size:.8em
    }
}
@media all and (min-width: 501px) {
    .npcFullCardName {
        display:inline;
        color:#9c9;
        cursor:help
    }
}
@media all and (max-width: 1023px) {
    html {
        background:url(../images/background_low.jpg) no-repeat top center fixed #000
    }
    table {
        min-width:100%;
        width:100%
    }
}
@media all and (min-width: 1024px) {
    html {
        background:url(../images/background.jpg) no-repeat center center fixed #000;
        -webkit-background-size:cover;
        -moz-background-size:cover;
        -o-background-size:cover;
        background-size:cover
    }
    #content {
        margin:0 auto;
        max-width:1000px
    }
}
}
/*Row Highlight*/

/*................................................................*/
 .current-row td {
    background-color:#1b1b1b;
    color:#FFF
}
.completed td {
    opacity: 0.3
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-08-13 23:00:51

在这里,你们都知道了--我刚加了一个if ($(this).hasClass(c)),而不是if ($(this).attr("class") == c)

参考片段

代码语言:javascript
复制
$(function(){// Filter Row Script Type of Quest
// ........................................
$('.filterMenu a').on('click', function (e) {
    e.preventDefault();
    var c = $(this).data('qtype');

    //get all trs from tbody
    var trs = $("#questTable").find("tbody tr");
    trs.hide();

    //Filter all trs from tbody
    trs.filter(function (i, v) {
        if ($(this).data("qtype") == c) {
            return true;
        }
        if (c == "all") {
            return true;
        }
        return false;
    })
    //just show the row if it fits the criteria
    .show();

});

// Filter Row Script Quest Completed or Not
// ........................................
$('.filterMenuCompleted a').on('click', function (e) {
    e.preventDefault();
    var c = $(this).attr('class');

    //get all trs from tbody
    var trs = $("#questTable").find("tbody tr");
    trs.hide();

    //Filter all trs from tbody
    trs.filter(function (i, v) {
        if ($(this).hasClass( c)) {
            return true;
        }
        if (c == "all") {
            return true;
        }
        return false;
    })
    //just show the row if it fits the criteria
    .show();

});

// Row Hover Script
// ........................................
$(document).ready(function () {
    $('.table-row').hover(function () {
        $(this).addClass('current-row');
    }, function () {
        $(this).removeClass('current-row');
    });
});

// Row hide Script
// ........................................
$(document).ready(function () {
    $('tr')
        .filter(':has(:checkbox:checked)')
        .addClass('completed')
        .end()
        .click(function (event) {
        if (event.target.type !== 'checkbox') {
            $(':checkbox', this).trigger('click');
        }

    })
        .find(':checkbox')
        .click(function (event) {
        $(this).parents('tr:first').toggleClass('completed');
    });
});

$(document).ready(function () {
    $('tr')
        .filter(':has(:checkbox:not(:checked))')
        .addClass('notCompleted')
        .end()
        .click(function (event) {
        if (event.target.type !== 'checkbox') {
            $(':checkbox', this).trigger('click');
        }

    })
        .find(':checkbox')
        .click(function (event) {
        $(this).parents('tr:first').toggleClass('notCompleted');
    });
});

});
代码语言:javascript
复制
/*....................................*/
 * {
    margin:0;
    padding:0
}
html {
    font-family:Arial, sans-serif;
    font-size:1em
}
h1, h2, h3 {
    margin:0 auto 1em;
    text-align:center
}
h1 {
    margin-top:1em;
    margin-bottom:0
}
h2 {
    color:#e8d19e
}
h3 {
    color:#5a9dd1
}
h4 {
    margin-bottom:.5em;
    padding-bottom:20px;
    text-align:center
}
a {
    color:#CC9900;
    text-decoration:none;
    font-weight:700
}
p {
    margin-bottom:1em;
    text-align:justify;
    line-height:1.3em
}
img {
    max-width:100%
}
table {
    margin:1em auto 3em;
    border-collapse:collapse
}
table th {
    padding:.5em 1em;
    height:2em;
    font-weight:700;
    color:#9ba3b6;
    text-shadow:0 0 5px #000;
    border-bottom:solid 1px #222;
}
table td {
    padding:.5em 1em;
    height:2em;
    background-color:#282828;
    border-bottom:solid 1px #666;
    line-height:1.4em;
    text-align:left
}
#content {
    padding:1em;
    color:#DDD
}

.filterMenuCompleted ul{height: 20px;float: left; margin-right: 50px;}
.filterMenuCompleted ul li{
	display: block;
	float: left;
	margin-right: 5px;
}

.filterMenu ul{height: 20px;float: left; margin-right: 50px;}
.filterMenu ul li{
	display: block;
	float: left;
	clear: right;
	margin-right: 5px;
}
/*Responsive Settings*/

/*...............................................*/
 @media all and (max-width: 500px) {
    table {
        font-size:.8em
    }
    .npcFullCardName, .npcIcon img {
        display:none
    }
    .cardLocation {
        padding:15px;
        width:300px;
        background-size:300px;
        left:-85px;
        font-size:.8em
    }
}
@media all and (min-width: 501px) {
    .npcFullCardName {
        display:inline;
        color:#9c9;
        cursor:help
    }
}
@media all and (max-width: 1023px) {
    html {
        background:url(../images/background_low.jpg) no-repeat top center fixed #000
    }
    table {
        min-width:100%;
        width:100%
    }
}
@media all and (min-width: 1024px) {
    html {
        background:url(../images/background.jpg) no-repeat center center fixed #000;
        -webkit-background-size:cover;
        -moz-background-size:cover;
        -o-background-size:cover;
        background-size:cover
    }
    #content {
        margin:0 auto;
        max-width:1000px
    }
}
}
/*Row Highlight*/

/*................................................................*/
 .current-row td {
    background-color:#1b1b1b;
    color:#FFF
}
.completed td {
    opacity: 0.3
}
代码语言:javascript
复制
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="content">
        <div class="filterMenuCompleted">
				<ul>
					<li><a href="#" class="all">All</a></li>
	        		<li><a href="#" class="completed">Completed</a></li>
	        		<li><a href="#" class="notCompleted">Not Completed</a></li>
        		</ul>
			</div>
        <div class="filterMenu">
            <ul>
                <li><a href="#" data-qtype="all">All</a></li>
                <li><a href="#" data-qtype="mcq">Main Scenario</a></li>
                <li><a href="#" data-qtype="sq">Side Quest</a></li>
            </ul>
        </div>
        <table id="questTable" style="max-width: 800px;" class="all">
            <thead>
                <tr>
                    <th class="table-header">#</th>
                    <th class="table-header">Type</th>
                    <th class="table-header">Icon</th>
                    <th class="table-header">LvL</th>
                    <th class="table-header">Name</th>
                    <th class="table-header">Issuing NPC</th>
                    <th class="table-header">Location</th>
                    <th class="table-header">Done It?</th>
                </tr>
            </thead>
            <tbody>
                <tr id="line1" class="table-row" data-qtype="mcq">
                    <td class="shortTd">MC-1</td>
                    <td class="shortTd">Main Scenario</td>
                    <td class="shortTd">
                        <img src="./images/Quests/Main_Scenario/mc.png" alt="" />
                    </td>
                    <td class="shortTd">1</td>
                    <td> <a href="#"> mcq 1</a>

                    </td>
                    <td>Name 1</td>
                    <td>Area 1</td>
                    <td class="shortTd">
                        <input class="completion" type="checkbox" id="checkbox1">
                    </td>
                </tr>
                <tr id="line2" class="table-row" data-qtype="mcq">
                    <td class="shortTd">Mc-2</td>
                    <td class="shortTd">Main Scenario</td>
                    <td class="shortTd">
                        <img src="./images/Quests/Main_Scenario/mc.png" alt="" />
                    </td>
                    <td class="shortTd">1</td>
                    <td> <a href="#">mcq 2</a>

                    </td>
                    <td>Name 2</td>
                    <td>Area 2</td>
                    <td class="shortTd">
                        <input class="completion" type="checkbox" id="checkbox2">
                    </td>
                </tr>
                <tr id="line2" class="table-row" data-qtype="sq">
                    <td class="shortTd">Sq-1</td>
                    <td class="shortTd">Side Quest</td>
                    <td class="shortTd">
                        <img src="./images/Quests/Main_Scenario/mc.png" alt="" />
                    </td>
                    <td class="shortTd">1</td>
                    <td> <a href="#">Side quest</a>

                    </td>
                    <td>Name 3</td>
                    <td>Area 3</td>
                    <td class="shortTd">
                        <input class="completion" type="checkbox" id="checkbox2">
                    </td>
                </tr>
            </tbody>
        </table>
    </div>
    <div id="footer">Brought to you by Vesper Tia @ Moogle
        <br/>&copy; 2010 - 2015 SQUARE ENIX CO., LTD. All Rights Reserved.</div>

票数 2
EN

Stack Overflow用户

发布于 2015-08-13 23:20:34

我知道您已经接受了,但是我修改了您的代码,并提出了一种结合使用这两个过滤器的工作方法。下面是我更改的HTML (我实际上只修改了两个过滤器中的列表锚点,因此它们具有filterVal属性而不是类):

代码语言:javascript
复制
<div class="filterMenuCompleted">
    <ul>
        <li><a href="#" filterVal="all">All</a></li>
        <li><a href="#" filterVal="completed">Completed</a></li>
        <li><a href="#" filterVal="notCompleted">Not Completed</a></li>
    </ul>
</div>
<div class="filterMenu">
    <ul>
        <li><a href="#" filterVal="all">All</a></li>
        <li><a href="#" filterVal="mcq">Main Scenario</a></li>
        <li><a href="#" filterVal="sq">Side Quest</a></li>
    </ul>
</div>

而JavaScript (我刚刚抹去了你所拥有的,重新开始了,所以,很抱歉--但是你可以随便挑出你想要保存的碎片,然后把剩下的扔出去):

代码语言:javascript
复制
var filterMenuCompSel = false;
var filterMenuSel = false;

$('.filterMenu ul li a').on('click', function(){
    $(this).closest('ul').find('li').removeClass('selected');
    $(this).closest('li').addClass('selected');
    filterMenuSel = $(this).attr('filterVal');
    ApplyFilter();
});
$('.filterMenuCompleted ul li a').on('click', function(){
    $(this).closest('ul').find('li').removeClass('selected');
    $(this).closest('li').addClass('selected');
    filterMenuCompSel = $(this).attr('filterVal');
    ApplyFilter();
});

function ApplyFilter(){
    $('#questTable tbody tr').each(function(){
        hide = false;
        switch(filterMenuCompSel){
            case 'completed':
                if(!$(this).find('.completion').is(':checked')){
                    hide = true;   
                }
                break;
            case 'notCompleted':
                if($(this).find('.completion').is(':checked')){
                    hide = true;
                }
                break;
        }
        if(!hide){
            switch(filterMenuSel){
                case 'mcq':
                    if($(this).find('td:nth-child(2)').text() != 'Main Scenario'){
                        hide = true;
                    }
                    break;
                case 'sq':
                    if($(this).find('td:nth-child(2)').text() != 'Side Quest'){
                        hide = true;
                    }
                    break;
            }
        }
        if(!hide){
            $(this).show();
        }
        if(hide){
            $(this).hide();
        }
    });
}

下面是一个有用的小提琴:https://jsfiddle.net/62qL9hed/

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31999583

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档