在jquery中组合表过滤器

Combines table filters in jquery

本文关键字:过滤器 组合 jquery      更新时间:2023-09-26

我有一个有不同位置的表,并实现了一些允许您筛选列表的简单按钮。第一个过滤器是位置(北、东、中、南、西),这是基于邮政编码的。另一个过滤器是"印象"。这只显示列中具有4或更高值的位置。过滤器单独工作很好,但不能一起工作。我想要的结果是,当我按下"西部"时,它会显示"西部"中的地方,当我点击"印象"时,我希望看到西部的地方有4或5分的印象。

JSFiddle这里

$('.table td.postcode').each(function() {
	var cellText = $(this).html();
	var locationString = cellText.substring(0,2);
	if (locationString.indexOf('W') > -1){
		$(this).parent().addClass('west');    			
	}
	if (locationString.indexOf('C') > -1){
		$(this).parent().addClass('central');    			
	} 
	if (locationString.indexOf('E') > -1){
		$(this).parent().addClass('east');    			
	}
	if (locationString.indexOf('S') > -1){
		$(this).parent().addClass('south');    			
	}
	if (locationString.indexOf('N') > -1){
		$(this).parent().addClass('north');    			
	}
});
$("input[name='filterStatus'], select.filter").change(function () {
	var classes = [];
	$("input[name='filterStatus']").each(function() {
		if ($(this).is(":checked")) {
			classes.push('.'+$(this).val());
		}
	});
	if (classes == "") {
	// if no filters selected, show all items
		$("#StatusTable tbody tr").show();
	} else {
	// otherwise, hide everything...
		$("#StatusTable tbody tr").hide();
		    		        
		// then show only the matching items
		rows = $("#StatusTable tr").filter(classes.length ? classes.join(',') : '*');
		if (rows.size() > 0) {
		    rows.show();
		}
	}
		    		    
});
$("input[name='impressStatus']").change(function(){
	var classes = [];
	$("input[name='impressStatus']").each(function() {
		if ($(this).is(":checked")) {
			classes.push('.'+$(this).val());
		}
	});
	if(classes == ""){
		$("#StatusTable tbody tr").show();
	}
	else{
	$(".table td.impress").each(function(){
			if($(this).data("impress") >= 4){
				$(this).parent().show();
			}
			else{
				$(this).parent().hide();
			}
		});	
	}
});
<script src="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!--BUTTON FILTERS -->
<div class="btn-toolbar" role="toolbar" aria-label="...">
    <div class="btn-group" style="" data-toggle="buttons">
		<label class="btn btn-primary outline">
			<input type="checkbox" name="filterStatus" value="north" autocomplete="off">North
		</label>
		<label class="btn btn-primary outline">
			<input type="checkbox" name="filterStatus" value="east" autocomplete="off" class="radio">East
		</label>
		<label class="btn btn-primary outline">
			<input type="checkbox" name="filterStatus" value="central" autocomplete="off" class="radio">Central
		</label>
		<label class="btn btn-primary outline">
			<input type="checkbox" name="filterStatus" value="south"autocomplete="off" class="radio">South				            </label>
		<label class="btn btn-primary outline">
			<input type="checkbox" name="filterStatus" value="west" autocomplete="off" class="radio">West
		</label>
	</div><!-- button group -->
            
	<label class="btn btn-primary outline">
		<input type="checkbox" name="impressStatus" class="radio" aria-pressed="true" autocomplete="off">Impress her
    </label>
 </div><!-- btn toolbar-->
            
 <!--TABLE -->
            
