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. So I have the following in my Div;
HTML Result
<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.
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
$('.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. 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.
$(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. 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. Same issue with putting the dropdown to 'All' instead of a desired result, it then shows nothing on the page.