您的位置:首页 > 编程语言 > PHP开发

ThinkPHP与EasyUI整合之三(searchbox):在datagrid中查询指定记录

2015-10-08 10:40 591 查看
在datagrid中toolbar添加searchbox查询框,根据列范围查询数据,先看效果图:

1         function qq(value,name){
2             $('#dg').datagrid('load', { "searchKey": name, "searchValue": value });
3         }


3. 后台处理:接收前台传递的参数$_POST['searchkey'],$_POST['searchValue']

1         public function read(){
2             $pagenum=isset($_POST['page']) ? intval($_POST['page']) : 1;
3             $rowsnum=isset($_POST['rows']) ? intval($_POST['rows']) : 10;
4             $User=M("User");
5             if(isset($_POST['searchValue']) and $_POST['searchValue']!=""){
6                 $userlist=array();
7                 $map[$_POST['searchKey']]=array('like',array('%'.$_POST['searchValue'].'%'));
8                 //$userlist=$User->where($_POST['searchKey'].'="'.$_POST['searchValue'].'"')->limit(($pagenum-1)*$rowsnum.','.$rowsnum)->order('id asc')->select();
9                 //$total=$User->where($_POST['searchKey'].'="'.$_POST['searchValue'].'"')->count();
10                 $userlist=$User->where($map)->limit(($pagenum-1)*$rowsnum.','.$rowsnum)->order('id asc')->select();
11                 $total=$User->where($map)->count();
12                 if ($total==0){
13                     $userlist=array("firstname"=>'',"lastname"=>'',"phone"=>'',"email"=>'',"id"=>'');
14                     $json='{"total":'.$total.',"rows":['.json_encode($userlist).']}';
15                     echo $json;
16                 }else{
17                     $json='{"total":'.$total.',"rows":'.json_encode($userlist).'}';//重要,easyui的标准数据格式,数据总数和数据内容在同一个json中
18                     echo $json;
19                 }
20             }else{
21                 $total = $User->count();    //计算总数
22                 $userlist=array();
23                 $userlist=$User->limit(($pagenum-1)*$rowsnum.','.$rowsnum)->order('id asc')->select();
24                 if ($total==0){
25                     $userlist=array("firstname"=>'',"lastname"=>'',"phone"=>'',"email"=>'',"id"=>'');
26                     $json='{"total":'.$total.',"rows":['.json_encode($userlist).']}';
27                     echo $json;
28                 }else{
29                     $json='{"total":'.$total.',"rows":'.json_encode($userlist).'}';//重要,easyui的标准数据格式,数据总数和数据内容在同一个json中
30                     echo $json;
31                 }
32             }
33         }


4.分析后台代码

  1. 首先判断是否要生成查询数据,条件是传递参数$_POST['searchKey']存在且不为空 。

    if(isset($_POST['searchValue']) and $_POST['searchValue']!="")

  2. 采用like查询语言扩大查询范围,$map[$_POST['searchKey']]=array('like',array('%'.$_POST['searchValue'].'%'));生成的查询代码是:$_POST['searchKey']

    like % $_POST['searchValue'] %

  3. 生成的查询记录要符合datagrid的json数据格式。其中json数据的总记录用count()生成,需位于where条件之后。
http://www.cnblogs.com/m199/archive/2012/12/20/2825720.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: