Alexko64,
function fnSearch(arr, pattern) {
return arr.filter(function(product) {
return pattern
.trim()
.toLowerCase()
.split(/\s+/)
.every(function(p) {
return product.name.toLowerCase().includes(p)
});
});
}
var products = [
{name:'Samsung Galaxy s9',
html:'<div>Samsung Galaxy S9</div>'
},
{name:'Galaxy S9 Samsung',
html:'<div>Galaxy S9 Samsung</div>'
},
];
</script>
<script>
var SLS = {
products: $('#products'),
plow:$('#plow'),
phigh:$('#phigh'),
isearch:$('#isearch'),
prod:products,
sproducts: products,
page:1,
limit:12,
isort:0,
init:function(){
SLS.pload(SLS.page,SLS.limit);
SLS.events();
},
pload:function(page,limit){
for(var i=((page-1)*limit);i<(limit*page);i++){
if(SLS.prod[i])
SLS.products.append($(SLS.prod[i].html));
}
},
sorta:function(type){
var byPrice = SLS.prod.slice(0);
byPrice.sort(function(a,b) {
return a.price - b.price;
});
if(type==1)
byPrice=byPrice.reverse();
SLS.prod=byPrice;
SLS.page=1;
SLS.products.html('');
SLS.pload(SLS.page,SLS.limit);
},
events:function(){
$(window).on('scroll',function(){
if($(this).scrollTop()+250>=($(document).height() - window.innerHeight-$('footer').height())){
SLS.page++;
SLS.pload(SLS.page,SLS.limit);
}
});
SLS.plow.on('click',function(){
SLS.isort=0;
SLS.sorta(SLS.isort);
});
SLS.phigh.on('click',function(){
SLS.isort=1;
SLS.sorta(SLS.isort);
});
SLS.isearch.on('keyup',function(){
var results = [];
var toSearch = SLS.isearch.val();
if(toSearch=='') {
SLS.prod = products;
}else{
SLS.prod = fnSearch(SLS.sproducts, toSearch);
}
SLS.page=1;
SLS.products.html('');
SLS.sorta(SLS.isort);
});
}
};
SLS.init();