<table class="table" id="StatusTable">
    <thead>
        <tr>
          <th data-sort="string" style="cursor:pointer">name</th>
      <!-- <th>Description</th> -->
          <th data-sort="string" style="cursor:pointer;">postcode</th>
          <th data-sort="int" style="cursor:pointer;">price</th>
          <th data-sort="int" style="cursor:pointer;">total</th>
          <th data-sort="int" style="cursor:pointer;">impress</th>
          <th colspan="4"></th>
        </tr>
    </thead>
  <tbody>
      <tr data-link="/places/1">
        <td>Name of place 1</td>
        <td class="postcode">NW1</td>
        <td class="price" data-price='3'>3</td>
        <td class="rating" data-rating='69'>69</td>
        <td class="impress" data-impress='4'>4</td>
      </tr>
      <tr data-link="/places/2">
        <td>Name of place 2</td>
        <td class="postcode">E3</td>
        <td class="price" data-price='4'>4</span></td>
        <td class="rating" data-rating='89'>89</td>
        <td class="impress" data-impress='5'>5</td>
      
      </tr>
      <tr data-link="/places/3">
        <td>Name of place 3</td>
        <td class="postcode">SW3</td>
        <td class="price" data-price='2'>2</td>
        <td class="rating" data-rating='51'>51</td>
        <td class="impress" data-impress='3'>3</td>
      </tr>
    </tbody>
</table>

代码可能不是最有效的,但它可以工作:)。完成后,我想添加更多的滤镜

(抱歉我的英文不好)

如果你只需要这些过滤器,你可以使用两种不同类型的过滤器:

    通过Css隐藏

如果你使用两种类型的过滤器,过滤器可以正常工作,而不需要使用复杂的javascript代码来管理大量不同的情况和组合:

我添加了一个初始(在文档加载时)控件,检查tr是否具有值impress cell>4,如果有则添加一个新类:is_impress,否则添加另一个类:no_impress

$('.table td.impress').each(function(){
    var _class = ($(this).data("impress") >= 4) ? "is_impress" : "no_impress";
    $(this).parent().addClass(_class);
});

按位置过滤的代码相同…但是…我通过impress编辑过滤器,以便在活动时向table()添加一个类,并在非活动时将其关闭:

$("input[name='impressStatus']").change(function(){
    (!$(this).is(":checked"))
        ? $("#StatusTable").removeClass("active_impress")
        : $("#StatusTable").addClass("active_impress");
});

如果表有active_impress类,则css规则覆盖dispaly的内联代码以隐藏所有没有impress的行>4:

#StatusTable.active_impress tr.no_impress{
    display:none !important;
}

这种类型的过滤器覆盖任何其他显示修改,直到复选框保持选中状态。

我编辑你的小提琴:https://jsfiddle.net/Frogmouth/gkba343L/1/

使用CSS来过滤

首先更改负载检查,添加price:

$('.table tbody tr').each(function(){
    var _class = "";
    _class += ($(this).find(".price").data("price") >= 2) ? "is_price " : "no_price ";
    _class += ($(this).find(".impress").data("impress") >= 4) ? "is_impress " : "no_impress ";
    console.log(_class);
    $(this).addClass(_class);
});

添加另一个处理程序到新的过滤器:

$("input[name='priceStatus']").change(function(){
    (!$(this).is(":checked"))
        ? $("#StatusTable").removeClass("active_price")
        : $("#StatusTable").addClass("active_price");
});

为CSS规则添加新的选择器:

#StatusTable.active_impress tr.no_impress,
#StatusTable.active_price tr.no_price{
    display:none !important;
}

结果如下:https://jsfiddle.net/Frogmouth/gkba343L/3/

优化代码以添加更多过滤器:

HTML过滤器按钮:

<label class="btn btn-primary outline">
    <input type="checkbox" name="impress" class="cssFilter radio" aria-pressed="true" autocomplete="off">Impress her
</label>

使用cssFilter来表示这是一个css过滤器按钮,并使用name属性来定义过滤器的名称,然后在css类中使用这个命名空间:

.active_{name} .no_{name} .is_{name}

并使用通用处理程序:

$("input.cssFilter").change(function(){
    var _name = $(this).attr("name");
    console.log(_name);
    (!$(this).is(":checked"))
        ? $("#StatusTable").removeClass("active_"+_name)
        : $("#StatusTable").addClass("active_"+_name);
});

有了这个,你可以用一个唯一的处理程序来管理所有的过滤器,记住为每个新的过滤器添加onload检查和新的选择器。

小提琴:https://jsfiddle.net/Frogmouth/gkba343L/4/