DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> jQuery入門知識 >> JQuery特效代碼 >> 基於Jquery實現表格動態分頁實現代碼
基於Jquery實現表格動態分頁實現代碼
編輯:JQuery特效代碼     
當頁面點擊分頁圖標時,程序會自動去後台獲取對應頁數的記錄。
關鍵代碼如下:
需要引入的css和js文件有:
代碼如下:
<link rel="stylesheet" type="text/css" href="<%=basePath %>css/theme/default/css/jpage.css"></link>
<link ID="skin" rel="stylesheet" type="text/css" href="<%=basePath %>css/config.css" />
<script language="javascript" type="text/javascript" src="<%=basePath %>js/jquery.js"></script>

其中jsp頁面代碼如下:
代碼如下:
<script language="jscript" type="text/javascript">
var pageIndex = 1; //當前頁數
$(function(){
GetPageCount();//獲取分頁數量以及總的記錄條數
$("#load").hide();//隱藏loading提示
$("#template").hide();//隱藏模板
ChangeState(0,1);//設置翻頁按鈕的初始狀態
bind();//綁定第一頁的數據
//第一頁按鈕click事件
$("#first").click(function(){
pageIndex = 1;
ChangeState(0,1);
bind();
});
//上一頁按鈕click事件
$("#previous").click(function(){
pageIndex -= 1;
ChangeState(-1,1);
if(pageIndex <= 1){
pageIndex = 1;
ChangeState(0,-1);
}
bind();
});
//下一頁按鈕click事件
$("#next").click(function(){
pageIndex += 1;
ChangeState(1,-1);
if(pageIndex>=pageCount)
{
pageIndex = pageCount;
ChangeState(-1,0);
}
bind(pageIndex);
});
//最後一頁按鈕click事件
$("#last").click(function(){
pageIndex = pageCount;
ChangeState(1,0);
bind(pageIndex);
});
//每頁顯示記錄條數select事件
$("#pageSize").change(function(){
bind();
})
});
//AJAX方法取得數據並顯示到頁面上
function bind(){
$("#load").show();
var pageSize = $("#pageSize").val();
$.ajax({
type: "get",//使用get方法訪問後台
dataType: "json",//返回json格式的數據
url: "<%=basePath%>actionSmUser.do?method=listUser2",//要訪問的後台地址
data: "pageIndex=" + pageIndex+"&pageSize="+pageSize,//要發送的數據
complete : function(msg){//msg為返回的數據,在這裡做數據綁定
$("[id=ready]").remove();
var data = eval("("+msg.responseText+")");
$.each(data, function(i, n){
var row = $("#template").clone();
row.find("#userId").text(n.userId);
row.find("#userName").text(n.userName);
row.find("#depId").text(n.depId);
row.find("#createTime").text(n.createTime);
if(n.createTime !== undefined) row.find("#createTime").text(n.createTime);
row.find("#creator").text(n.creator);
row.find("#menusId").text(n.menusId);
row.find("#isValid").text(n.isValid);
row.attr("id","ready");//改變綁定好數據的行的id
row.appendTo("#datas");//添加到模板的容器中
});
$("[id=ready]").show();
SetPageInfo();
}
});
}
function ChangeDate(date){
return date.replace("-","/").replace("-","/");
}
//設置第幾頁/共幾頁的信息
function SetPageInfo(){
var pageCount = $("#pageCount").val();
var totalCount = $("#totalCount").val();
var pageSize = $("#pageSize").val();
$("#pageinfo").html(" 第<input class='default_pgCurrentPage' id='pageIndex' type='text' value='"+pageIndex+
"' style='width: 30px' /> 頁" + "/" +"共 "+pageCount+"頁"+
" 檢索到 "+totalCount+"條記錄,顯示第 "+(pageIndex*pageSize-pageSize)+" 條 - 第 "+(pageIndex*pageSize)+" 條記錄");
}
//AJAX方法取得分頁總數
function GetPageCount(){
var pageSize = $("#pageSize").val();
$.ajax({
type: "get",
dataType: "text",
url: "<%=basePath%>actionSmUser.do?method=getPageCount",
data: "pageSize="+pageSize ,
async: false,
success: function(msg){
var data = eval("("+msg+")");
$("#pageCount").val(data[0].pageCount);
$("#totalCount").val(data[0].totalCount);
}
});
}
//改變翻頁按鈕狀態
function ChangeState(state1,state2){
$("#first").attr("class","default_pgFirst default_pgBtn");
$("#previous").attr("class","default_pgPrev default_pgBtn");
$("#next").attr("class","default_pgNext default_pgBtn");
$("#last").attr("class","default_pgLast default_pgBtn");
if(state1 == 1) {
document.getElementById("first").disabled = "";
document.getElementById("previous").disabled = "";
}else if(state1 == 0){
document.getElementById("first").disabled = "disabled";
document.getElementById("previous").disabled = "disabled";
$("#first").attr("class","default_pgFirstDisabled default_pgBtn");
$("#previous").attr("class","default_pgPrevDisabled default_pgBtn");
}if(state2 == 1){
document.getElementById("next").disabled = "";
document.getElementById("last").disabled = "";
}else if(state2 == 0){
document.getElementById("next").disabled = "disabled";
document.getElementById("last").disabled = "disabled";
$("#next").attr("class","default_pgNextDisabled default_pgBtn");
$("#last").attr("class","default_pgLastDisabled default_pgBtn");
}
}
</script>

