Js分页错误

时间:2011-02-18 14:47:11

标签: javascript ajax

我需要执行分页,每次我需要显示5行。显示前5行。在第二次单击时,下一个5,第三行没有。 11-15,依此类推。我需要显示即使至少有一行(在n次点击后,totalRows mod 5小于5)。我不懂。

function rightArrow()
{   
        pageCount = document.getElementById('pgCnt').value; //totalRows / 5
        totCount = document.getElementById('totCnt').value; //totalRows
        currPage = tempRows - pageCount + 2; //current set of rows
        document.getElementById('tempPage').value = tempPage;
        var m = totCount%5;

        if(pageCount != tempRows)
            m = 5;
        else
        {   m = (totCount % 5) - 1; 
            document.getElementById('rightArr').disabled = true; 
        }

        document.getElementById('pgCnt').value = document.getElementById('pgCnt').value - 1;

        for(i = 0; i < m;i++)
        {
            $.ajax({    
                type: "POST",
                url: "getEODRow.php",
                data: "page=" + currPage + "&row=" + i,
                success: function(html)
                {
                    var row = document.getElementById('chRecommend').insertRow(0);
                    temp = html.split(",");
                    for(j = 0; j < 9; j++)
                    {
                        str = temp[j].replace("\"","");
                        str = temp[j].replace("\"",'');
                        str = temp[j].replace("[",'');
                        col = row.insertCell(j);
                        col.innerHTML = str;
                    }
                }
            });
        }
        currPage++;
}

1 个答案:

答案 0 :(得分:0)

我意识到这个问题已经死了,但我偶然发现了其他东西,我想我会给它一个答案。由于它不是一个关键问题,我继续重新设计了你在这里得到的所有内容以及你没有选择包含的大量代码。

// App namespace
var MyApp = {Paging: {}};

