easyui searchBox 搜尋方塊
<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;"><!-- 這裡的padding-top是讓搜尋欄向下點,也就是與"添加"等按鈕對齊,但在HTML裡面不好用,在jsp頁面中已測試可用 --><input id="searchBox" class="easyui-searchbox" searcher="searchData"prompt="請輸入查詢內容" style="width:300px"></input><div id="searchMenu" style="width:100px"></div></div><div id="menu" class="easyui-menu" style="width:120px;display: none;"><div onclick="append();" data-options="iconCls:'icon-add'">增加</div><div onclick="removeData();" data-options="iconCls:'icon-remove'">刪除</div><div onclick="edit();" data-options="iconCls:'icon-edit'">編輯</div></div></div></body>
2.js
//迴圈列名,產生搜尋的下拉式清單 var fields = $('#datagrid').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>";};$('#searchMenu').html($('#searchMenu').html()+muit);$('#searchBox').searchbox({ menu:'#searchMenu' }); //擷取產生的搜尋方塊var tSearch = $("#search"); //將產生好的搜尋方塊放入工具列 $(".datagrid-toolbar").append(tSearch);
3. 搜尋方法
function searchData(value,name){ $('#datagrid').datagrid('load', { "searchKey": name, "searchValue": value });}
4.searchKey是要查詢的欄位名,searchValue是這個欄位名的值,後台接收到這兩個參數然後自己拼接sql查詢
a.這是查詢總記錄的
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.這是查詢列表
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;}
封裝的DAO
/** * 查詢集合(帶分頁) * * @param hql * @param param * @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();}
一張:( 發現設定了哪個padding還是不能讓搜尋方塊和按鈕對齊)