easyUI searchBox搜尋方塊的實現

來源:互聯網
上載者:User

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還是不能讓搜尋方塊和按鈕對齊)








相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.