typeahead搜索提示

来源:互联网 发布:淘宝详情页图片下载器 编辑:程序博客网 时间:2024/06/05 23:16




一:引入样式文件与js

     <link href="~/Content/bootstrap.min.css" rel="stylesheet" />

     <script type="text/JavaScript" src="~/Content/js/bootstrap-typeahead.js"></script>


二:建立一个文本框

<input type="text" id="s_mdport" />

  注意样式冲突一般该input父级对a的样式可能会影响到他



三:绑定typeahead

[javascript] view plain copy
  1. $('#s_mdport').typeahead({  
  2.             items: 20,  
  3.             source: function (query, process) {  
  4.                 GetMdPorts(query, function (billNumbers) {  
  5.                     var results = $.map(billNumbers, function (billNumber) {  
  6.                         if (billNumber.NameEN != '' && billNumber.NameEN != null) {  
  7.                             return billNumber.NameEN + ',' + billNumber.NameCN + '';  
  8.                         } else {  
  9.                             return billNumber.NameEN;  
  10.                         }  
  11.                     });  
  12.                     process(results);  
  13.                 });  
  14.             },  
  15.             highlighter: function (item) {  
  16.                 return item;  
  17.             },  
  18.             updater: function (item) {//选中  
  19.                 console.log("'" + item + "' selected.");  
  20.                 //tem = item.substring(0, item.indexOf(','));  
  21.                 $(':submit').focus();  
  22.                 return item;  
  23.             }  
  24.         });  
[javascript] view plain copy
  1. function GetMdPorts(query, _func) {  
  2.   
  3.     var billNumbers = $.parseJSON('[{"NameEN":"ABIDJAN","NameCN":"阿比让"},{"NameEN":"B","NameCN":"香蕉"}]');  
  4.     _func(billNumbers);  
  5.   
  6.     //真正用的时候从后台取数据  
  7.     //$.ajax({  
  8.     //    url: '/Home/GetGByKey?key=' + query,  
  9.     //    type: 'post',  
  10.     //    dataType: "json",  
  11.     //    success: function (result) {  
  12.     //        billNumbers = result;  
  13.     //        _func(billNumbers);  
  14.     //    }  
  15.     //});  
  16. }  


五:特殊需求默认提示

      

[javascript] view plain copy
  1. $('#s_ship_date').typeahead({  
  2.             items: 20,  
  3.             source: function (query, process) {  
  4.   
  5.                 var reg = /^(\d{1,7})/;  
  6.                 if (!query.match(reg))  
  7.                     $('#s_ship_date').val("周");//除了1-7的数字,不管输入什么弹出默认的提示  
  8.   
  9.                 GetShips2(query, function (billNumbers) {  
  10.                     var results = $.map(billNumbers, function (billNumber) {  
  11.                         return billNumber.Value;  
  12.                     });  
  13.                     process(results);                    
  14.                 });  
  15.             },  
  16.             highlighter: function (item) {  
  17.                 return item;  
  18.             },  
  19.             updater: function (item) {  
  20.                 $(':submit').focus();  
  21.                 return item;  
  22.             }        
  23.         });  
  24.   
  25.         function GetShips2(query, _func) {  
  26.             var billNumbers = $.parseJSON('[{"Value":"周一,1"},{"Value":"周二,2"},{"Value":"周三,3"},{"Value":"周四,4"},{"Value":"周五,5"},{"Value":"周六,6"},{"Value":"周日,7"}]');  
  27.             _func(billNumbers);  
  28.         }  

六:修改一些样式,宽度,背景色等

        效果:

        

     1:修改颜色

          把css里边的默认蓝色全部替换成#13c0a2,下次要修改样式直接在这个基础上把#13c0a2全部替换成其他颜色就行了

     2:修改宽度

          .dropdown-menu{position:absolute;top:100%;left:0;z-index:1000;display:none;float:left;min-width:330px;......}

          min-width:330p修改成想要的宽度就可以了

      3:右边出现文字,就是就是拼一个a标签

       

