简体   繁体   English

JQuery - 使用不同输入的多个过滤器过滤数据属性

[英]JQuery - Filtering data attributes with multiple filters of different inputs

I am working on a project where by I want to use JQuery in order to filter on data variables embedded onto divs that are on the page, its sort of like a showcase in which the users will be able to filter in various ways. 我正在开发一个项目,我希望使用JQuery来过滤嵌入到页面上的div上的数据变量,它就像一个展示用户将能够以各种方式过滤的展示。 So I have the following in my Div; 所以我的Div中有以下内容;

HTML Result HTML结果

<div class="box col-xs-6 col-sm-4 col-md-3 col-lg-2" 
    data-remote-name="BFT Mitto B RCB - 2 Button Remote" 
    data-remote-model="Mitto B RCB 2" 
    data-remote-freq="433" 
    data-remote-dips="" 
    data-remote-clone="" style="display: block;">

Data is pulled from an SQL table and hundreds of these are generated wit relevant data. 数据从SQL表中提取,其中数百个是通过相关数据生成的。

I got the following for working with the name alone, but I can't seem to figure out a way to have it filter on all the other inputs, as well as have the name and model looked at with this one input. 我单独使用这个名称得到了以下内容,但我似乎无法找到一种方法来过滤所有其他输入,以及使用这一个输入查看名称和模型。

JS/Query JS /查询

$('.box').hide().filter(function() {
    regExName = new RegExp($('#search-name').val().trim(), "ig");
    regExModel = new RegExp($('#search-name').val().trim(), "ig");
    return $(this).data("remote-name").match(regExName);
}).show();

This is then towards the top of the page and where the filtering criteria is. 然后,这将是页面顶部和过滤条件所在的位置。 It currently contains 1 text input, 2 check boxes and 2 dropdowns. 它目前包含1个文本输入,2个复选框和2个下拉列表。 There will be many, many more filters when done. 完成后会有更多,更多的过滤器。

  <div role="form" name="filters-form" id="filters-form">
<div class="col-sm-12">
  <input class="form-control" id="search-name" placeholder="Search for name or model..."/>
</div>

<div class="col-sm-3 col-xs-6">
  <div class="checkbox ">
    <label>
      <input type="checkbox" id="dispwitch" value="dispwitch">
      Has Dip Switches?
    </label>
  </div>
  <div class="checkbox">
    <label>
      <input type="checkbox" id="cloneable" value="cloneable">
      Cloneable?
    </label>
  </div>
</div>  

<div class="col-sm-3 col-xs-6">
  Frequency:
  <select class="freq-dropdown form-control" id="frequency" style="width:100%;">
    <option value="all">All</option>
    <?php foreach ($this->frequencies AS $frequency) {
      echo "<option value=\"" . $frequency->frequency . "\">" . $frequency->frequency . "</option>";
    }?>
  </select>
</div>

<div class="col-sm-3 col-xs-6">
  Manufacturer:
  <select class="manufacturer-dropdown form-control" id="manufacturer" style="width:100%;">
    <option value="all">All</option>
    <?php foreach ($this->manufacturers AS $manufacturer) {
      echo "<option value=\"" . $manufacturer->name . "\">" . $manufacturer->name . "</option>";
    }?>
  </select>
</div>

<div class="col-sm-3 col-xs-6">

</div>

Any help would be greatly appreciated. 任何帮助将不胜感激。

EDIT: Below is my current JS file as well as the output of 'data' which is an object i use to store states of filters. 编辑:下面是我当前的JS文件以及'data'的输出,'data'是我用来存储过滤器状态的对象。

$(function() {
$('.nav > li#remotes').toggleClass('active');

var data = { dips: null, cloneable: null, freq: null, manufacturer: null }; 

$("#filters-form").on("change keyup paste", function(){
    if ($('#dispwitch').is(':checked')) { data.dips = "1"; } else { data.dips = "0"; }
    if ($('#cloneable').is(':checked')) { data.cloneable = "1"; } else { data.cloneable = "0"; }
    data.freq = $('#frequency').val();
    data.manufacturer = $('#manufacturer').val();

    $('.box').hide().filter(function() {
        regExName = new RegExp($('#search-name').val().trim(), "ig");
        regExModel = new RegExp($('#search-name').val().trim(), "ig");
        return $(this).data("remote-name").match(regExName); 
        //I took out the previous answers changes, this currently works filtering via name.
    }).show();
    console.log(data);
}); 

}); });

