Easyui searchBox search box
<Body class = "easyui-layout" data-options = "fit: true"> <div data-options = "region: 'center', border: false "style =" overflow: hidden; "> <table id =" datagrid "> </table> </div> <div id =" search "style =" display: inline; padding-top: 10px; "> <! -- 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 --> <input id = "searchBox" class = "easyui-searchbox" searcher = "searchData" prompt = "Enter the query content" style = "width: 300px "> </input> <div id =" searchMenu "style =" width: 100px "> </div> <div id =" menu "class =" easyui-menu "style =" width: 120px; display: none; "> <div onclick =" append (); "data-options =" iconCls: 'icon-add' "> add </div> <div onclick =" removeData (); "data-options =" iconCls: 'icon-delete' "> Delete </div> <div onclick =" edit (); "data-options =" iconCls: 'icon-edit' "> edit </div> </body>
2. js
// Generate the search drop-down list var fields =$ ('# datagrid') for the loop column name '). datagrid ('getcolumnfields '); var muit = ""; for (var I = 0; I <fields. length; I ++) {var opts =$ ('# datagrid '). datagrid ('getcolumnoption ', fields [I]); muit + = "<div name ='" + fields [I] + "'>" + opts. title + "</div>" too many characters ('{searchmenu'{.html({('{searchmenu'{.html () + muit); $ ('# searchbox '). searchbox ({menu: '# searchMenu'}); // obtain the generated search box var tSearch =$ ("# search "); // Add the generated search box to the toolbar $ (". datagrid-toolbar "). append (tSearch );
3. search methods
function searchData(value,name){ $('#datagrid').datagrid('load', { "searchKey": name, "searchValue": value });}
4. searchKey is the field name to be queried, and searchValue is the value of this field name. The background receives these two parameters and concatenates them into an SQL query.
A. This is the total query record.
String hql = "select count(*) from PageImg p where 1=1";if (!UtilTool.isNull(pageImgModel.getSearchKey()) && !UtilTool.isNull(pageImgModel.getSearchValue())) {hql = hql + " and p." + pageImgModel.getSearchKey() + " like '%" + pageImgModel.getSearchValue() + "%'";}
B. This is the query list.
private List<PageImg> find(PageImgModel pageImgModel) {String hql = "from PageImg p where 1=1 ";List<Object> values = new ArrayList<Object>();hql = addWhere(pageImgModel, hql, values);if (pageImgModel.getSort() != null && pageImgModel.getOrder() != null) {hql += " order by " + pageImgModel.getSort() + " " + pageImgModel.getOrder();}return pageImgDAO.find(hql, values, pageImgModel.getPage(), pageImgModel.getRows());}private String addWhere(PageImgModel imgModel, String hql, List<Object> values) {if (!UtilTool.isNull(imgModel.getSearchKey()) && !UtilTool.isNull(imgModel.getSearchValue())) {hql += "and p." + imgModel.getSearchKey() + " like ? ";values.add("%" + imgModel.getSearchValue().trim() + "%");}return hql;}
Encapsulated DAO
/*** Query set (with pagination) ** @ param hql * @ param page * @ param rows * @ return */public List <T> find (String hql, List <Object> param, integer page, Integer rows); public List <T> find (String hql, List <Object> param, Integer page, Integer rows) {if (page = null | page <1) {page = 1 ;}if (rows = null | rows <1) {rows = 10 ;} query q = this. getCurrentSession (). createQuery (hql); if (param! = Null & param. size ()> 0) {for (int I = 0; I <param. size (); I ++) {q. setParameter (I, param. get (I) ;}} return q. setFirstResult (page-1) * rows ). setMaxResults (rows ). list ();}
One: (found which padding is set still cannot align the search box with the button)