/// /// /// DataPager = function (DataPagerID) { this.ParentElement = $(document); //要加入的父容器 this.id = DataPagerID; this.PageIndex = 1; this.PageSize = 10; this.PageCount = 0; this.RowCount = 0; this.Align = ""; //导航对齐 this.PageNumber = 5; //每次分页数字1-5 this.PageIndexNumber = 1; //分页UI的分页数 this.PageCountNumber = 0; //分页总数 this.PageStyle = 0; //0默认1数字 this.PageListID = this.id + "_listPageSize"; this.FirstButtonID = this.id + "_FirstButton"; this.PrevButtonID = this.id + "_PrevButton"; this.NextButtonID = this.id + "_NextButton"; this.LastButtonID = this.id + "_LastButton"; this.PageIndexID = this.id + "_PageIndex"; this.PageTitleID = this.id + "_PageTitle"; this.NumberButton = this.id + "_NumberButton"; this.PagerFlag = false; this.PageList = new Array(); var EventHandler = new Sys.EventHandlerList(); var My = this; //#region 初始化 this.Init = function () { // if (My.ParentElement == null) My.ParentElement = $(document.body); var div = $("#" + My.id, My.ParentElement); if (My.PageStyle == 0) { if (My.PageList.length == 0) { Array.add(this.PageList, { text: 10 }); Array.add(this.PageList, { text: 20 }); Array.add(this.PageList, { text: 30 }); Array.add(this.PageList, { text: 50 }); Array.add(this.PageList, { text: 100 }); Array.add(this.PageList, { text: 500 }); Array.add(this.PageList, { text: 1000 }); Array.add(this.PageList, { text: 10000 }); } var htmlString = new Sys.StringBuilder(); htmlString.append(""); htmlString.append(""); htmlString.append(""); htmlString.append(""); htmlString.append(""); htmlString.append(""); htmlString.append(""); htmlString.append(""); htmlString.append(""); htmlString.append(""); htmlString.append(""); htmlString.append("
页,共0页,0条记录
"); div.html(htmlString.toString()); $("#" + My.id + ' .easyui-linkbutton', My.ParentElement).linkbutton(); $("#" + My.PageIndexID, My.ParentElement).numberbox(); //初始化下拉框 $("#" + My.PageListID, My.ParentElement).combobox({ panelHeight: '160px', data: My.PageList, valueField: 'text', textField: 'text', onSelect: function () { My.PageSize = parseInt($(this).combobox("getValue")); My.SetPageIndex(1); OnPageIndexChanged(); } }); //选择默认下拉框每页数量 $("#" + My.PageListID, My.ParentElement).combobox("setValue", My.PageSize); $("#" + My.PageListID, My.ParentElement).combobox("textbox").bind("click", function () { $("#" + My.PageListID, My.ParentElement).combobox("showPanel"); }); //页选择输入框 $("#" + My.PageIndexID, My.ParentElement).bind("blur", function () { var index = $("#" + My.PageIndexID, My.ParentElement).val(); if (index == "") index = 1; var index2 = parseInt(index); if (isNaN(index2) == true) index2 = 1; if (index2 > My.PageCount || index2 < 1) index2 = 1; if (index2 == My.PageIndex) { My.SetPageIndex(index2); return false; } My.SetPageIndex(index2); My.PagerFlag = true; OnPageIndexChanged(); }); //页选择输入框 $("#" + My.PageIndexID, My.ParentElement).bind("keypress", function () { if (event.keyCode == 13) { var index = $("#" + My.PageIndexID, My.ParentElement).val(); if (index == "") index = 1; var index2 = parseInt(index); if (isNaN(index2) == true) index2 = 1; if (index2 > My.PageCount || index2 < 1) index2 = 1; if (index2 == My.PageIndex) { My.SetPageIndex(index2); return false; } My.SetPageIndex(index2); My.PagerFlag = true; OnPageIndexChanged(); } }); My.MathPageNumber(); } //数字样式 if (My.PageStyle == 1) { var htmlString = new Sys.StringBuilder(); htmlString.append(""); htmlString.append(""); htmlString.append(""); htmlString.append(""); htmlString.append(""); htmlString.append(""); htmlString.append("
"); div.html(htmlString.toString()); $("#" + My.id + ' .easyui-linkbutton', My.ParentElement).linkbutton(); $("#" + My.NumberButton + " td", My.ParentElement).live("click", function () { $("#" + My.NumberButton + " td", My.ParentElement).css("background-color", ""); $(this).css("background-color", "#82B0DF"); My.PageIndex = parseInt($(this).text()); My.PagerFlag = true; OnPageIndexChanged(); //引发分页事件 }); $("#" + My.NumberButton + " td", My.ParentElement).live("mouseover", function () { $(this).css("border", "1px solid #AECEEE"); }); $("#" + My.NumberButton + " td", My.ParentElement).live("mouseout", function () { $(this).css("border", ""); }); } } //#endregion //#region 计算页数 this.MathPageNumber = function () { if (My.PageStyle == 0) { if (My.RowCount > 0) { My.PageCount = Math.ceil(My.RowCount / My.PageSize); $("#" + My.PageIndexID, My.ParentElement).numberbox({ max: My.PageCount }); //如果当前索引大于总页数,当前索引减1,如果再次比较总页数还是大于索引,那么重置页索引 if (My.PageIndex > My.PageCount) { My.SetPageIndex(My.PageIndex - 1); if (My.PageIndex > My.PageCount) My.SetPageIndex(1); } else { My.SetPageIndex(My.PageIndex); } $("#" + My.PageTitleID, My.ParentElement).text("页,共" + My.PageCount + "页," + My.RowCount + "条记录"); $("#" + My.PageIndexID, My.ParentElement).attr("max", My.PageCount); $("#" + My.FirstButtonID, My.ParentElement).unbind("click"); $("#" + My.PrevButtonID, My.ParentElement).unbind("click"); $("#" + My.NextButtonID, My.ParentElement).unbind("click"); $("#" + My.LastButtonID, My.ParentElement).unbind("click"); if (My.PageIndex > 1) { $("#" + My.FirstButtonID, My.ParentElement).linkbutton("enable"); $("#" + My.PrevButtonID, My.ParentElement).linkbutton("enable"); //首页 $("#" + My.FirstButtonID, My.ParentElement).bind("click", function () { My.SetPageIndex(1); OnPageIndexChanged(); }); //上一页 $("#" + My.PrevButtonID, My.ParentElement).bind("click", function () { My.SetPageIndex(My.PageIndex - 1); My.PagerFlag = true; OnPageIndexChanged(); }); } else { $("#" + My.FirstButtonID, My.ParentElement).linkbutton("disable"); $("#" + My.PrevButtonID, My.ParentElement).linkbutton("disable"); } if (My.PageIndex == My.PageCount || My.PageCount == 0) { $("#" + My.NextButtonID, My.ParentElement).linkbutton("disable"); $("#" + My.LastButtonID, My.ParentElement).linkbutton("disable"); } else { $("#" + My.NextButtonID, My.ParentElement).linkbutton("enable"); $("#" + My.LastButtonID, My.ParentElement).linkbutton("enable"); //下一页 $("#" + My.NextButtonID, My.ParentElement).bind("click", function () { My.SetPageIndex(My.PageIndex + 1); My.PagerFlag = true; OnPageIndexChanged(); }); //末页 $("#" + My.LastButtonID, My.ParentElement).bind("click", function () { My.PageIndex = My.PageCount; My.SetPageIndex(My.PageCount); My.PagerFlag = true; OnPageIndexChanged(); }); } } else { My.PageCount = 0; My.SetPageIndex(1); $("#" + My.PageIndexID, My.ParentElement).attr("max", 1); $("#" + My.PageIndexID, My.ParentElement).numberbox({ max: 1 }); $("#" + My.PageTitleID, My.ParentElement).text("页,共" + My.PageCount + "页," + My.RowCount + "条记录"); $("#" + My.FirstButtonID, My.ParentElement).linkbutton("enable"); $("#" + My.PrevButtonID, My.ParentElement).linkbutton("enable"); $("#" + My.NextButtonID, My.ParentElement).linkbutton("enable"); $("#" + My.LastButtonID, My.ParentElement).linkbutton("enable"); $("#" + My.FirstButtonID, My.ParentElement).unbind("click"); $("#" + My.PrevButtonID, My.ParentElement).unbind("click"); $("#" + My.NextButtonID, My.ParentElement).unbind("click"); $("#" + My.LastButtonID, My.ParentElement).unbind("click"); } } //数字样式 if (My.PageStyle == 1) { if (My.RowCount > 0) { My.PageCount = Math.ceil(My.RowCount / My.PageSize); My.PageCountNumber = Math.ceil(My.RowCount / (My.PageSize * My.PageNumber)); if (My.PageCountNumber > My.PageIndexNumber) My.PageIndexNumber = 1; //重新创建页导行按钮 var htmlString = new Sys.StringBuilder(); htmlString.append(""); htmlString.append(""); var StartIndex = (My.PageIndexNumber - 1) * My.PageNumber; //当前开始页索引 var StopIndex = StartIndex + My.PageNumber; //结束页索引 if (StopIndex > My.PageCount) StopIndex = My.PageCount; for (var i = StartIndex; i < StopIndex; i++) { htmlString.append(""); //border: 1px solid #AECEEE; } htmlString.append(""); htmlString.append("
" + (i + 1) + "
"); $("#" + My.NumberButton, My.ParentElement).html(htmlString.toString()); //如果当前索引大于总页数,当前索引减1,如果再次比较总页数还是大于索引,那么重置页索引 if (My.PageIndex > My.PageCount) { My.SetPageIndex(My.PageIndex - 1); if (My.PageIndex > My.PageCount) My.SetPageIndex(1); } else { My.SetPageIndex(My.PageIndex); } $("#" + My.FirstButtonID, My.ParentElement).linkbutton("enable"); $("#" + My.NextButtonID, My.ParentElement).linkbutton("enable"); $("#" + My.PrevButtonID, My.ParentElement).unbind("click"); $("#" + My.NextButtonID, My.ParentElement).unbind("click"); if (My.PageIndexNumber > 1) { $("#" + My.PrevButtonID, My.ParentElement).linkbutton("enable"); //上一页 $("#" + My.PrevButtonID, My.ParentElement).bind("click", function () { My.PageIndexNumber--; My.MathPageNumber(); //重算索引 }); } else { $("#" + My.PrevButtonID, My.ParentElement).linkbutton("disable"); } if (My.PageIndexNumber == My.PageCountNumber || My.PageCountNumber == 0) { $("#" + My.NextButtonID, My.ParentElement).linkbutton("disable"); } else { $("#" + My.NextButtonID, My.ParentElement).linkbutton("enable"); //下一页 $("#" + My.NextButtonID, My.ParentElement).bind("click", function () { My.PageIndexNumber++; My.MathPageNumber(); //重算索引 }); } } else { My.PageCount = 0; My.PageIndex = 1; My.PageCountNumber = 0; My.PageIndexNumber = 1; //上下都不能按 $("#" + My.NumberButton, My.ParentElement).html(""); $("#" + My.FirstButtonID, My.ParentElement).linkbutton("enable"); $("#" + My.NextButtonID, My.ParentElement).linkbutton("enable"); $("#" + My.PrevButtonID, My.ParentElement).unbind("click"); $("#" + My.NextButtonID, My.ParentElement).unbind("click"); } } } //#endregion //#region 设置当前页数 this.SetPageSize = function (PageSize) { My.PageSize = PageSize; $("#" + My.PageListID, My.ParentElement).combobox("setValue", My.PageSize); } //#endregion //#region 设置当前选择页数 this.SetPageIndex = function (PageIndex) { My.PageIndex = PageIndex; if (My.PageStyle == 0) $("#" + My.PageIndexID, My.ParentElement).val(My.PageIndex); if (My.PageStyle == 1) $("#" + My.NumberButton + " td", My.ParentElement).css("background-color", ""); if (My.PageStyle == 1) $("#" + My.NumberButton + " td[index='" + PageIndex + "']", My.ParentElement).css("background-color", "#82B0DF"); } //#endregion //#region 订购事件监听(分页) this.add_PageIndexChanged = function (handler) { EventHandler.addHandler('PageIndexChanged', handler); } //#endregion //#region 引发自定义事件(分页) function OnPageIndexChanged() { var h = EventHandler.getHandler('PageIndexChanged'); if (h) h(My, Sys.EventArgs.Empty); } //#endregion }