chosen icon indicating copy to clipboard operation
chosen copied to clipboard

ajax Chosen

Open Tim-Wils opened this issue 13 years ago • 0 comments

Hi, I made a extended plugin voor chosen to search with an ajax request! It works great. The code can be cleaned up a little bit.

Elements.implement({ ajaxchosen: function(data, options){ return this.each(function(el){ if (!el.hasClass("chzn-done")) { return new Chosen.Ajax(el, data, options); } }); } });

Chosen.Ajax = new Class({

Extends: Chosen,
Implements: [Options,Events],

options: {
    url:''
},

initialize: function(elmn, data, options){
    this.parent(elmn);

    this.req = new Request.JSON({url:data.url,headers: {'X-Request': 'JSON'}});
    var that = this;
    this.req.addEvents({
        'success': function(dataa){that.reqSuccess(dataa)}
    });
},

reqSuccess: function(data){
    var items;
    var that = this;
    if (!(data != null)) {
        return;
    }

    Array.each(this.form_field.getElements('option'),function(el) {
        if (!el.get("selected")) {
            el.destroy();
        }
    });

    Object.each(data, function(el) {
        that.form_field.grab(new Element('option',{'value':el.value,'html':el.text}));
    });

    this.form_field.fireEvent("liszt:updated");
    this.search_field.set('value', this.search_field.get('prevVal'));
},
keyup_checker: function(evt){
    var field, val;
    val = evt.target.get('value');
    if (val.length < 3 || val === evt.target.get('prevVal')) {
      return false;
    }
    evt.target.set('prevVal', val);
    field = evt.target;

    this.req.send('term='+val);
}

});

Tim-Wils avatar Sep 28 '11 15:09 Tim-Wils