Mootools 过滤器插件

发布于 2024-12-22 11:57:53 字数 248 浏览 0 评论 0原文

也许你可以帮助我:

请检查这个演示:插件演示 插件描述: 插件描述

如何在列表顶部仅显示匹配的项目而不隐藏输入时匹配? 谢谢!

may be you can help me:

Please check this demo: Plugin demo
Plugin description: Plugin description

How can I show only matching items at the top of the list and hide not matching during input typing?
Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

﹏雨一样淡蓝的深情 2024-12-29 11:57:53

当然,您可以使用 onShow 和 onHide 处理程序来完成此操作。
看这里:http://jsfiddle.net/ypfPY/3/

var myFilter = new ElementFilter('search-term', '#my-list li', {
    trigger: 'keyup',
    cache: false,
    onShow: function(element) {
       element.setStyle('display', 'block');
    },
    onHide: function(element) {
       element.setStyle('display', 'none');
    }
});

坦白说,我遇到了一套ElementFilter 源中的错误(显示所有项目时首先检索“显示”和 false 而不是 true 的 matchOverride 的错误),因此,您最好从这里获取源:

var ElementFilter = new Class({

  // implements
  Implements: [Options,Events],

  // options
  options: {
    cache: true,
    caseSensitive: false,
    ignoreKeys: [13, 27, 32, 37, 38, 39, 40],
    matchAnywhere: true,
    property: 'text',
    trigger: 'mouseup',
    onStart: $empty,
    onShow: $empty,
    onHide: $empty,
    onComplete: $empty
  },

  //initialization
  initialize: function(observeElement,elements,options) {
    //set options
    this.setOptions(options);
    //set elements and element
    this.observeElement = document.id(observeElement);
    this.elements = $(elements);
    this.matches = this.elements;
    this.misses = [];
    //start the listener
    this.listen();
  },

  //adds a listener to the element (if there's a value and if the event code shouldn't be ignored)
  listen: function() {
    //add the requested event
    this.observeElement.addEvent(this.options.trigger,function(e) {
    //if there's a value in the box...
    if(this.observeElement.value.length) {
      //if the key should not be ignored...
      if(!this.options.ignoreKeys.contains(e.code)) {
        this.fireEvent('start');
        this.findMatches(this.options.cache ? this.matches : this.elements);
        this.fireEvent('complete');
      }
    }
    else{
      //show all of the elements - changed to true
      this.findMatches(this.elements,true);
    }
  }.bind(this));
},

//check for matches within specified elements
findMatches: function(elements,matchOverride) {
  //settings
  var value = this.observeElement.value;
  var regExpPattern = this.options.matchAnywhere ? value : '^' + value;
  var regExpAttrs = this.options.caseSensitive ? '' : 'i';
  var filter = new RegExp(regExpPattern, regExpAttrs);
  var matches = [];                
  //recurse
  elements.each(function(el){
    var match = matchOverride == undefined ? filter.test(el.get(this.options.property)) : matchOverride;
   //if this element matches, store it...
   if(match) { 
     // default value added  
     if(!el.retrieve('showing', true)){
       this.fireEvent('show',[el]);
     }
     matches.push(el); 
     el.store('showing',true);
   }
   else {
     if(el.retrieve('showing', true)) {
       this.fireEvent('hide',[el]);
     }
     el.store('showing', false);
   }
   return true;
 }.bind(this));
 return matches;
 }
});

You can do it using onShow and onHide handlers, of course.
See here: http://jsfiddle.net/ypfPY/3/

var myFilter = new ElementFilter('search-term', '#my-list li', {
    trigger: 'keyup',
    cache: false,
    onShow: function(element) {
       element.setStyle('display', 'block');
    },
    onHide: function(element) {
       element.setStyle('display', 'none');
    }
});

Frankly speaking, I encountered a set of bugs in ElementFilter source (bug on firstly retrieving "showing" and false instead of true for matchOverride when showing all items), so, you better get source from here:

var ElementFilter = new Class({

  // implements
  Implements: [Options,Events],

  // options
  options: {
    cache: true,
    caseSensitive: false,
    ignoreKeys: [13, 27, 32, 37, 38, 39, 40],
    matchAnywhere: true,
    property: 'text',
    trigger: 'mouseup',
    onStart: $empty,
    onShow: $empty,
    onHide: $empty,
    onComplete: $empty
  },

  //initialization
  initialize: function(observeElement,elements,options) {
    //set options
    this.setOptions(options);
    //set elements and element
    this.observeElement = document.id(observeElement);
    this.elements = $(elements);
    this.matches = this.elements;
    this.misses = [];
    //start the listener
    this.listen();
  },

  //adds a listener to the element (if there's a value and if the event code shouldn't be ignored)
  listen: function() {
    //add the requested event
    this.observeElement.addEvent(this.options.trigger,function(e) {
    //if there's a value in the box...
    if(this.observeElement.value.length) {
      //if the key should not be ignored...
      if(!this.options.ignoreKeys.contains(e.code)) {
        this.fireEvent('start');
        this.findMatches(this.options.cache ? this.matches : this.elements);
        this.fireEvent('complete');
      }
    }
    else{
      //show all of the elements - changed to true
      this.findMatches(this.elements,true);
    }
  }.bind(this));
},

//check for matches within specified elements
findMatches: function(elements,matchOverride) {
  //settings
  var value = this.observeElement.value;
  var regExpPattern = this.options.matchAnywhere ? value : '^' + value;
  var regExpAttrs = this.options.caseSensitive ? '' : 'i';
  var filter = new RegExp(regExpPattern, regExpAttrs);
  var matches = [];                
  //recurse
  elements.each(function(el){
    var match = matchOverride == undefined ? filter.test(el.get(this.options.property)) : matchOverride;
   //if this element matches, store it...
   if(match) { 
     // default value added  
     if(!el.retrieve('showing', true)){
       this.fireEvent('show',[el]);
     }
     matches.push(el); 
     el.store('showing',true);
   }
   else {
     if(el.retrieve('showing', true)) {
       this.fireEvent('hide',[el]);
     }
     el.store('showing', false);
   }
   return true;
 }.bind(this));
 return matches;
 }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文