首页 新闻 会员 周边 捐助

jquery翻页

0
悬赏园豆:40 [已关闭问题] 关闭于 2013-06-26 17:43

自己做的一个JS读取XML文件 同时读取4个XML文件显示不同地方。。但是分页这里被难住了。。不知道怎么解决。。希望JS高手能帮帮我。 

function ZongHe() {
    var href = location.search;
    var findStr = "newsTypeID";
    var beginIndex = href.indexOf(findStr) + findStr.length + 1;
    var newsType = href.substr(beginIndex) ? href.substr(beginIndex) : 79;
    var newsObject = {
        xmlUrl: "../xml/GameNews_{0}.xml",
        newsType: newsType,
        newsTypeName: "综合",
        newsFormat: "<li><span class='time_news'>{0}</span><a href='javascript:;'>[{1}]</a>&nbsp;&nbsp;<a href='content.aspx?newsId={2}' target='_blank'>{3}</a></li>",

        pageIndex: 1,
        pageSize: 10
    };

    switch (parseInt(newsType)) {
        case 79: newsObject.newsTypeName = "综合";
            break;
        case 77: newsObject.newsTypeName = "新闻";
            break;
        case 76: newsObject.newsTypeName = "公告";
            break;
        case 62: newsObject.newsTypeName = "活动";
            break;
    }
    newsObject.xmlUrl = newsObject.xmlUrl.replace("{0}", newsType);
    loadNews(newsObject);
}

function NewsList() {
    var href = location.search;
    var findStr = "newsTypeID";
    var beginIndex = href.indexOf(findStr) + findStr.length + 1;
    var newsType = href.substr(beginIndex) ? href.substr(beginIndex) : 77;
    var newsObject = {
        xmlUrl: "../xml/GameNews_{0}.xml",
        newsType: newsType,
        newsTypeName: "综合",
        newsFormat: "<li><span class='time_news'>{0}</span><a href='javascript:;'>[{1}]</a>&nbsp;&nbsp;<a href='content.aspx?newsId={2}' target='_blank'>{3}</a></li>",

        pageIndex: 1,
        pageSize: 10
    };

    switch (parseInt(newsType)) {
        case 79: newsObject.newsTypeName = "综合";
            break;
        case 77: newsObject.newsTypeName = "新闻";
            break;
        case 76: newsObject.newsTypeName = "公告";
            break;
        case 62: newsObject.newsTypeName = "活动";
            break;
    }
    newsObject.xmlUrl = newsObject.xmlUrl.replace("{0}", newsType);
    loadNews(newsObject);
}

function GongGao() {
    var href = location.search;
    var findStr = "newsTypeID";
    var beginIndex = href.indexOf(findStr) + findStr.length + 1;
    var newsType = href.substr(beginIndex) ? href.substr(beginIndex) : 76;
    var newsObject = {
        xmlUrl: "../xml/GameNews_{0}.xml",
        newsType: newsType,
        newsTypeName: "综合",
        newsFormat: "<li><span class='time_news'>{0}</span><a href='javascript:;'>[{1}]</a>&nbsp;&nbsp;<a href='content.aspx?newsId={2}' target='_blank'>{3}</a></li>",

        pageIndex: 1,
        pageSize: 10
    };

    switch (parseInt(newsType)) {
        case 79: newsObject.newsTypeName = "综合";
            break;
        case 77: newsObject.newsTypeName = "新闻";
            break;
        case 76: newsObject.newsTypeName = "公告";
            break;
        case 62: newsObject.newsTypeName = "活动";
            break;
    }
    newsObject.xmlUrl = newsObject.xmlUrl.replace("{0}", newsType);
    loadNews(newsObject);
}

function HuoDong() {
    var href = location.search;
    var findStr = "newsTypeID";
    var beginIndex = href.indexOf(findStr) + findStr.length + 1;
    var newsType = href.substr(beginIndex) ? href.substr(beginIndex) : 62;
    var newsObject = {
        xmlUrl: "../xml/GameNews_{0}.xml",
        newsType: newsType,
        newsTypeName: "综合",
        newsFormat: "<li><span class='time_news'>{0}</span><a href='javascript:;'>[{1}]</a>&nbsp;&nbsp;<a href='content.aspx?newsId={2}' target='_blank'>{3}</a></li>",

        pageIndex: 1,
        pageSize: 10
    };

    switch (parseInt(newsType)) {
        case 79: newsObject.newsTypeName = "综合";
            break;
        case 77: newsObject.newsTypeName = "新闻";
            break;
        case 76: newsObject.newsTypeName = "公告";
            break;
        case 62: newsObject.newsTypeName = "活动";
            break;
    }
    newsObject.xmlUrl = newsObject.xmlUrl.replace("{0}", newsType);
    loadNews(newsObject);
}

