Ajax實現(xiàn)搜索功能的分頁
本文實例為大家分享了ajax實現(xiàn)搜索功能的分頁,供大家參考,具體內(nèi)容如下
之前只實現(xiàn)了搜索功能,但是并不能分頁2333,所以在上篇的基礎(chǔ)上修改實現(xiàn)分頁(首頁,上一頁,下一頁,尾頁,跳轉(zhuǎn)頁面),腦袋瓜不夠用,要記下來記下來
html代碼
<a class="btn-lit" type="submit" onclick="searchresult(on())">搜索</a> //嵌套函數(shù),用來情空最大頁數(shù)和當(dāng)前頁數(shù) <div id="fenye" class="cnt-wp" style="display: none;"> <span id="pageajax"> 頁次:{$pagefunc[1]}/{$pagefunc[4]} 每頁22總數(shù){$pagefunc[2]} <a class="btn-lit" onclick="pageajaxfirst()">首頁</a> <a class="btn-lit" onclick="pageajaxprev()">上一頁</a> <a class="btn-lit" onclick="pageajaxnext()">下一頁</a> <a class="btn-lit" onclick="pageajaxend()">尾頁</a> <input type="text" name="pagetiao" id="pagetiao" style="width: 35px;height: 20px;margin-top:-2px;"> <a onclick="tiao()" class="btn-lit">跳轉(zhuǎn)</a> <input type="hidden" name="maxys" id="maxys" value=""> <input type="hidden" name="page" id="pagecurrent" value=""> //hidden用來保存最大頁和當(dāng)前頁 </div>
js代碼
<script type="text/javascript"> //清空最大頁和當(dāng)前頁,保證能夠多次搜索 function on() { $('#pagecurrent').val(''); $('#maxys').val(''); } //首頁 function pageajaxfirst() { $('#pagecurrent').val(1); searchresult(); } //尾頁 function pageajaxend() { var maxys = $('#maxys').val(); $('#pagecurrent').val(maxys); searchresult(); } //下一頁 function pageajaxnext() { var maxys = $('#maxys').val(); var page = $('#pagecurrent').val(); $('#pagecurrent').val(parseint(page) +1); if (page > maxys) { $('#pagecurrent').val(maxys); } searchresult(); } //上一頁 function pageajaxprev() { var maxys = $('#maxys').val(); var page = $('#pagecurrent').val(); $('#pagecurrent').val(parseint(page) -1); if (page < 1) { $('#pagecurrent').val(1); } searchresult(); } //跳轉(zhuǎn)頁面 function tiao () { var maxys = $('#maxys').val(); var now = $('#pagetiao').val(); $('#pagecurrent').val(now); var page = $('#pagecurrent').val(); if (page < 1) { $('#pagecurrent').val(1); } if (page > maxys) { $('#pagecurrent').val(maxys); } searchresult(); } function searchresult() { var formdata = $("#mypost").serialize(); var caption = $("#caption"); var html = ''; $('#pagebar').remove(); $.ajax({ type:"get", url:"/user/search/do_search", data:formdata, success:success, error: fail }); function fail() { alert('查詢條件有誤。') } function success(search){ if (search != null) { var obj = json.parse(search); var content = obj[0]; var nowpage = obj[1]; var total1 = obj[2]; var allpage = obj[4]; $.each(content, function (key, value) { html += '<tr class="sousuo"><td class="chk"><input value="" val="' + content[key]['user_id'] + '" type="checkbox" name="checkbox[' + content[key]['user_id'] + ']" class="check-box" /></td> <td class="txt40 c">' + content[key]['user_id'] + '</td><td class="txt40 c"><a href="/user/user/user_info/user_id/' + content[key]['user_id'] +'" rel="external nofollow" ' + '"title="查看資料">'+ content[key]['account'] + '</a></td>' + '<td class="txt40 c">' + content[key]['nickname']+ '</td><td class="txt40 c">' + content[key]['sex'] + '</td><td class="txt80 c">' + content[key]['coin'] + '</td><td class="txt40 c">' + content[key]['tickets'] + '</td><td class="txt40 c">' + content[key]['gem'] + '</td><td class="txt40 c">vip' + content[key]['vip_id'] + '</td><td class="txt40 c">' + content[key]['user_type'] + '</td> <td class="txt80 c">' + content[key]['register_platform'] + ' </td> <td class="txt80 c">' + content[key]['register_date'] + '</td> <td class="txt80 c">'+content[key]['last_login_time'] + '</td><td class="txt80 c">'+ content[key]['last_login_ip'] + '</td> <td class="txt80 c">'+ content[key]['game_login_times'] +'</td><td class="txt80 c">'+ content[key]['prohibit_flag'] + '</td><td class="icon"><a class="opt" title="編輯" href="/user/user/edit_user/user_id/' + content[key]['user_id'] + '}" rel="external nofollow" ><span class="icon-sprite icon-edit"></a></td></tr>'; }); $('.showtr').remove(); $('.sousuo').remove(); $('.xiaoshi').remove(); $('.show').after(html); $('#maxys').val(allpage); $('#pagecurrent').val(nowpage); } else { $('.show').after('<tr>沒有數(shù)據(jù)</tr>'); } $('#fenye').css('display','block'); $('#pageajax').html('頁次:'+ nowpage + '/' + allpage +'每頁22總數(shù)' + total1); } } </script>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持碩編程。