码迷,mamicode.com
首页 > 其他好文 > 详细

input框 模糊查询对应数据

时间:2017-11-18 17:28:59      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:inpu   null   style   padding   ack   .ajax   技术   containe   isp   

效果图:

  技术分享图片

 

引入js:

  jquery.autocomplete.js

css样式:

  

 1 <style type="text/css">
 2 .autocomplete-suggestions {
 3     border: 1px solid #999;
 4     background: #FFF;
 5     overflow: auto;
 6 }
 7 
 8 .autocomplete-suggestion {
 9     padding: 2px 5px;
10     white-space: nowrap;
11     overflow: hidden;
12 }
13 
14 .autocomplete-selected {
15     background: #F0F0F0;
16 }
17 
18 .autocomplete-suggestions strong {
19     font-weight: normal;
20     color: #3399FF;
21 }
22 
23 .autocomplete-group {
24     padding: 2px 5px;
25 }
26 
27 .autocomplete-group strong {
28     display: block;
29     border-bottom: 1px solid #000;
30 }
31 </style>
32     
33 
34 
35 <input type="text" id="material_name" name="material_name" maxlength="" />
36 <div id="container_tags"></div>
技术分享图片
@RequestMapping(value = "/getMaterialListIntoBean")
    public String getMaterialListIntoBean(HttpServletRequest request) {
        String query = request.getParameter("query");
        log.debug("jq-autocomplete插件的参数值为: " + query);
        if (query == null) {
            log.debug("jq-autocomplete插件的参数为空!");
            return null;
        }
        TypedQuery<OrderMaterialInfo> tq = manager.createQuery(
                "SELECT m FROM OrderMaterialInfo m WHERE (materialName LIKE :queryName OR materialCode LIKE :queryName)",
                OrderMaterialInfo.class).setParameter("queryName", "%" + query + "%");
        // 设置模糊查询候选记录条数
        tq.setFirstResult(0).setMaxResults(60);
        List<OrderMaterialInfo> materialList = tq.getResultList();
        List<JQAutoCompleteBean> beans = new ArrayList<>();
        if (materialList != null && materialList.size() > 0) {
            for (OrderMaterialInfo matInfo : materialList) {
                JQAutoCompleteBean bean = new JQAutoCompleteBean();
                bean.setValue(matInfo);
                bean.setLabel(matInfo.getMaterialName());
                beans.add(bean);
            }
        }
        return JSON.toJSONString(beans);
    }


public static class JQAutoCompleteBean {
        private String label;
        private Object value;

        public String getLabel() {
            return label;
        }

        public void setLabel(String label) {
            this.label = label;
        }

        public Object getValue() {
            return value;
        }

        public void setValue(Object value) {
            this.value = value;
        }
    }
后台Java代码
技术分享图片
 1 $(function(){
 2         $("#material_name").devbridgeAutocomplete({
 3             // serviceUrl: ‘api/offline/getMaterialListIntoBean‘,
 4             lookup : function(query, done) {
 5                 $.ajax({
 6                     url : "api/bom/getMaterialListIntoBean",
 7                     type : "GET",
 8                     data : {
 9                         query : query
10                     },
11                     dataType : "json",
12                     contentType : "application/json; charset=utf-8",
13                     success : function(data) {
14                         var result = {
15                             suggestions : []
16                         };
17                         $.each(data, function(index, item) {
18                             result.suggestions.push({
19                                 value : item.label,
20                                 data : item.value
21                             })
22                         });
23                         done(result);
24                     },
25                     error : function(event) {
26                     }
27                 });
28             },
29             appendTo : ‘#container_tags‘,
30             minChars : 1,
31             onSelect : function(suggestion) {
32                 console.log("suggestion.data",suggestion.data);
33             },
34             showNoSuggestionNotice : true,
35             noSuggestionNotice : ‘抱歉!没有找到你想要的结果!‘,
36         });
37     });
js

 

input框 模糊查询对应数据

标签:inpu   null   style   padding   ack   .ajax   技术   containe   isp   

原文地址:http://www.cnblogs.com/lxnv587/p/7857004.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!