Thinkphp and easyui integration 3 (searchbox): query a specified record in the DataGrid

Source: Internet
Author: User

In the DataGrid, add the searchbox query box to the toolbar and query data based on the column range. First, see:

1. searchbox uses the demo example of easyui, and then expands it with Js. the query range is automatically generated based on the column data in the DataGrid.

View code

1 <Div id = "Testa" style = "display: inline; padding-top: 17px;"> 2 <! -- Here, padding-top is used to align the search column down, that is, with the "add" button, but it is not useful in HTML, test availability on the JSP page --> 3 <input id = "Sss" class = "easyui-searchbox" searcher = "QQ" prompt = "Enter the query content" style =" width: 200px "> </input> 4 <Div id =" mm "style =" width: 100px "> 5 </div> 6 </div>
View code

1 // generate the search drop-down list by repeating the column name. 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 optional ('{mm'}.html('{mm'}.html () + muit); 9 $ ('# ss '). searchbox ({10 menu: '# mm' 11}); 12 // obtain the generated search box 13 var A =$ ("# Testa "); 14 // Add the generated search box to the toolbar 15 $ (". dataGrid-toolbar "). append (a); 16}); 17 function QQ (value, name) {18 $ ('# DG '). dataGrid ('load', {"searchkey": name, "searchvalue": Value}); 19}

 

2. The fields and values to be queried on the foreground are passed to the DataGrid. Specifically, searchkey is the field name and searchvalue is the value to be queried.

View code

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

 

3. background processing: receives the parameter $ _ post ['searchkey'], $ _ post ['searchvalue'] passed by the foreground.

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 ). '}'; // important: Standard easyui data format. The total number of data and data content are in the same JSON 18 echo $ JSON; 19} 20} else {21 $ Total = $ user-> count (); // calculates the total number of 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 ). '}'; // important, standard easyui data format. The total number of data and data content are 30 echo $ JSON in the same JSON; 31} 32} 33}

 

4. Analyze the background code

1. First, determine whether to generate the query data. The condition is that the $ _ post ['searchkey'] parameter is passed and is not null.

If (isset ($ _ post ['searchvalue']) and $ _ post ['searchvalue']! = "")

2. use the like Query Language to expand the query range. $ map [$ _ post ['searchkey'] = array ('like', array ('% '. $ _ post ['searchvalue']. '%'); the generated query code is: $ _ post ['searchkey']

Like % $ _ post ['searchvalue'] %

3. The generated query records must comply with the DataGrid JSON data format. The total records of JSON data are generated using count (), which must be placed behind the where condition.

  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.