// Paging Controller
MyApp.Paging.Controller = function(){ this.init.apply(this,arguments);};
MyApp.Paging.Controller.prototype =
{
    // Initializer gets everything hooked up
    init: function()
    {
        $.log("Initializing Paging Controller.")
        // Get all the nodes we'll need
        this.totCntNode = document.getElementById("totCnt");
        this.pgCntNode = document.getElementById("pgCnt");
        this.currMinNode = document.getElementById("currMin");
        this.currMaxNode = document.getElementById("currMax");
        this.prevPageButton = document.getElementById("prevPageButton");
        this.nextPageButton = document.getElementById("nextPageButton");

        this.pageContainer = document.getElementById("pageOfItems");
        // Mimic table's .insertRow to make row adding easy
        this.pageContainer.insertRow = function() {
            var row = document.createElement("div");
            row.className = "row clearfix";
            for (var i = 0; i < MyApp.Paging.Model.itemsPerRow; i++)
            {
                var cell = document.createElement("span");
                row.appendChild(cell);
            }
            this.appendChild(row);
            return row;
        };

        // Attach listeners to the next and previous buttons
        this.prevPageButton.onclick = this.showPrevPage.bind(this);
        this.nextPageButton.onclick = this.showNextPage.bind(this);

        // Update the display for the first time
        this.updatePageInfo();
    },

    // Run this whenever the model has changed and needs to update the display
    updatePageInfo: function()
    {
        // Get info about what page we're on
        var currentPage = MyApp.Paging.Model.currentPage,
            totalPages = MyApp.Paging.Model.getTotalPages(),
            itemCount = MyApp.Paging.Model.itemCount,
            pageSize = MyApp.Paging.Model.getPageSize(),
            rowsPerPage = MyApp.Paging.Model.rowsPerPage,
            rowsOnThisPage = Math.ceil(MyApp.Paging.Model.getItemsOnPage(currentPage)/MyApp.Paging.Model.itemsPerRow);

        // Clear out previous page data
        while (this.pageContainer.children.length > 0)
        {
            this.pageContainer.removeChild(this.pageContainer.children[0]);
        }

        // Add space for the new page data
        for (var rowInd = 0; rowInd < rowsOnThisPage ; rowInd++)
        {
            this.pageContainer.insertRow();
        }

        $.log("Loading Page " + currentPage + ".");

        // Request the data via ajax for each row
        for(var i = 0; i < rowsOnThisPage ; i++)
        {
            $.ajax({    
                type: MyApp.Paging.Model.queryType,
                url: MyApp.Paging.Model.queryURI,
                data: MyApp.Paging.Model.getQueryData(currentPage, i),
                success: function(pageNum, rowNum, result)
                {
                    // Don't serve data from the wrong page
                    if (pageNum !== MyApp.Paging.Model.currentPage) return;

                    // When we get the data back, put it into the correct container
                    // regardless of when it was received
                    var row = this.pageContainer.children[rowNum],
                        temp = result.replace(/[\["]/g,"").split(","),
                        str = "";
                    for(var j = 0; j < temp.length; j++)
                    {
                        row.children[j].innerHTML = temp[j];
                    }
                }.bind(this, currentPage, i)
            });
        }

        // Update the informational bits under the items
        this.totCntNode.textContent = itemCount;
        this.pgCntNode.textContent = totalPages;
        var min = currentPage * (pageSize ) + 1;
        this.currMinNode.textContent = min;
        this.currMaxNode.textContent = Math.min(min + pageSize - 1, itemCount);

        // Disable the prev page button if there are no previous pages
        if (currentPage <= 0)
        {
            if (this.prevPageButton.className.indexOf("disabled") < 0)
            {
                this.prevPageButton.className += " disabled";
            }
        }
        // Enable the prev page button if there are previous pages
        else
        {
            if (this.prevPageButton.className.indexOf("disabled") > -1)
            {
                this.prevPageButton.className = this.prevPageButton.className.replace(/(?:^|\s+)disabled(?!\S)/g, "");
            }
        }

        // Disable the next page button if there are next pages
        if (currentPage + 1 >= totalPages)
        {
            if (this.nextPageButton.className.indexOf("disabled") < 0)
            {
                this.nextPageButton.className += " disabled";
            }
        }
        // Enable the next page button if there are next pages
        else
        {
            if (this.nextPageButton.className.indexOf("disabled") > -1)
            {
                this.nextPageButton.className = this.nextPageButton.className.replace(/(?:^|\s+)disabled(?!\S)/g, "");
            }
        }

    },

    // This is called when the next page button is clicked. 
    showNextPage: function()
    {
        if (MyApp.Paging.Model.currentPage + 1 >= MyApp.Paging.Model.getTotalPages())
        {
            // Shouldn't have been able to activate this anyway
        }
        else
        {
            MyApp.Paging.Model.currentPage++;

            this.updatePageInfo();
        }
        return false;
    },

    // This is called when the prev page button is clicked
    showPrevPage: function()
    {
        if (MyApp.Paging.Model.currentPage <= 0)
        {
            // Shouldn't have been able to activate this anyway
        }
        else
        {
            MyApp.Paging.Model.currentPage--;

            this.updatePageInfo();
        }
        return false;
    }
};

// I typically expect an object like this to be created by the server and dropped dynamically onto the page
MyApp.Paging.Model = {
    itemCount: 140,
    itemsPerRow: 9,
    rowsPerPage: 5,
    currentPage: 0,

    queryType: "POST",
    queryURI: "getEODRow.php",
    queryDataFormat: "page={itemPage}&row={itemRow}",

    getTotalPages: function() {
        with(MyApp.Paging.Model) {
            return Math.ceil(itemCount/(itemsPerRow*rowsPerPage));
        }
    },
    getPageSize: function() {
        with(MyApp.Paging.Model) {
            return itemsPerRow * rowsPerPage;
        }
    },
    getItemsOnPage: function(pageNum) {
        with(MyApp.Paging.Model) {
            return Math.min(((pageNum+1) * getPageSize()), itemCount) - pageNum*getPageSize();
        }
    },
    getItemsInRow: function(pageNum, rowNum) {
        with(MyApp.Paging.Model) {
            var onPage = getItemsOnPage(pageNum);
            return Math.min((rowNum+1)*itemsPerRow, onPage) - rowNum*itemsPerRow;
        }
    },
    getQueryData: function(itemPage, itemRow) {
        with(MyApp.Paging.Model) {
            var data = queryDataFormat;
            data = data.replace(/{itemPage}/gi, itemPage);
            data = data.replace(/{itemRow}/gi, itemRow);
            return data;
        }
    }
};

因此,无论何时加载页面,使用onload处理程序或其他任何内容,您都可以创建并挂起到分页控制器的单例实例。

MyApp.Paging.Controller.instance = new MyApp.Paging.Controller();

这应该处理Ajax调用无序返回的问题,以及这些页面中的部分数据页面和部分行。

演示:http://jsfiddle.net/2UJH8/8/