EDIT2: Another thing of note is some of the values are true/false/null so that needs to be accounted for in the filters. EDIT2:另一个需要注意的是一些值为true / false / null,因此需要在过滤器中考虑。 For example I got the check box filters semi-working with the following; 例如,我使用以下复选框过滤器半工作;

return $(this).data("remote-name").match(regExName) && $(this).data("remote-clone") == data.cloneable && $(this).data("remote-dips") == data.dips;

BUT... For those which have [data-remote-dips=""] as oppose to 1 or 0, once the filter is turned on or off a single time, the null valued are never shown until a refresh. 但是......对于那些[data-remote-dips=""]与1或0相反的情况,一旦滤波器打开或关闭一次,就会在刷新之前显示空值。 Same issue with putting the dropdown to 'All' instead of a desired result, it then shows nothing on the page. 将下拉列表设置为“全部”而不是所需结果的问题相同,它在页面上不显示任何内容。

So after half a day of playing with JSFiddle I managed to get it working as I wanted, using RegExp as @David Johnson had menntioned. 因此,在玩了半天的JSFiddle后,我设法让它按照我的意愿运行,使用RegExp作为@David Johnson提到过。

https://jsfiddle.net/JokerDan/tqv0ybbz/2/ https://jsfiddle.net/JokerDan/tqv0ybbz/2/

Working HTML 正在使用HTML

<div class="status"></div>
<div id="filterDiv">
  <input type="text" class="myInput" id="0"/>

  <select class="mySel" id="1">
    <option value="">All</option>
    <option value="a">A</option>
    <option value="b">B</option>
    <option value="c">C</option>
  </select>

  <select class="mySel" id="2">
    <option value="">All</option>
    <option value="123">123</option>
    <option value="231">231</option>
    <option value="321">321</option>
  </select>

  <input type="checkbox" id="3"> Test Data :: 1 | 0 | NULL
</div>
<p class="p a 123" data-name="apple" data-a="a" data-b="123" data-test="1">Apple A 123 1</p>
<p class="p b 123" data-name="banana" data-a="b" data-b="123" data-test="0">Banana B 123 0</p>
<p class="p c 321" data-name="cherry" data-a="c" data-b="321" data-test="">Cherry C 321 X</p>
<p class="p a 321" data-name="date" data-a="a" data-b="321" data-test=""> Date A 321 X</p>
<p class="p a 123" data-name="elderberry" data-a="a" data-b="123" data-test="1">Elderberry A 123 1</p>
<p class="p c 231" data-name="fig" data-a="c" data-b="231" data-test="1">Fig C 231 1</p>

Working JS 工作JS

$('#filterDiv').on("change keyup", function() {
  chkBox = { datatest: null };

  if ($('#3').is(':checked')) { chkBox.datatest = "1"; } else { chkBox.datatest = ""; }

  $("p").hide().filter(function() {
    var rtnData = "";

    regExName   = new RegExp($('#0').val().trim(), "ig");
    regExA          = new RegExp($('#1').val().trim(), "ig");
    regExB          = new RegExp($('#2').val().trim(), "ig");
    regExTest       = new RegExp(chkBox.datatest, "ig")

    rtnData = (
      $(this).attr("data-name").match(regExName) && 
      $(this).attr("data-a").match(regExA) && 
      $(this).attr("data-b").match(regExB) &&
      $(this).attr("data-test").match(regExTest)
    );

    //console.log(rtnData);
    return rtnData;
  }).show();
});

Don't you just need to extend the condition like this: 你不需要像这样扩展条件:

freq = new RegExp($('#frequency').val().trim(), "ig"); 
dips= new RegExp($('#dispwitch').val().trim(), "ig");
clone= new RegExp($('#cloneable').val().trim(), "ig");

return $(this).data("remote-name").match(regExName) || $(this).data("remote-freq").match(freq) || $(this).data("remote-dips").match(dips) || $(this).data("remote-clone").match(clone) 

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

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