Bug: 'items' option is not setting the default value
Based on your documentation, we should be able to set the default value(s) for when the select field first renders, by adding an "items" property with an array of default values. Sounds really easy (and necessary).
But for some reason this does not work. The Selectize works, it pulls the options dynamically from the API, and it does filter when I type a name (does additional AJAX calls). But when we specify the default value, it does not correlate that id with the values of the dynamic options in order to select the correct option when I first load the page.
myEl.selectize({
items: [36153],
valueField: 'id',
labelField: 'title',
searchField: ['id','title'],
maxItems: 1,
create: true,
persist: false,
options: this.collection.toJSON(),
render: {
option: function (model, escape) {
return view.template({model: model});
}
},
load: function (query, callback) {
if (!query.length) return callback();
view.collection.fetch(options);
callback(view.collection.toJSON());
}
});
We have also tried manually setting the value and that works. But this is a hack that shouldn't be necessary for something so basic as setting default values. Note this only works if we setValue for each item as an array.
// initiate Selectize
myEl.selectize(myOptions);
// Set Default Values AFTER they are Loaded
myEl.selectize.on('load', function() {
// dynamically set the values from the items
if(myOptions.items != null && myOptions.items.length > 0) {
_.each(myOptions.items, function(el) {
myEl[0].selectize.setValue([el]);
});
}
});
NOTE: at one point in the API docs on under 'Properties', it suggests that the second parameter to selectize is 'items', and below that on the page is says items are the selected items. e.g.
myEl.selectize(options, items);
I've tried passing in the selected items that way, but it still doesn't do anything.
The documentation no longer shows that items is available as a separate parameter to selectize().
Passing the initial selections as selectize({ items: [ 'value1', 'value2' ] }) works for me; perhaps you could set up an example in CodePen or JSFiddle that shows the problem?
I have the same issue.
$('#input-tags').selectize({
valueField: 'id',
labelField: 'name',
closeAfterSelect: true,
items: [{ id: 1, name: 'test' }, { id: 2, name: 'test2' }],
options: [],
create: false,
onItemAdd: function(value) { tagAdded(value) },
onItemRemove: function(value) { tagRemoved(value) },
load: function (query, callback) { // ajax query },
});
the items property expects an array of values not an array of objects. quote: "Items: the list of selected options. Or more exactly, the list of the values of the selected options."
I had the same issue, items doesn't work so I have used the below workaround:
var $eventTypes = $('#event-types');
var preloadedItems = $eventTypes.val().split(',');
$eventTypes.val('');
$eventTypes.selectize({
valueField: 'id',
labelField: 'name',
searchField: 'name',
sortField: 'name',
create: false,
preload: true,
render: {
option: function(item, escape) {
return '<div><span class="name">' + escape(item.name) + '</span></div>';
}
},
load: function(query, callback) {
$.ajax({
url: 'event-types',
type: 'GET',
error: function() {
callback();
},
success: function(res) {
callback(res);
}
});
},
onLoad: function(_) {
var selectize = $eventTypes[0].selectize;
selectize.setValue(preloadedItems);
}
});
I was wondering if this is still the situation with going about pre-selected dynamic/loaded items? I wound up setting it up with options and then using the onLoad function like this:
$('myselect').selectize({
plugins: ['remove_button', 'restore_on_backspace', 'drag_drop'],
create: false,
preload: true,
valueField: 'id',
labelField: 'name',
searchField: 'name',
sortField: 'name',
options: aryobjs,
render: {
option: function (item, escape) {
return '<div>' +
'<span class="title">' +
'<span class="name"></i>' + escape(item.name) + '</span>' +
'</span>' +
'</div>';
}
},
onLoad: function (_) {
this.setValue((Array.isArray(aryobjs) ? aryobjs.map(function(item){ return item.id}).join(',') : ""));
},
These are the conditions I found to work:
- Utilize options on initializing selectize as in aryobj above and set the values within your input/select field (just the ids)
- Utilize options on init and then set them within the onLoad function as above but you will also trigger a 'onChange' event when the plugin initializes.
This is just a combination of the above but seems to be an efficient combination of answers so far. Thanks @lukaszlenart and @mcavalletto for your posts. You saved me a lot of time.
to use items param, just set it to an array of the ids ONLY in the list of the items:
For example, if your list containts items [{id:'a', name:'Bob'}, {id:'b', name:'Mary'}] then generate an array of ["a","b"] and set using items: ["a","b"] it will show up as "Bob" and "Mary" in the selectize list.
The 'items' property needs to be an array of values, but you also have to provide the 'options' property in order for selectize to accept the initially selected items. Otherwise, it just ignores them. Of course, the 'options' array of objects needs to have the selected values given through the 'items'.
This makes sense, however it needs to be documented properly.
@gorgi could you give an example from which we can see how to use all of it properly?
Using the original example, this works:
myEl.selectize({
options: this.collection.where({id: 36153}),
items: [36153],
valueField: 'id',
labelField: 'title',
searchField: ['id','title'],
maxItems: 1,
create: true,
persist: false,
options: this.collection.toJSON(),
render: {
option: function (model, escape) {
return view.template({model: model});
}
},
load: function (query, callback) {
if (!query.length) return callback();
view.collection.fetch(options);
callback(view.collection.toJSON());
}
});
This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days