Select Component API
The form_select component is Vue.js driven child component of the matestack_form component and is used to collect user input.
matestack_form my_form_config do
form_select key: :status, options: { 'active': 1, 'deactive': 0 }, #...
endParameters
key - required
Defines the key which should be used when posting the form data to the server.
options - required
Can either be an Array or Hash:
Array usage
matestack_form my_form_config do
form_select key: :status, options: [0, 1]
endwill render:
<select>
<option value="0">0</option>
<option value="1">1</option>
</select>Hash usage
will render:
The hash values will be used as values for the options, the keys as displayed values.
ActiveRecord Enum Mapping
If you want to use ActiveRecord enums as options for your select input, you can use the enum class method:
disabled_values
Defines which options (by value) should be disabled. Pass in as Array.
or
multiple
If set to true, a native HTML multiple select will be rendered. Selected values will be posted as an Array to the server.
init
Defines the init value of the select input. If mapped to an ActiveRecord model, the init value will be derived automatically from the model instance.
when multiple is set to true
Pass in an Array of init values:
when multiple is set to false/not specified
Pass in an Integer:
placeholder
Defines the placeholder which will be rendered as first, disabled option.
label
Defines the label which will be rendered right before the select tag. You can also use the label component in order to create more complex label structures.
Custom Select
If you want to create your own select component, that's easily done since v.1.3.0. Imagine, you want to use select2.js and therefore need to adjust the select rendering and need to initialize the third party library:
Create your own Ruby component:
app/matestack/components/my_form_select.rb
Create the corresponding Vue.js component:
Generic code:
In order to support the select2.js library, you would do something like this:
app/matestack/componenst/my_form_select.js
Don't forget to require and register the custom component JavaScript according to your JS setup!
Finally, use it within a
matestack_form:
Last updated