javascript - How to set default options fro selectize, When options load from server -


i try create select input, forms , used selectize.js,

this good, have little problem that.

i loaded options, server add data, , okay.

when try create data editor, must load last data in form inputs.

in select box, loaded options, , want set of them selected default .

i wrote code:

var $select = $(this).selectize({ create: false, valuefield: 'id', labelfield: 'title', searchfield: ['title'], plugins: ['remove_button'], preload: true, render: {     item: function (item, escape) {     return '<div>' +          (item.title ? '<span class="title">' + escape(item.title) + '</span>' : '') +          '</div>';     },     options: function (item, escape) {         return '<div>' +             (item.id ? '<span class="title">' + escape(item.id) + '</span>' : '') +             '</div>';     }, }, ondelete: function (values) {     return confirm(values.length > 1 ? 'آیا شما میخواهید  ' + values.length + ' مورد را حذف کنید؟' : 'آیا شما از حذف این مورد اطمینان دارید؟'); }, load:function(query, callback){     $.getjson( url,{mdl:mdl,action:action,id:id}, function( res ) {             callback((res.select))     }); } 

});

and tried code set default :

var selectize = $select[0].selectize; selectize.setvalue("1"); 

it's not working ... tried code :

var selectize = $select[0].selectize; var opt = {id:1,title:"عمومی"} selectize.addoption(opt); selectize.setvalue("1"); 

it works , but, in solution, must add selected option twice firstly server , secondly in javascript.

please me. all.

i found solution :

changing code solved :

var $select = $(this).selectize({             create: false,             valuefield: 'value',             labelfield: 'title',             searchfield: ['title'],             maxitems: 3,             plugins: ['remove_button'],             preload: true,             render: {                 item: function (item, escape) {                     return '<div>' +                     (item.title ? '<span class="title">' + escape(item.title) + '</span>' : '') +                     '</div>';                 },                 options: function (item, escape) {                     return '<div>' +                     (item.id ? '<span class="title">' + escape(item.id) + '</span>' : '') +                     '</div>';                 },             },             ondelete: function (values) {                 return confirm(values.length > 1 ? 'آیا شما میخواهید  ' + values.length + ' مورد را حذف کنید؟' : 'آیا شما از حذف این مورد اطمینان دارید؟');             },         });         var selectize = $select[0].selectize;         $.getjson( url,{mdl:mdl,action:action,id:id}, function( res ) {             selectize.addoption(res.select);             selectize.additem(res.select);              var value = [1,2,3,4];             selectize.setvalue(value);          }); 

Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -