Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selectize.js manually add some items

I want add some items to a selectized input after user clicks on a button. The input data are loaded via Ajax. When I call addItem(value) no thing happens. But if I try to type some string in the input it loads data and after this addItem(value) will works.

https://github.com/brianreavis/selectize.js/blob/master/docs/api.md

like image 500
Handsome Nerd Avatar asked Dec 01 '13 09:12

Handsome Nerd


2 Answers

This plugin does not attempt to load an item metadata from the server. You need to first add an option using addOption() method. Next, you can use addItem().

v.selectize.addOption({value:13,text:'foo'}); //option can be created manually or loaded using Ajax v.selectize.addItem(13);  
like image 145
Handsome Nerd Avatar answered Oct 08 '22 19:10

Handsome Nerd


You can add options like this:

var $select = $(document.getElementById('mySelect')).selectize(options); var selectize = $select[0].selectize; selectize.addOption({value: 1, text: 'whatever'}); selectize.refreshOptions(); 

This only adds the option as possible selection. Now you can use addItem to add the new option to the list:

selectize.addItem(1); 

This does not need a refresh function. You do not need to use "refreshOptions" if you add the new option immediately.

like image 24
andyrandy Avatar answered Oct 08 '22 18:10

andyrandy