简体   繁体   English

使用JQuery中的复选框进行过滤时,如何在数据属性的完整字符串中进行搜索,而不仅仅是完全匹配?

[英]When filtering with checkboxes in JQuery, how to search within a data-attribute's full string, rather than just an exact match?

I'm working on filtering elements via data-attributes with checkboxes. 我正在通过带有复选框的数据属性过滤元素。 So far it's working by looking for blank checkboxes, pairing those with their corresponding element via data-attributes and only showing the others ones (ie the elements associated with a checked box). 到目前为止,它的工作方式是查找空白复选框,通过数据属性将其与相应元素配对,并仅显示其他复选框(即与复选框关联的元素)。

I have three categories. 我分为三类。

  • Regions: APAC, EMEA, Americas (always will be one and only one of these) 地区:亚太地区,欧洲,中东,非洲,美洲(永远是其中一个,并且仅是其中之一)

    Type: Elite, Preferred, Authorized (always will be one and only one of these) 类型:精英,首选,授权(永远是其中之一,并且仅是其中之一)

    Countries: Argentina, Brazil, Mexico (can have more than one of these) 国家:阿根廷,巴西,墨西哥(可以有多个)

The code I have so far in the Fiddle below works fine for the Regions and Types, because it's pretty straightforward when there's only one possibility. 到目前为止,我在下面的小提琴中提供的代码对于Regions和Types都适用,因为只有一种可能性时,它非常简单。

However, the problem arises with Countries when there are more than one. 但是,存在多个国家的国家就会出现问题。

For example, in the Fiddle, if you uncheck Brazil, the gray one with only Brazil as its country goes away (and comes back if you check it). 例如,在Fiddle中,如果您取消选中“巴西”,则灰色国家(仅巴西作为其国家)就会消失(如果选中,则会返回)。 That's great. 那很棒。

But if you uncheck Argentina AND Brazil, the blue one with "Argentina, Brazil" does NOT go away. 但是,如果您取消选中阿根廷和巴西,则带有“阿根廷,巴西”的蓝色图标不会消失。

Is there a way to update the JQuery below to handle this? 有没有一种方法可以更新下面的JQuery来解决此问题? In essence, it needs to look for the country ANYWHERE in the full data-country string, not just look for an exact match. 本质上,它需要在完整的数据国家/地区字符串中寻找ANYWHERE,而不仅仅是寻找完全匹配的国家/地区。

This seems incredibly complicated to me, but hoping someone can steer me in the right direction. 这对我来说似乎非常复杂,但是希望有人可以将我引向正确的方向。

https://jsfiddle.net/2xa1Lpet/9/ https://jsfiddle.net/2xa1Lpet/9/

HTML: HTML:

<div class="Row" style="background: #eeeeee;" data-region="Americas" data-country="Brazil" data-tier="Elite Reseller">
<div class="Heading">Allegiant Technology</div>
<div class="Copy">Brazil</div>
<div class="Copy">Elite Reseller</div>
</div>

<div class="Row" style="background: red;" data-region="Americas" data-country="Mexico" data-tier="Preferred Reseller">
<div class="Heading">Folco Communications</div>
<div class="Copy">Mexico</div>
<div class="Copy">Preferred Reseller</div>
</div>
<div class="Row" style="background: blue;" data-region="Americas" data-country="Argentina, Mexico, Brazil" data-tier="Authorized Reseller">
<div class="Heading">Latin Telecom</div>
<div class="Copy">Argentina; Mexico; Brazil</div>
<div class="Copy">Authorized Reseller</div>
</div>


<div style="text-align:left; max-width: 1000px;  margin-left: auto;  margin-right: auto;  padding-left: 50px;">
<span class="title">Region:</span><br>
<input class="css-checkbox" type="checkbox" id="APAC" data-type="region" data-value="APAC" checked> 
<label for="APAC" class="css-label">APAC</label>
<input class="css-checkbox" type="checkbox" id="EMEA" data-type="region" data-value="EMEA" checked>
<label for="EMEA" class="css-label">EMEA</label>
<input class="css-checkbox" type="checkbox" id="Americas" data-type="region" data-value="Americas" checked>
<label for="Americas" class="css-label">Americas</label><br><br>
<span class="title">Partner Type:</span><br>
<input class="css-checkbox" type="checkbox" id="Preferred" data-type='tier'  data-value='Preferred Reseller' checked>
<label for="Preferred" class="css-label">Preferred</label>
<input class="css-checkbox" type="checkbox" id="Elite" data-type='tier'  data-value='Elite Reseller' checked>
<label for="Elite" class="css-label">Elite</label>
<input class="css-checkbox" type="checkbox" id="Authorized" data-type='tier'  data-value='Authorized Reseller' checked>
<label for="Authorized" class="css-label">Authorized</label>
<br>
<br>
<span class="title">Country:</span><br>
<input class="css-checkbox" type="checkbox" id="Argentina" data-type='country'  data-value='Argentina' checked>
<label for="Argentina" class="css-label">Argentina</label>
<input class="css-checkbox" type="checkbox" id="Brazil" data-type='country'  data-value='Brazil' checked>
<label for="Brazil" class="css-label">Brazil</label>
<input class="css-checkbox" type="checkbox" id="Mexico" data-type='country'  data-value='Mexico' checked>
<label for="Mexico" class="css-label">Mexico</label>
</div>

JS: JS:

var $boxes = $('input[data-type]'), //all input boxes with data-type attribute
    $dataObjects =$(); //will be filled with all bound data elements
$boxes.each(function(ind, inp){     //create filter information
    var type = inp.dataset.type, value = inp.dataset.value; //for older browsers, use  $(inp).data('type')  
  var filter =  'div[data-' + type +'="' + value +'"]';     
  inp.dataset.filter = filter;
    $.merge($dataObjects,$(filter));
}); 

$boxes.change(function(){
    var blacklist = $boxes.filter(function(i,b){return !b.checked})
    .map(function(i,b){return b.dataset.filter}).toArray().join();
  $dataObjects.hide().not(blacklist).show();
});

Attribute Contains Selector [name*=”value”]

 jQuery( "[data-foo*='food']" ).addClass("sel") 
 .sel {background-color: yellow; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div data-foo="food">food</div> <div data-foo="beer">food</div> <div data-foo="beer, food">beer, food</div> <div data-foo="beer, food, wine">beer, food, wine</div> <div data-foo="beer, wine">beer, wine</div> 

And for your other issue.... 还有其他问题...

$('[type="checkbox"]').on('change', function () {
  $('[data-foo].sel).removeClass('sel'); // remove selections
  $('[type="checkbox"]:checked').each( function () { //loop over checked checkboxes
    var value = inp.dataset.value; //not sure why you are not using just value...
    $('[data-foo*="' + value + '"]').addClass('sel'); // find the attribute
  })
})

$('[type="checkbox"]').on('change', function() {
  $('div.actual').removeClass('actual');
  $('[type="checkbox"]:checked').each(function() {
    var inp = this
    var type = inp.dataset.type,
    value = inp.dataset.value;
    $('div[data-' + type + '*="' + value + '"]').addClass("actual");
  })
}).change();

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

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