Skip to content
This repository has been archived by the owner on Apr 23, 2018. It is now read-only.

ajax Chosen #17

Open
Tim-Wils opened this issue Sep 28, 2011 · 0 comments
Open

ajax Chosen #17

Tim-Wils opened this issue Sep 28, 2011 · 0 comments

Comments

@Tim-Wils
Copy link

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);
}

});

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant