Jquery:ajax實現(xiàn)翻頁無刷新功能代碼
更新時間:2013年08月05日 16:41:01 作者:
ajax實現(xiàn)翻頁在實際應用中還是比較常見的,實現(xiàn)ajax翻頁有兩部分:js部分、html部分,具體如下,感興趣的朋友可以參考下,希望對大家有所幫助
不多說,直接貼代碼:
下面是js部分:
var pageSize = "10";//每頁行數(shù)
var currentPage = "1";//當前頁
var totalPage = "0";//總頁數(shù)
var rowCount = "0";//總條數(shù)
var params="";//參數(shù)
var url="activity_list.action";//action
$(document).ready(function(){//jquery代碼隨著document加載完畢而加載
//分頁查詢
function queryForPages(){
$.ajax({
url:url,
type:'post',
dataType:'json',
data:"qo.currentPage="+currentPage+"&qo.pageSize="+pageSize+params,
success:function callbackFun(data){
//解析json
var info = eval("("+data+")");
//清空數(shù)據(jù)
clearDate();
fillTable(info);
}
});
}
//填充數(shù)據(jù)
function fillTable(info){
if(info.length>1){
totalPage = info[info.length-1].totalPage;
var tbody_content="";//不初始化字符串"",會默認"undefined"
for(var i=0 ; i<info.length-1;i++){
tbody_content +="<tr>"
+"<td data-title='序號' >"+(i+1+(currentPage-1)*pageSize)+"</td>"
+"<td data-title='標題'>"+info[i].title.substr(0,20)+"</td>"
+"<td data-title='地點'>"+info[i].address.substr(0,6)+"</td>"
+"<td data-title='已報名'>"+info[i].quota_sign+"人</td>"
+"<td data-title='類別'>"+info[i].type+"</td>"
+"<td data-title='操作'><a href='<%=request.getContextPath()%>/activity_edit.action?id="+info[i].id+"'>編輯</a></td>"
+"</tr>"
$("#t_body").html(tbody_content);
}
}else{
$("#t_head").html("");
$("#t_body").html("<div style='height: 200px;width: 700px;padding-top: 100px;' align='center'>"+info.msg+"</div>");
}
}
//清空數(shù)據(jù)
function clearDate(){
$("#t_body").html("");
}
//搜索活動
$("#searchActivity").click(function(){ queryForPages(); });
//首頁
$("#firstPage").click(function(){
currentPage="1";
queryForPages();
});
//上一頁
$("#previous").click(function(){
if(currentPage>1){
currentPage-- ;
}
queryForPages();
});
//下一頁
$("#next").click(function(){
if(currentPage<totalPage){
currentPage++ ;
}
queryForPages();
});
//尾頁
$("#last").click(function(){
currentPage = totalPage;
queryForPages();
});
});
下面是html代碼部分:
<table class="table style-5">
<thead id="t_head">
<tr>
<th>序號</th>
<th>標題</th>
<th>地點</th>
<th>已報名</th>
<th>類別</th>
<th>操作</th>
</tr>
</thead>
<tbody id="t_body">
<!-- ajax填充列表 -->
</tbody>
</table>
<button id="firstPage">首頁</button>
<button id="previous">上一頁</button>
<button id="next">下一頁</button>
<button id="last">尾頁</button>
下面是js部分:
復制代碼 代碼如下:
var pageSize = "10";//每頁行數(shù)
var currentPage = "1";//當前頁
var totalPage = "0";//總頁數(shù)
var rowCount = "0";//總條數(shù)
var params="";//參數(shù)
var url="activity_list.action";//action
$(document).ready(function(){//jquery代碼隨著document加載完畢而加載
//分頁查詢
function queryForPages(){
$.ajax({
url:url,
type:'post',
dataType:'json',
data:"qo.currentPage="+currentPage+"&qo.pageSize="+pageSize+params,
success:function callbackFun(data){
//解析json
var info = eval("("+data+")");
//清空數(shù)據(jù)
clearDate();
fillTable(info);
}
});
}
//填充數(shù)據(jù)
function fillTable(info){
if(info.length>1){
totalPage = info[info.length-1].totalPage;
var tbody_content="";//不初始化字符串"",會默認"undefined"
for(var i=0 ; i<info.length-1;i++){
tbody_content +="<tr>"
+"<td data-title='序號' >"+(i+1+(currentPage-1)*pageSize)+"</td>"
+"<td data-title='標題'>"+info[i].title.substr(0,20)+"</td>"
+"<td data-title='地點'>"+info[i].address.substr(0,6)+"</td>"
+"<td data-title='已報名'>"+info[i].quota_sign+"人</td>"
+"<td data-title='類別'>"+info[i].type+"</td>"
+"<td data-title='操作'><a href='<%=request.getContextPath()%>/activity_edit.action?id="+info[i].id+"'>編輯</a></td>"
+"</tr>"
$("#t_body").html(tbody_content);
}
}else{
$("#t_head").html("");
$("#t_body").html("<div style='height: 200px;width: 700px;padding-top: 100px;' align='center'>"+info.msg+"</div>");
}
}
//清空數(shù)據(jù)
function clearDate(){
$("#t_body").html("");
}
//搜索活動
$("#searchActivity").click(function(){ queryForPages(); });
//首頁
$("#firstPage").click(function(){
currentPage="1";
queryForPages();
});
//上一頁
$("#previous").click(function(){
if(currentPage>1){
currentPage-- ;
}
queryForPages();
});
//下一頁
$("#next").click(function(){
if(currentPage<totalPage){
currentPage++ ;
}
queryForPages();
});
//尾頁
$("#last").click(function(){
currentPage = totalPage;
queryForPages();
});
});
下面是html代碼部分:
復制代碼 代碼如下:
<table class="table style-5">
<thead id="t_head">
<tr>
<th>序號</th>
<th>標題</th>
<th>地點</th>
<th>已報名</th>
<th>類別</th>
<th>操作</th>
</tr>
</thead>
<tbody id="t_body">
<!-- ajax填充列表 -->
</tbody>
</table>
<button id="firstPage">首頁</button>
<button id="previous">上一頁</button>
<button id="next">下一頁</button>
<button id="last">尾頁</button>
您可能感興趣的文章:
- JQuery的Ajax請求實現(xiàn)局部刷新的簡單實例
- JQuery+Ajax無刷新分頁的實例代碼
- jQuery+AJAX實現(xiàn)無刷新下拉加載更多
- jQuery實現(xiàn)form表單基于ajax無刷新提交方法詳解
- jQuery Pagination Ajax分頁插件(分頁切換時無刷新與延遲)中文翻譯版
- jQuery實現(xiàn)AJAX定時刷新局部頁面實例
- 基于jquery ajax 用戶無刷新登錄方法詳解
- JS+Ajax+Jquery實現(xiàn)頁面無刷新分頁以及分組 超強的實現(xiàn)
- 基于Jquery 解決Ajax請求的頁面 瀏覽器后退前進功能,頁面刷新功能實效問題
- jQuery ajax請求struts action實現(xiàn)異步刷新
相關文章
jQuery實現(xiàn)Twitter的自動文字補齊特效
本文介紹了一款jQuery實現(xiàn)的文字自動補全特效的插件,該插件可以結合本地數(shù)據(jù)進行一些操作。推薦關注一下H5的幾種數(shù)據(jù)存儲的方式(localstorage與sessionstorage、IndexedDB、離線緩存manifest文件)2014-11-11
jQuery+CSS3實現(xiàn)仿花瓣網(wǎng)固定頂部位置帶懸浮效果的導航菜單
這篇文章主要介紹了jQuery+CSS3實現(xiàn)仿花瓣網(wǎng)固定頂部位置帶懸浮效果的導航菜單,可實現(xiàn)頁面向下滑動后導航欄橫向懸浮并固定在頂部的功能,涉及jQuery事件響應及頁面元素屬性動態(tài)修改相關操作技巧,需要的朋友可以參考下2016-09-09
淺析ajax請求json數(shù)據(jù)并用js解析(示例分析)
這應該是每個web開發(fā)的人員都應該掌握的基礎技術,需要的朋友可以參考下2013-07-07
jquery中ready()函數(shù)執(zhí)行的時機和window的load事件比較
這篇文章主要介紹了jquery中ready()函數(shù)執(zhí)行的時機和window的load事件比較的相關資料,需要的朋友可以參考下2015-06-06
jQuery快速上手:寫jQuery與直接寫JS的區(qū)別詳細解析
jQuery代碼具體的寫法和原生的Javascript寫法在執(zhí)行常見操作時的區(qū)別如下所示。需要的朋友可以過來參考下2013-08-08

