Skip to content Skip to sidebar Skip to footer

Select2 Change Ajax Url

Im working with knockout.js and select2 plugin. Im trying to change the select2 ajax url based on a value of an observable. for example if based on some selection i do ajax call to

Solution 1:

I've finally managed to solve this one.

From what i could read from the select2 documentation, you should pass string or function to ajax url parameter. So this is what i've done I've written a function that returns value of the observable (which is the url):

self.returnUrl = function() {
    returnself.dynamicUrl();
};

And then i pass that function to my custom binding options:

<input data-bind="combobox: { ... sourceUrl: $data.returnUrl }, value: ApplyToSubject"type="hidden" >

Then the custom binding works the same as in the code in the question, with a small change:

...
ajax: {
     url: sourceUrl <- this is the returnUrl function
...
}

Post a Comment for "Select2 Change Ajax Url"