现在的位置: 首页 > 综合 > 正文

分页获取数据

2012年07月22日 ⁄ 综合 ⁄ 共 5663字 ⁄ 字号 评论关闭

/**
* =====================================================================================
* =====================================================================================
*/
/**
* @版本  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;

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

抱歉!评论已关闭.