Zdravo svima. :) Početnik sam i možda sam zagrizao više nego što mogu da progutam. Trudim se da napravim neki vid online shop-a i nešto je zapelo oko stranice gde su mi izlistani produkti. Produkte izvlačim iz baze putem AJAX-a, i nekoliko funkcija obrađuje to što stigne. Dobar deo stvari sam sredio ali ne mogu da dovedem u red pagination. Skinuo sam par skripti ali jedino što je pokazivalo znake života je nešto što sam pokupio preko jednog tutorijala na yt. Pagination sam vezao za AJAX poziv, to jest za funkciju koja filtrira i sortira podatke dobijene preko AJAX-a. Ova konkretna verzija pagination skripte sve izlistane produkte podeli produkte po "stranicama" tako što neke sakrije a neke otkrije ( jQuery show/hide). Kada se otvori zadata stranica, brojevi stranica se vide ali se vide i svi produkti što ne bi trebalo da se desi. Kada se klikne, npr. na 1, on sakrije sve osim prve dve (ili više) stranice, tj. tad skripta profunkcioniše. Ne znam u čemu je problem. Ostaviću deo koda ako neko može da me barem usmeri u logici....
Code:
function checkedFilters(){
var opts = [];
$checkboxes.each(function(){
if(this.checked){
opts.push(this.id);
}
});
return opts;
}
var $checkboxes = $("input:checkbox");
$checkboxes.on("change", function(){
var opts = checkedFilters();
var $orderBy = $("#by-price").val();
fetchData(opts, $orderBy);
});
$checkboxes.trigger("change");
$("#by-price").on("change", function() {
var opts = checkedFilters();
var $orderBy = $("#by-price").val();
fetchData(opts, $orderBy);
});
function fetchData (opts, $orderBy){
$.ajax({
type: 'post',
url: 'submit.php',
dataType : 'json',
cache: false,
success: function(records){
globalContent(jQuery.parseJSON(formatData(records)), opts, $orderBy);
}
});
}
Array.prototype.contains = function(obj) {
var i = this.length;
while (i--) {
if (this[i] == obj) {
return true;
}
}
return false;
};
function globalContent (result, opts, $orderBy) {
var orderBy = $orderBy;
var results = [];
var filterOpt = opts;
for (var x = 0; x < result.length; x++) {
for (var i = 0; i < filterOpt.length; i++) {
if (result[x].colors[i] !== null) {
if (result[x].colors.contains(filterOpt[i]) || filterOpt[i] === result[x].brand || filterOpt[i] === result[x].format) {
if (results.contains(result[x]) === false) {
results.push(result[x]);
}
}
}
}
}
if (results.length == 0) {
if (orderBy == 1) {
result.sort(function (a, b) {
return parseInt(a.price, 10) - parseInt(b.price, 10);
});
$('#products-section').html(makeContent(result));
} else {
result.sort(function (a, b) {
return parseInt(b.price, 10) - parseInt(a.price, 10);
});
$('#products-section').html(makeContent(result));
}
}
else {
if (orderBy == 1) {
results.sort(function (a, b) {
return parseInt(a.price, 10) - parseInt(b.price, 10);
});
$('#products-section').html(makeContent(results));
} else {
results.sort(function (a, b) {
return parseInt(b.price, 10) - parseInt(a.price, 10);
});
$('#products-section').html(makeContent(results));
}
}
$(".paginate").trigger("custom");
}
$(".paginate").on("custom", function () {
$(".paginate").customPagination({
itemsForPage : ".product",
itemsPerPage : 2
});
$(".paginate ul:not(:first)").remove();
});
function checkedFilters(){
var opts = [];
$checkboxes.each(function(){
if(this.checked){
opts.push(this.id);
}
});
return opts;
}
var $checkboxes = $("input:checkbox");
$checkboxes.on("change", function(){
var opts = checkedFilters();
var $orderBy = $("#by-price").val();
fetchData(opts, $orderBy);
});
$checkboxes.trigger("change");
$("#by-price").on("change", function() {
var opts = checkedFilters();
var $orderBy = $("#by-price").val();
fetchData(opts, $orderBy);
});
function fetchData (opts, $orderBy){
$.ajax({
type: 'post',
url: 'submit.php',
dataType : 'json',
cache: false,
success: function(records){
globalContent(jQuery.parseJSON(formatData(records)), opts, $orderBy);
}
});
}
Array.prototype.contains = function(obj) {
var i = this.length;
while (i--) {
if (this[i] == obj) {
return true;
}
}
return false;
};
function globalContent (result, opts, $orderBy) {
var orderBy = $orderBy;
var results = [];
var filterOpt = opts;
for (var x = 0; x < result.length; x++) {
for (var i = 0; i < filterOpt.length; i++) {
if (result[x].colors[i] !== null) {
if (result[x].colors.contains(filterOpt[i]) || filterOpt[i] === result[x].brand || filterOpt[i] === result[x].format) {
if (results.contains(result[x]) === false) {
results.push(result[x]);
}
}
}
}
}
if (results.length == 0) {
if (orderBy == 1) {
result.sort(function (a, b) {
return parseInt(a.price, 10) - parseInt(b.price, 10);
});
$('#products-section').html(makeContent(result));
} else {
result.sort(function (a, b) {
return parseInt(b.price, 10) - parseInt(a.price, 10);
});
$('#products-section').html(makeContent(result));
}
}
else {
if (orderBy == 1) {
results.sort(function (a, b) {
return parseInt(a.price, 10) - parseInt(b.price, 10);
});
$('#products-section').html(makeContent(results));
} else {
results.sort(function (a, b) {
return parseInt(b.price, 10) - parseInt(a.price, 10);
});
$('#products-section').html(makeContent(results));
}
}
$(".paginate").trigger("custom");
}
$(".paginate").on("custom", function () {
$(".paginate").customPagination({
itemsForPage : ".product",
itemsPerPage : 2
});
$(".paginate ul:not(:first)").remove();
});
Potpis
"Stop crying Eddie, cuz you can get the fuck out."
"You're gettin' the fuck...
I know you're seven !"
"But you'll be a seven year old
walkin' the dog no house motherfucker !"
"You're gettin' the fuck...
I know you're seven !"
"But you'll be a seven year old
walkin' the dog no house motherfucker !"