ThinkPHP与EasyUI整合之三(searchbox):在datagrid中查询指定记录
在datagrid中toolbar添加searchbox查询框,根据列范围查询数据,先看效果图:
1. searchbox采用easyui的Demo例子,再加以js扩展,根据datagrid中的列数据自动生成选择查询范围。
1 <div id="testa" style="display:inline;padding-top:17px;">
2 <!-- 这里的padding-top是让搜索栏向下点,也就是与"添加"等按钮对齐,但在HTML里面不好用,在jsp页面中已测试可用 -->
3 <input id="sss" class="easyui-searchbox" searcher="qq" prompt="请输入查询内容" style="width:200px"></input>
4 <div id="mm" style="width:100px">
5 </div>
6 </div>
1 //循环列名,生成搜索的下拉列表
2 var fields = $('#dg').datagrid('getColumnFields');
3 var muit="";
4 for(var i=0; i<fields.length; i++){
5 var opts = $('#dg').datagrid('getColumnOption', fields[i]);
6 muit += "<div name='"+ fields[i] +"'>"+ opts.title +"</div>";
7 };
8 $('#mm').html($('#mm').html()+muit);
9 $('#sss').searchbox({
10 menu:'#mm'
11 });
12 //获取生成的搜索框
13 var a=$("#testa");
14 //将生成好的搜索框放入工具栏
15 $(".datagrid-toolbar").append(a);
16 });
17 function qq(value,name){
18 $('#dg').datagrid('load', { "searchKey": name, "searchValue": value });
19 }
2. 前台需将要查询的字段和值传给datagrid。其中searchKey为字段名,searchValue为所要查询的值。
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