tp5頁面輸出時,搜索功能在跳轉下一頁時,如果不做任何處理,會返回原有是第二頁輸出的數據。為了保證跳轉下一頁時輸出的是搜索到的數據,做以下處理。
(要根據自己的搜索字段進行適當修改)
頁面js代碼,給地址欄加上參數,以便於點擊搜索按鈕後台通過地址欄得到搜索條件。
$(document).ready(function(){
//點擊搜索按鈕執行搜索 $("#search").click(function() { var form = $('#form'); var url = form.attr('action'); var query = $('.key').serialize(); query = query.replace(/(&|^)(\w*?\d*?\-*?_*?)*?=?((?=&)|(?=$))/g, ''); query = query.replace(/^&/g, ''); if (url.indexOf('?') > 0) { url += '&' + query; } else { url += '?' + query; } form.attr('action',url); }); //回車搜索 $(".search-form").keyup(function(e) { if (e.keyCode === 13) { $(".search").click(); return false; } });
//設定input框的值,保留搜索條件,setValue的方法參照http://www.cnblogs.com/xiaopiyuanzi/p/5920864.html setValue('shop_id', "{:input('get.shop_id')}"); setValue('key', "{:input('get.key')}"); })
搜索部分頁面html代碼
<div class="search search-form" > <form action="{:url('news')}" id="form" method="post" <table border="0" cellpadding="5" width="35%"> <tr> <td>商品類: <select name="shop_id" class="key"> <option value="all">全部</option> {volist name='li' id='k'} <option value="{$k.id}">{$k.name}</option> {/volist} </select> </td> <td>標題:<input type="text" name="key" class="key" class="inpMain" placeholder="新聞標題或標題包含字"></td> <td><input type="submit" id="search" class="btn" value="搜索" /></td> </tr> </table> </form> </div>
控制器主要代碼
if (empty(input('get.'))) { $page = NewsModel::where('isdelete',1)->order('id desc')->paginate(8); $this->assign('list',$page); return $this->fetch(); } else { $shop_id=input('get.shop_id'); $key=input('get.key'); if ($shop_id=='all' && $key != '') { $page=NewsModel::where(['title'=>['like',"%$key%"],'isdelete'=>['eq',1]])->order('id desc')->paginate(8,false,[ 'query' => ['shop_id' => $shop_id,'key' => $key] //url額外參數 ]); } elseif ($shop_id != 'all' && $key != '') { $page=NewsModel::where(['title'=>['like',"%$key%"],'shop_id'=>['eq',$shop_id],'isdelete'=>['eq',1]])->order('id desc')->paginate(8,false,[ 'query' => ['shop_id' => $shop_id,'key' => $key] ]); } elseif ($shop_id != 'all' && $key == '') { $page=NewsModel::where(['shop_id'=>['eq',$shop_id],'isdelete'=>['eq',1]])->order('id desc')->paginate(8,false,[ 'query' => ['shop_id' => $shop_id,'key' => $key] ]); } else { $page=NewsModel::where('isdelete',1)->order('id desc')->paginate(8,false,[ 'query' => ['shop_id' => $shop_id,'key' => $key] ]); } $this->assign('list',$page); return $this->fetch(); }