EasyUI Forum
June 19, 2024, 05:15:08 PM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News:
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: 终于完美实现了 combobox 汉字拼音检索, 英文检索也支持, 并且效果更好  (Read 24546 times)
iamjxc
Jr. Member
**
Posts: 80


View Profile
« on: April 20, 2014, 02:50:48 AM »

在检索时, 自动高亮第一条符合的记录(同时保留textbox的内容不变),
当 panel 关闭时, 才更新 textbox 的值
对 easyui 改动较多, 希望未来版本直接合并进去.

(function($){
   
   $.extend($.fn.combobox.defaults, {
      filter: filterComboboxData,
      onHidePanel: onComboboxHidePanel
   });

   $.extend($.fn.combobox.defaults.keyHandler,{
      query: doQuery
   });
   
   function doQuery(q, evt){
      var target = this;
      var el = $(this);
      
      var state = $.data(target, 'combobox');
      var opts = state.options;
      
      if (opts.multiple && !q){
         el.combobox("setValues", [], true);
      } else {
         el.combobox("setValues", [q], true);
      }
      
      if (opts.mode == 'remote'){
         
      } else {
         var panel = el.combo('panel');
         panel.find('div.combobox-item-selected,div.combobox-item-hover').removeClass('combobox-item-selected combobox-item-hover');
         panel.find('div.combobox-item,div.combobox-group').hide();
         var data = opts.data;
         var vv = [];
         var qq = opts.multiple ? q.split(opts.separator) : [q];
         $.map(qq, function(q){
            q = $.trim(q);
            for(var i=0; i<data.length; i++){
               var row = data;
               if (opts.filter.call(el, q, row)){
                  var v = row[opts.valueField];
                  var s = row[opts.textField];
                  var g = row[opts.groupField];
                  var item = opts.finder.getEl(target, v).show();
                  vv.push(v);
//                  if (s.toLowerCase() == q.toLowerCase()){
//                     vv.push(v);
//                     item.addClass('combobox-item-selected');
//                  }

               }
            }
         });
         
         // 默认高亮第一项
         if (vv.length > 0 && q){
            var v = vv[0];
            var item = opts.finder.getEl(target, v);
            item.addClass('combobox-item-selected');
            
            // 只改value, 不改text
            el.combo("setValues", [v], true);
         }
         
//         $(target).combo('setValues', vv);

      }
   }
   
})(jQuery);




   function filterComboboxData(input, row) {
      // 清空内容, 则不过滤
      if (input == "") return true;
      
      input = input.toUpperCase();
      
      // 先判断显示值
      var opts = $(this).combobox('options');
      var text = row[opts.textField].toUpperCase();
      if (text.indexOf(input) >= 0) {
         return true;
      }
      
      // 优先用top里共享的拼音检索函数(避免每个页面都加载拼音js)
      var spell = top.getFirstSpellLetter;
      if (!spell) spell = getFirstSpellLetter;
      if (spell) {
         var letter = spell(text);
         //alert(letter+":"+input)
         if (letter.indexOf(input) >= 0) {
            return true;
         }
         return false;
      }

      // 默认不过滤
      return true;
   }

   function onComboboxHidePanel(){
      var el = $(this);
      el.combobox('textbox').focus();
      
      // 检查录入内容是否在数据里            
      var opts = el.combobox("options");
      var data = el.combobox("getData");
      var value = el.combobox("getValue");
                  
      // 有高亮选中的项目, 则不进一步处理
      var panel = el.combobox("panel");
      var items = panel.find(".combobox-item-selected");
      if (items.length > 0) {
         var values = el.combobox("getValues");
         el.combobox("setValues", values);
         return;
      }
      
      var allowInput = opts.allowInput;            
      if (allowInput){
         // 允许录入, 并且当前下拉没内容(过滤掉了), 则加入下拉列表里
         var idx = data.length;
         
         data[idx] = [];
         data[idx][opts.textField] = value;
         data[idx][opts.valueField] = value;
         
         el.combobox("loadData", data);               
      }else{
         // 不允许录入任意项, 则清空
         el.combobox("clear");
      }

   }
   

Logged
zh_CN
Newbie
*
Posts: 32



View Profile Email
« Reply #1 on: April 20, 2014, 05:59:40 AM »

我收下了啊,十分感谢分享
Logged

From the great China
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines Valid XHTML 1.0! Valid CSS!