html頁面代碼如下:
代碼如下:
<body>
<div>
<div>
<br />
<table id="datas" align="center" class="listtable" width="100%" bgcolor="#CCCCCC" cellSpacing="1" cellpadding="1" style="margin-top:5px;">
<tr class="fixheader">
<th width="14%">
用戶ID</th>
<th width="14%">
用戶名稱</th>
<th width="14%">
所在科室</th>
<th width="14%">
創建時間</th>
<th width="14%">
創建人</th>
<th width="14%">
菜單集名稱</th>
<th width="14%">
是否有效</th>
</tr>
<tr id="template" height="22px" bgcolor="#F9FDFF" onmouseover="javascript:this.style.backgroundColor='#FFFFCC'; return true;" onMouseOut="javascript:this.style.backgroundColor='#F9FDFF'; return true;">
<td id="userId" class="tdc">
</td>
<td id="userName" class="tdc">
</td>
<td id="depId" class="tdc">
</td>
<td id="createTime" class="tdc">
</td>
<td id="creator" class="tdc">
</td>
<td id="menusId" class="tdc">
</td>
<td id="isValid" class="tdc">
</td>
</tr>
</table>
</div>
<div id="load" style="left: 0px; position: absolute; top: 0px; background-color: red">
LOADING....
</div>
<div class="default_pgContainer" >
<div class="default_container">
<div class="default_pgPanel" id="skinDiv">
<table class="default_pgToolbar">
<tr>
<td class="black_pgCurrentPage">
<select id="pageSize" name="pageSize">
<option selected="selected" value="10">10</option>
<option value="20">20</option>
<option value="30">30</option>
</select>
</td>
<td>
<div id="first" class="default_pgFirst default_pgBtn" />
</td>
<td>
<div id="previous" class="default_pgPrev default_pgBtn" />
</td>
<td class="default_separator">
</td>
<td>
<div id="next" class="default_pgNext default_pgBtn" />
</td>
<td>
<div id="last" class="default_pgLast default_pgBtn" />
</td>
<td class="default_separator">
</td>
<td>
<span id="pageinfo"></span>
</td>
</tr>
</table>
</div>
</div>
</div>
</div>
<div id="test"></div>
<input type="hidden" id="pageCount" style="width: 45px" />
<input type="hidden" id="totalCount" style="width: 45px" />
</body>

後台action中代碼如下:
代碼如下:
//分頁獲取用戶信息
public void listUser2(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response){
RequestTool tool = new RequestTool(request);
Integer pageSize = tool.getIntParameter("pageSize");
Integer pageIndex = tool.getIntParameter("pageIndex");
ResultPage res = serviceSmUserImpl.findAllSmUsers(pageIndex, pageSize);
List<SmUser> smUserList = (List<SmUser>)res.getResult();
JSONArray array = new JSONArray();
JSONObject object ;
for(SmUser user:smUserList){
object = new JSONObject();
object.put("userId", user.getUserId());
object.put("userName",user.getUserName());
object.put("depId", user.getOrganCode());
object.put("createTime", user.getCreateTime());
object.put("creator", user.getCreator());
object.put("menusId", user.getMenusId());
object.put("isValid", (user.getValid().equals("1")?"有效":"無效"));
array.add(object);
}
AjaxTool.returnAjaxResponse(response, array.toJSONString());
}
//獲取總的記錄數和總頁數
public void getPageCount(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response){
RequestTool tool = new RequestTool(request);
int pageSize = tool.getIntParameter("pageSize");
List<POJO> pojos = serviceSmUserImpl.findAll();
int pageCount = pojos.size()% pageSize > 0 ? (pojos.size()/ pageSize+1):(pojos.size()/ pageSize);
JSONArray array = new JSONArray();
JSONObject object = new JSONObject();
object.put("pageCount", pageCount);
object.put("totalCount", pojos.size());
array.add(object);
AjaxTool.returnAjaxResponse(response,array.toJSONString());
}

文件打包下載
XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved