JQuery нумерация страниц с переключателями = проблемы - PullRequest
0 голосов
/ 20 ноября 2011

Отсутствие ответов, которые я получаю, заставляет меня поверить, что это невозможно ...

Итак, две простые концепции:

1) Нажатие кнопки, назначенной сообщению, переключит ВСЕpost (title + content), заставляя его исчезнуть.

2) Моя нумерация страниц принимает все div с помощью class = "z" и разбивает их на страницы (отображает 10 div на страницу).

Моя проблемачто если я на странице 1 и переключаю (например: переключать 3 сообщения) сообщения, на странице не будет постоянных 10 сообщений на странице.Вместо этого он оставит 7 постов на странице 1.

То, что я хочу, чтобы это произошло, это то, что при х переключенном количестве постов следующее х постов сместится вверх (верхний х постов на странице2 появляются внизу страницы 1 и т. Д.).

Я знаю, что это можно сделать ... Я просто не знаю, как!

toggle.js (ищет divс class = "toggle", который при нажатии отправит некоторый ajax и при успехе переключит div)

$(document).on("click", ".toggle", function(){
postID = $(this).attr('id').replace('toggle_', '');

// Declare variables
value = '0';

myajax();

return false;
});

function myajax(){
// Send values to database
$.ajax({
    url: 'check.php',
    //check.php receives the values sent to it and stores them in the database
    type: 'POST',
    data: 'postID=' + postID + '&value=' + value,
    success: function(result) {
         $('#post_' + postID).toggle();
                }
});
}

pagination.js

var Imtech = {};
Imtech.Pager = function() {
this.paragraphsPerPage = 3;
this.currentPage = 1;
this.pagingControlsContainer = '#pagingControls';
this.pagingContainerPath = '#contained';
this.numPages = function() {
var numPages = 0;
if (this.paragraphs != null && this.paragraphsPerPage != null) {
    numPages = Math.ceil(this.paragraphs.length / this.paragraphsPerPage);
}
return numPages;
};
this.showPage = function(page) {
this.currentPage = page;
var html = '';
this.paragraphs.slice((page-1) * this.paragraphsPerPage,
    ((page-1)*this.paragraphsPerPage) + this.paragraphsPerPage).each(function() {
    html += '<div>' + $(this).html() + '</div>';
});
$(this.pagingContainerPath).html(html);
renderControls(this.pagingControlsContainer, this.currentPage, this.numPages());
}
var renderControls = function(container, currentPage, numPages) {
var pagingControls = 'Page: <ul>';
for (var i = 1; i <= numPages; i++) {
    if (i != currentPage) {
        pagingControls += '<li><a href="#" onclick="pager.showPage(' + i + '); return false;">' + i + '</a></li>';
    } else {
        pagingControls += '<li>' + i + '</li>';
    }
}
pagingControls += '</ul>';
$(container).html(pagingControls);
}
}

1 Ответ

0 голосов
/ 05 декабря 2013

Используйте алгоритм, основанный на делении и модуле деления, такой как:

1. Read the page number requested from the user

2. Identify how many total records exist

3. Calculate the index of the last page

4. Check whether the requested that page number is valid

5. Calculate the range of records needed for the requested page

6. Create a subset of records based on the calculated range

7. Output the records along with updated hyperlinks
...