[javascript] view plain copy
  1. $("#pols").typeahead({  
  2.             items: 20,  
  3.             source: function (query, process) {  
  4.                 index.GetMdPorts(query, function (billNumbers) {  
  5.                     var results = $.map(billNumbers, function (billNumber) {  
  6.                         if (billNumber.Value != '' && billNumber.Value != null) {  
  7.                             return billNumber.Value + "<a style='float:right'>国家</a>";//拼一个a标签并居右  
  8.                         } else {  
  9.                             return billNumber.Value;  
  10.                         }  
  11.                     });  
  12.                     process(results);  
  13.                 });  
  14.             },  
  15.             highlighter: function (item) {  
  16.                 return item;  
  17.             },  
  18.             updater: function (item) {//选中    
  19.                 //console.log("'" + item + "' selected.");  
  20.                 $(':submit').focus();  
  21.                 return item;  
  22.             }  
  23.         });  
        });


七:点击时弹出提示


1:要监听点击方法

.on('click', $.proxy(this.click, this)) //要监听点击事件不然就不会执行了


2:点击方法里边做处理,调用回调函数,他默认是在lookup事件里写的,参照他写就可以

 click: function (e) {  //aj点击时的提示


      var items
      this.query = this.$element.val()


      //if (!this.query || this.query.length < this.options.minLength) { //这个是限制为空就不查询了,取消掉
      //    return this.shown ? this.hide() : this
      //}


      if (this.shown == false) {


          items = $.isFunction(this.source) ? this.source(this.query, $.proxy(this.process, this)) : this.source


          return items ? this.process(items) : this
      }
      else
      {
          var val = this.$menu.find('.active').attr('data-value')
          this.$element
            .val(this.updater(val))
            .change()
          return this.hide()
      }
      //e.stopPropagation()
      //e.preventDefault()
      //this.select()
      //this.$element.focus()
    }



一:引入样式文件与js

     <link href="~/Content/bootstrap.min.css" rel="stylesheet" />

     <script type="text/JavaScript" src="~/Content/js/bootstrap-typeahead.js"></script>


二:建立一个文本框

<input type="text" id="s_mdport" />

  注意样式冲突一般该input父级对a的样式可能会影响到他



三:绑定typeahead

[javascript] view plain copy
  1. $('#s_mdport').typeahead({  
  2.             items: 20,  
  3.             source: function (query, process) {  
  4.                 GetMdPorts(query, function (billNumbers) {  
  5.                     var results = $.map(billNumbers, function (billNumber) {  
  6.                         if (billNumber.NameEN != '' && billNumber.NameEN != null) {  
  7.                             return billNumber.NameEN + ',' + billNumber.NameCN + '';  
  8.                         } else {  
  9.                             return billNumber.NameEN;  
  10.                         }  
  11.                     });  
  12.                     process(results);  
  13.                 });  
  14.             },  
  15.             highlighter: function (item) {  
  16.                 return item;  
  17.             },  
  18.             updater: function (item) {//选中  
  19.                 console.log("'" + item + "' selected.");  
  20.                 //tem = item.substring(0, item.indexOf(','));  
  21.                 $(':submit').focus();  
  22.                 return item;  
  23.             }  
  24.         });  
[javascript] view plain copy
  1. function GetMdPorts(query, _func) {  
  2.   
  3.     var billNumbers = $.parseJSON('[{"NameEN":"ABIDJAN","NameCN":"阿比让"},{"NameEN":"B","NameCN":"香蕉"}]');  
  4.     _func(billNumbers);  
  5.   
  6.     //真正用的时候从后台取数据  
  7.     //$.ajax({  
  8.     //    url: '/Home/GetGByKey?key=' + query,  
  9.     //    type: 'post',  
  10.     //    dataType: "json",  
  11.     //    success: function (result) {  
  12.     //        billNumbers = result;  
  13.     //        _func(billNumbers);  
  14.     //    }  
  15.     //});  
  16. }  


