在datagrid中toolbar添加searchbox查詢框,根據列范圍查詢數據,先看效果圖:

1. searchbox采用easyui的Demo例子,再加以js擴展,根據datagrid中的列數據自動生成選擇查詢范圍。
View Code
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>
View Code
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為所要查詢的值。
View Code
1 function qq(value,name){ 2 $('#dg').datagrid('load', { "searchKey": name, "searchValue": value }); 3 }
3. 后台處理:接收前台傳遞的參數$_POST['searchkey'],$_POST['searchValue']
View Code
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條件之后。
