Alexko64,
<script>
function escapeRegExp(string){
return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
}
function fnSearch(arr, pattern) {
pattern = escapeRegExp(pattern);
pattern = new RegExp("^(" + pattern.trim().replace(/\s+/g, "|") + ")", "i");
return arr.filter(function(el) {
return Object.values(el).some(function(val) {
return pattern.test(val)
})
})
};
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();
</script>