function loadNews(newsObject) {
    $.get(newsObject.xmlUrl, function (xml) {
        var news = $.xml2json(xml).news;
        if ((!news) || news.length <= 0) {
            $(".list_news").empty().html("<p class='empty'>暂无内容!</p>");
            $(".area_page").empty();
            return false;
        }

        if (news.length == undefined) {
            news.length = 1;
        }

        var sumPages;
        var temp = parseInt(news.length / newsObject.pageSize, 10);
        var rest = news.length % newsObject.pageSize;



        if (rest == 0) {
            sumPages = temp;
        } else {
            sumPages = temp + 1;
        }
        var pageObject = {
            sumPage: sumPages,
            currPage: newsObject.pageIndex
        }
        if (newsObject.pageIndex == sumPages) {
            sumPages = temp + 1;
        }

        setPagerConfig(pageObject);

        var htmlContents = "";
        var newsType = "";
        if (news.length == 1) {
            var newsDateTime = news.date.substr(0, 10);
            var newsTitle = news.title;
            newsType = getCmsCategory(news.typename);
            //显示格式为yy-mm-dd
            var arr = newsDateTime.split('/');
            if (arr[0] < 10) {
                arr[0] = 0 + arr[0];
            }
            if (arr[1] < 10) {
                arr[1] = 0 + arr[1];
            }
            newsDateTime = arr[2] + '-' + arr[0] + '-' + arr[1];
            htmlContents += newsObject.newsFormat.replace("{0}", newsDateTime)
                                                           .replace("{1}", newsType)
                                                           .replace("{2}", news.id)
                                                           .replace("{3}", newsTitle.length > 22 ? newsTitle.substr(0, 22) : newsTitle);
        } else {
            var beginIndex = (newsObject.pageIndex - 1) * newsObject.pageSize;
            var endIndex = (newsObject.pageIndex == sumPages && rest < newsObject.pageSize) ? (beginIndex + rest) : (beginIndex + newsObject.pageSize);
            for (var i = beginIndex; i < endIndex; i++) {
                var newsRow = news[i];
                var newsDateTime = newsRow.date.substr(0, newsRow.date.indexOf(" "));
                //显示格式为yy-mm-dd
                var arr = newsDateTime.split('/');
                if (arr[0] < 10) {
                    arr[0] = 0 + arr[0];
                }
                if (arr[1] < 10) {
                    arr[1] = 0 + arr[1];
                }
                newsDateTime = arr[2] + '-' + arr[0] + '-' + arr[1];
                var newsTitle = newsRow.title;
                newsType = getCmsCategory(newsRow.typename);
                htmlContents += newsObject.newsFormat.replace("{0}", newsDateTime)
                                                           .replace("{1}", newsType)
                                                           .replace("{2}", newsRow.id)
                                                           .replace("{3}", newsTitle.length > 22 ? newsTitle.substr(0, 22) : newsTitle);
            }
        }
        if (newsObject.newsType == 79) {
            $("#Comprehensive").empty().html(htmlContents);
        }
        else if (newsObject.newsType == 77) {
            $("#News").empty().html(htmlContents);
        }
        else if (newsObject.newsType == 76) {
            $("#Notice").empty().html(htmlContents);
        }
        else if (newsObject.newsType == 62) {
            $("#Activity").empty().html(htmlContents);
        }

    });
}

// 分页设置
function setPagerConfig(pageObject) {
    var pageObjectDefault = {
        sumPage: 1,
        currPage: 1
    }

    $.extend(pageObjectDefault, pageObject);
    var tempcurrP = parseInt(pageObjectDefault.currPage, 10);
    var tempSumP = parseInt(pageObjectDefault.sumPage, 10);
    var pageNumDivHtml = "";


    var pagePre = "<a id='pagePre' title='上一页' onclick='changePage(" + (tempcurrP - 1) + ")' class='prev'></a>";
    var pageNxt = "<a id='pageNxt' title='下一页' onclick='changePage(" + (tempcurrP + 1) + ")' class='next'></a>";
    var pageNumBtn = "";

    if (tempcurrP == 1) {
        pagePre = "<a id='pagePre' title='上一页'  class='prev'></a>";
    }

    if (tempcurrP == tempSumP) {
        pageNxt = "<a id='pageNxt' title='下一页'  class='next'></a>";
    }

    for (var i = 1; i <= tempSumP; i++) {
        if (i == tempcurrP) {
            pageNumBtn += "<span class='num_page'>&nbsp;" + tempcurrP + " / " + tempSumP + "</span>";
        }
        //else {
        //    pageNumBtn += "<span class='num_page'></span>";
        //}
    }
    pageNumDivHtml = pagePre + pageNumBtn + pageNxt;
    $(".area_page").empty().html(pageNumDivHtml);
}

//点击分页按钮事件
function changePage(pageIndex) {
    var newsType = "";
    var newsTypeName = "";
    var liCollection = $(".menu_con_1").find("a");
    for (var i = 0; i < liCollection.length; i++) {
        if (!$(liCollection[i]).attr("class") == "hover png_bg") {
            newsType = liCollection[i].id;
            newsTypeName = $(liCollection[i]).html();
        }
    }

    var newsObject = {
        xmlUrl: "../xml/GameNews_{0}.xml",
        newsType: newsType,
        newsTypeName: newsTypeName,
        pageIndex: pageIndex
    };
    newsObject.xmlUrl = newsObject.xmlUrl.replace("{0}", newsType);
    loadNews(newsObject);
}


$(function () {
    ZongHe();
    NewsList();
    GongGao();
    HuoDong();
});

 

 

迷失的小鱼的主页 迷失的小鱼 | 初学一级 | 园豆:46
提问于:2013-06-14 17:34
< >
分享
所有回答(1)
0

你这个分页类似于是在内存中进行分页,给你一点java内存分页的方式,看对你有没有一点帮助。

/**
* 解决内存分页问题
* @param sourcepage 需要分页的数组
* @param pageindex 当前页从1开始
* @param pageSize 每页显示条数
* @return 分页后的数据
* @throws Exception
*/
private final <E extends Object> List<E> MergePagingList(List<E> sourceList,int pageindex,int pageSize) throws Exception{
int sindex = pageindex==1?0:(pageindex-1)*pageSize;
int send = pageindex*pageSize;
if(send>sourceList.size()){
send = sourceList.size();
}
List<E> trgetList = sourceList.subList(sindex, send);
return trgetList;
}

迷失的java | 园豆:154 (初学一级) | 2013-06-14 19:51
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册