i have this jsfiddle http://jsfiddle.net/danieltulp/gz5gN/
what i want to do is fiter a unordered list on data- properties with about 6 sliders. but for my development i'm just using two
first question: the quality slider works just fine, but when i slide the price slider it sees the minP and maxP variables as objects, why? how can i fix that?
second question: my code is now pritty messy, i have to write code specific to each filter (ie: price, quality, etc), how can i simplify/shorten my code?
Code from fiddle:
Html
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"o/>
<div class="demo">
price<br />
<div id="price"></div>
quality<br />
<div id="quality"></div>
<ul id="products">
<li data-price="10" data-quality="20"> product - £10 q20</li>
<li data-price="50" data-quality="40"> product - £50 q40</li>
<li data-price="100" data-quality="80"> product - £100 q80</li>
<li data-price="150" data-quality="30"> product - £150 q30</li>
<li data-price="200" data-quality="40"> product - £200 q40</li>
</ul>
</div>
Javascript
function showProducts(minP, maxP, minQ, maxQ) {
$("#products li").filter(function() {
var price = parseInt($(this).data("price"), 10);
var quality = parseInt($(this).data("quality"), 10);
if(price >= minP && price <= maxP && quality >= minQ && quality <= maxQ){
$(this).show();
} else {
$(this).hide();
}
});
}
$(function() {
var options = {
range: true,
min: 0,
max: 250,
values: [0, 250],
slide: function(event, ui) {
if(event.target.id = "price"){
var minP = ui.values[0],
maxP = ui.values[1],
minQ = $("#quality").slider("values", 0),
maxQ = $("#quality").slider("values", 1);
}
if(event.target.id = "quality"){
var minP = $("#price").slider("values", 0),
maxP = $("#price").slider("values", 1),
minQ = ui.values[0],
maxQ = ui.values[1];
}
alert(minP +", "+ maxP +", "+ minQ +", "+ maxQ);
showProducts(minP, maxP, minQ, maxQ);
}
};
$("#price").slider(options);
$("#quality").slider(options);
});
Edit: The first problem with the min max variables being objects also happens to the quality slider if I move the if statement of quality above the one for price. Price values are then correct. So it appears to be a problem with the if statements in the slide function.
it appears there is a very handy jquery plugin called filter.js
demo: http://jiren.github.com/filter.js/filterjs.html