Adding two lists to selectize, the first one should be selected by default

那年仲夏 提交于 2019-12-24 07:06:01

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!