五:特殊需求默认提示

      

[javascript] view plain copy
  1. $('#s_ship_date').typeahead({  
  2.             items: 20,  
  3.             source: function (query, process) {  
  4.   
  5.                 var reg = /^(\d{1,7})/;  
  6.                 if (!query.match(reg))  
  7.                     $('#s_ship_date').val("周");//除了1-7的数字,不管输入什么弹出默认的提示  
  8.   
  9.                 GetShips2(query, function (billNumbers) {  
  10.                     var results = $.map(billNumbers, function (billNumber) {  
  11.                         return billNumber.Value;  
  12.                     });  
  13.                     process(results);                    
  14.                 });  
  15.             },  
  16.             highlighter: function (item) {  
  17.                 return item;  
  18.             },  
  19.             updater: function (item) {  
  20.                 $(':submit').focus();  
  21.                 return item;  
  22.             }        
  23.         });  
  24.   
  25.         function GetShips2(query, _func) {  
  26.             var billNumbers = $.parseJSON('[{"Value":"周一,1"},{"Value":"周二,2"},{"Value":"周三,3"},{"Value":"周四,4"},{"Value":"周五,5"},{"Value":"周六,6"},{"Value":"周日,7"}]');  
  27.             _func(billNumbers);  
  28.         }  

六:修改一些样式,宽度,背景色等

        效果:

        

     1:修改颜色

          把css里边的默认蓝色全部替换成#13c0a2,下次要修改样式直接在这个基础上把#13c0a2全部替换成其他颜色就行了

     2:修改宽度

          .dropdown-menu{position:absolute;top:100%;left:0;z-index:1000;display:none;float:left;min-width:330px;......}

          min-width:330p修改成想要的宽度就可以了

      3:右边出现文字,就是就是拼一个a标签

       

[javascript] view plain copy
  1. $("#pols").typeahead({  
  2.             items: 20,  
  3.             source: function (query, process) {  
  4.                 index.GetMdPorts(query, function (billNumbers) {  
  5.                     var results = $.map(billNumbers, function (billNumber) {  
  6.                         if (billNumber.Value != '' && billNumber.Value != null) {  
  7.                             return billNumber.Value + "<a style='float:right'>国家</a>";//拼一个a标签并居右  
  8.                         } else {  
  9.                             return billNumber.Value;  
  10.                         }  
  11.                     });  
  12.                     process(results);  
  13.                 });  
  14.             },  
  15.             highlighter: function (item) {  
  16.                 return item;  
  17.             },  
  18.             updater: function (item) {//选中    
  19.                 //console.log("'" + item + "' selected.");  
  20.                 $(':submit').focus();  
  21.                 return item;  
  22.             }  
  23.         });  
        });


七:点击时弹出提示


1:要监听点击方法

.on('click', $.proxy(this.click, this)) //要监听点击事件不然就不会执行了


2:点击方法里边做处理,调用回调函数,他默认是在lookup事件里写的,参照他写就可以

 click: function (e) {  //aj点击时的提示


      var items
      this.query = this.$element.val()


      //if (!this.query || this.query.length < this.options.minLength) { //这个是限制为空就不查询了,取消掉
      //    return this.shown ? this.hide() : this
      //}


      if (this.shown == false) {


          items = $.isFunction(this.source) ? this.source(this.query, $.proxy(this.process, this)) : this.source


          return items ? this.process(items) : this
      }
      else
      {
          var val = this.$menu.find('.active').attr('data-value')
          this.$element
            .val(this.updater(val))
            .change()
          return this.hide()
      }
      //e.stopPropagation()
      //e.preventDefault()
      //this.select()
      //this.$element.focus()
    }

原创粉丝点击