Hi again - based on your "Movies" example, I have a [select] box pulling in remote data. What's the best way to set a default value? Could it happen using the HTML element itself, it would it need to happen from within the selectize options object?
I'm having trouble with this too. Is there a way to just set the first option as selected?
I am a newbie and might have not seen something obvious, but i have the same problem: I need to set default values (for an edit view) and i haven't found any solution. I know it would be possible with some JS (addOption, setValue) but this is not a nice way. Like @ryanintn proposed, would be great if one could just use the first option as a default value
I need the saved database value to be loaded from remote in an edit form. There is a custom renderer, so [name, id] pair is not working out. This alone could be a deal-breaker for Selectize.
anyone found out how to set a default value?
onLoad: function(value){
// set default ;
}
?
2014-09-16 15:37 GMT+02:00 Vivian Guillen [email protected]:
anyone found out how to set a default value?
—
Reply to this email directly or view it on GitHub
https://github.com/brianreavis/selectize.js/issues/236#issuecomment-55742906
.
Pozdrawiam,
Sławomir Gajowniczek
Easiest way I've found is to grab the selectize instance, and use the setValue to set the selected option.
E.g., $('.selectize')[0].selectize.setValue('1')
(whereby 1, in this example, is the option value of the item I want selected)
@dule
The problem with this approach is that onChange event is fired.
I need to load the page, set some initial value inline on a and then watch for selection changes made by the user.
Something like this:
<select data-value="10" ></select>.
@temuri416: According to @brianreavis, the newest release supports a data-data field:
https://github.com/brianreavis/selectize.js/issues/155#issuecomment-72153150
But the setValue also accepts a silent param that should suppress the change event: https://github.com/brianreavis/selectize.js/blob/master/docs/api.md#methods_other
The best way to set the default value is to set the selected attribute on the option(s) that you want selected. This of course means that the selected options need to exist in the DOM first, which isn't a bad idea regardless (i.e. when you submit a form before javascript loads, you don't want your form data to be invalid).
@brianreavis
But that is not possible when select options are provided using "options" parameter.
The following trick works for me, but it's kind of ass-backwards:
<select ...>
<option value="5" selected="selected"></option>
</select>
Select's options are populated from JSON.
@temuri416 That's not the question though, is it? For that, set items: ['5'] in your settings. I just updated the usage documentation to cover that.
Thanks... items is what I was looking for.
Most helpful comment
Easiest way I've found is to grab the selectize instance, and use the setValue to set the selected option.
E.g.,
$('.selectize')[0].selectize.setValue('1')(whereby
1, in this example, is the option value of the item I want selected)