Jquery-ui,搜索框搜索历史、联想词插件

1、插件引入

引用,插件下载

<link rel="stylesheet" href="${commonStatic}/common/jquery/css/autocomplete.min.css">
<script src="${commonStatic}/common/jquery/ui/autocomplete.min.js"></script>

不要使用

<link rel="stylesheet" href="${webStatic}/common/plugins/jquery-ui-1.12.1/jquery-ui.min.css">
<script src="${webStatic}/common/plugins/jquery-ui-1.12.1/jquery-ui.min.js"></script>

后者可能会出现样式问题

2、代码
$("#searchOeInput").autocomplete({
      minLength: 0,
      source: function(request,response){
         if(request.term.length == 0){
           //查历史搜索记录
           $.ajax({
               type: 'post',
               url: '/portal/querySearchHistory',
               data:{'searchKeyword':request.term,'searchType':'epc','count':10},
               dataType: 'json',
               success:function(data){
                 var keywordOjects = data.keywordList,
                       result = [];
                 if(keywordOjects){
                   for(var i=0; i<keywordOjects.length; i++) {  
                         result.push(keywordOjects[i].searchKeyword);
                    }
                   response(result);//展现数据
                 }
               },
               error: function(XMLHttpRequest, textStatus, errorThrown){
                 console.log(textStatus);
               }
            });
         }else{
           //查联想词
           $.ajax({
               type: 'get',
               url: '/maindata/restApi/v1/oe/oeStandardNames',
               data:{'oeName':request.term,'vin':vin},
               dataType: 'json',
               success:function(result){
                 if(result && result.data){
                   response(result.data);
                 }
               },
               error: function(XMLHttpRequest, textStatus, errorThrown){
                 console.log(textStatus);
               }
            });
         }
       },
       select: function( event, ui){//从下拉列表中选择后触发
         //判断返回数据是历史记录还是联想词
         if(ui.item.hasOwnProperty('standard')){
           $(this).blur();
           searchEpcParts(ui.item.value);
         }else{
           event.preventDefault();//阻止默认动作(value值填充到input中)
           $(this).val(ui.item.value).trigger('input');//input中内容改变时触发事件
         }
       }
    }).focus(function() {//结合minLength: 0,focus时就触发事件
        $(this).autocomplete("search", $(this).val());
    }).data("ui-autocomplete")._renderItem = function(ul, item) {//_renderItem用于自定义下拉框样式
      //区分搜索历史记录和联想词
      if(item && item.hasOwnProperty('standard')){
        var html = '<a><span style="float:left;">' + item.alias + '</span><span style="float:right;font-weight:bold;">' + item.standard + '</span></a>';
            item.value = item.standard;
        return $("<li>").data("item.autocomplete", item)
                  .append(html)
                  .appendTo(ul);
      }
      return $("<li>").data("item.autocomplete", item)
              .append(item.value)
              .appendTo(ul);
    };
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值