tp5页面输出时,搜索后跳转下一页的处理

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" style="background: #e0e0e0;margin-bottom: 10px">
<form action="{:url('news')}" id="form" method="post"
<table border="0" style="height: 50px;font-size: 15px;color: black;" 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();
}
上一篇:js 闭包的简单理解


下一篇:js闭包之初步理解( JavaScript closure)