问题
Hi I've a litle problem with selectize, namely how to add two lists and he first one should be selected by default and enduser can select items from second list in same form. Below you can find my code. If I add two times options selectize takes second one only).
$(document).ready(function(){
lol = "lol1 , lol2 , lol3"
var lol = lol.split(',');
var lol = lol.map(function(x) { return { item: x}; });
console.log(lol)
console.log(typeof(lol))
wtf = "wtf1 , wtf2 , wtf3"
var wtf = wtf.split(',');
var wtf = wtf.map(function(x) { return { item: x}; });
console.log(wtf)
console.log(typeof(wtf))
$('#show_tags').selectize({
plugins: ['remove_button', 'restore_on_backspace'],
select: true,
delimiter: ',',
maxItems: null,
options: lol,
options: wtf,
labelField: 'item',
valueField: 'item',
searchField: 'item',
create: true
});
});
Ideas?
回答1:
You need to use items
to provide an array of values for the options that should be selected by default (instead of using two options
arrays). Option values are determined by your valueField
setting.
For example:
$('#select-id').selectize({
items: ['1', '2'], // preselected options values
options: [
{value: '1', name: 'Item 1'}, // this option will be preselected
{value: '2', name: 'Item 2'}, // this option will be preselected
{value: '3', name: 'Item 3'},
{value: '4', name: 'Item 4'}
],
valueField: 'value',
labelField: 'name',
searchField: ['name'],
delimiter: ',',
select: true,
create: true,
maxItems: null
});
来源:https://stackoverflow.com/questions/47672716/adding-two-lists-to-selectize-the-first-one-should-be-selected-by-default