zoukankan      html  css  js  c++  java
  • 分页获取数据

    /**
    * =====================================================================================
    * =====================================================================================
    */
    /**
    * @版本  1.0
    * @时间  2009-01-09
    * @作者  林松斌
    */
    /**
    * =====================================================================================
    * 初始化
    * =====================================================================================
    */

    $(document).ready(function() {
        GetChannelList(); //获取新闻频道列表
        GetNewsList();    //获取新闻列表
    });

    /**
    * =====================================================================================
    * 取数据
    * =====================================================================================
    */

    //获取新闻频道列表
    function GetChannelList() {
        $.ajax({
            url: "/Handler/NewsHandler/News.ashx?action=GetChannelList&&ts=" + new Date().getTime(),
            type: 'GET',
            dataType: 'json',
            timeout: 1000,
            error: function() {
                //alert('Error loading XML document');
            },
            success: function(result) {
                var data = (result.Table);
                $.each(data, function(i, n) {
                    CreateChannelList(n);
                });
            }
        });
    }

    //获取分页数据
    function GetPageData(myUrl, myData) {
        $.ajax({
            url: myUrl,
            data: myData,
            type: "POST",
            dataType: "json",
            error: function() {
                //alert('Error loading XML document');
            },
            success: function(result) {
                $('#NewsList').html("");
                var data = (result.Table);
                $.each(data, function(i, n) {
                    CreateNewsList(n); //创建新闻列表
                });

                var TotalCount = (result.TotalCount[0].TotalCount); //记录总数
                CreatePage(TotalCount); //创建分页
            }
        });
    }

    /**
    * =====================================================================================
    * 方法
    * =====================================================================================
    */

    //获取新闻列表
    function GetNewsList() {
        var url = "/Handler/NewsHandler/News.ashx";
        GetPageData(url, SetParms());
        var parm = SetParms();
    }

    //设置参数
    function SetParms() {
        var myData = "action=GetPageData" +
                     "&TableName=" + page.TableName +
                     "&FieldList=" + page.FieldList +
                     "&PrimaryKey=" + page.PrimaryKey +
                     "&StrWhere=" + page.StrWhere +
                     "&StrOrder=" + page.StrOrder +
                     "&PageSize=" + page.PageSize +
                     "&PageIndex=" + page.PageIndex +
                     "&ts=" + new Date().getTime();
        return myData;
    }

    /**
    * =====================================================================================
    * 功能函数
    * =====================================================================================
    */

    //字符串连接
    function StringBuffer() {
        this._string = new Array;
    }

    StringBuffer.prototype.append = function(str) {
        return this._string.push(str);
    }

    StringBuffer.prototype.toString = function() {
        return this._string.join("");
    }

    //字符串截取
    function SubString(str, len, hasDot) {
        var newLength = 0;
        var newStr = "";
        var chineseRegex = /[^\x00-\xff]/g;
        var singleChar = "";
        var strLength = str.replace(chineseRegex, "**").length;
        for (var i = 0; i < strLength; i++) {
            singleChar = str.charAt(i).toString();
            if (singleChar.match(chineseRegex) != null) {
                newLength += 2;
            }
            else {
                newLength++;
            }
            if (newLength > len) {
                break;
            }
            newStr += singleChar;
        }

        if (hasDot && strLength > len) {
            newStr += "...";
        }
        return newStr;
    }

    //取得参数
    function QueryString(item) {
        var sValue = location.search.match(new RegExp("[\?\&]" + item + "=([^\&]*)(\&?)", "i"))
        return sValue ? sValue[1] : sValue
    }

    /**
    * =====================================================================================
    * 事件
    * =====================================================================================
    */

    //点击频道
    function ChannelClick(ChannelId) {
        page.StrWhere = "ChannelId=" + ChannelId;
        GetNewsList();
    }

    //点击新闻
    function NewsClick(NewsId) {
        var url = "/News/DisplayNews.html?NewsId=" + NewsId;
        self.location = url;
    }

    //点击分页
    function PageClick(page_id, jq) {
        page.PageIndex = page_id + 1;
        GetNewsList();
    }

    /**
    * =====================================================================================
    * 操作 UI
    * =====================================================================================
    */

    //创建新闻列表
    function CreateNewsList(n) {
        var li = "<li>";
        li += "[";
        li += "<a href='javascript:;' onclick='ChannelClick(" + n.ChannelId + ")'; return false'>";
        li += n.ChannelName;
        li += "</a>";
        li += "] ";
        li += "<a href='javascript:;' onclick='NewsClick(" + n.NewsId + ")'; return false'>";
        li += n.Title;
        li += "</a>";
        li += "</li>";
        $('#NewsList').append($(li));
    }

    //创建新闻频道列表
    function CreateChannelList(n) {
        var li = "<li> ";
        li += "<a href='javascript:;' onclick='ChannelClick(" + n.ChannelId + ")'; return false'>";
        li += n.ChannelName;
        li += "</a>";
        li += "</li>";
        $('#ChannelList').append($(li));
    }

    //创建分页
    function CreatePage(TotalCount) {
        $("#Pagination").pagination(TotalCount, { items_per_page: page.PageSize, current_page: (page.PageIndex - 1), callback: PageClick });
    }

    /**
    * =====================================================================================
    * =====================================================================================
    */

    function Page(TableName, FieldList, PrimaryKey, StrWhere, StrOrder, PageSize, PageIndex) {
        this.TableName = TableName;
        this.FieldList = FieldList;
        this.PrimaryKey = PrimaryKey;
        this.StrWhere = StrWhere;
        this.StrOrder = StrOrder;
        this.PageSize = PageSize;
        this.PageIndex = PageIndex;
    }

    var page = new Page();
    page.TableName = "NE_news";
    page.FieldList = "NewsId, ChannelId, ChannelName, Title";
    page.PrimaryKey = "NewsId";
    page.StrWhere = "";
    page.StrOrder = "NewsId DESC";
    page.RecorderCount = 0;
    page.PageSize = 2;
    page.PageIndex = 1;

    /**
    * =====================================================================================
    * =====================================================================================
    */

  • 相关阅读:
    不要抱怨网速慢,只怪你自己不会调快网速
    2014年6月27日 几何和排列 答案
    我的世界杯生活
    javascript设计模式--备忘录模式(Memento)
    翻译:AngularJS应用的认证技术
    javascript设计模式--状态模式(State)
    一张思维导图说明jQuery的AJAX请求机制
    数据库设计中的14个技巧
    密室经典逃脱N部--考验你的智力!据说全球没几人走出去!不要看攻略!【工作之余也来休闲一下】
    关于编程,大学没有传授的10件事
  • 原文地址:https://www.cnblogs.com/LinFx/p/2123699.html
Copyright © 2011-2022 走看看