UiBibz::Ui::Core::Forms::Selects::DropdownSelectField
This component is based on the library Bootstrap select.
# by variable
ui_dropdown_select_field name, options, html_options
# or by block
ui_dropdown_select_field options, html_options do
name
end
options_tags = options_for_select(5.times.map{ |i| ["option #{i}", i] })
ui_dropdown_select_field "example1", option_tags: option_tags, prompt: "Select an option", include_blank: true
<div class="btn-group bootstrap-select">
<button type="button" class="btn dropdown-toggle btn-secondary" data-toggle="dropdown" data-id="example1" title="option 0">
<span class="filter-option pull-left">option 0</span>
<span class="bs-caret">
<span class="caret"></span>
</span>
</button>
<div class="dropdown-menu open">
<ul class="dropdown-menu inner" role="menu">
<li data-original-index="0" class="selected">
<a tabindex="0" class="" style="" data-tokens="null">
<span class="text">Select an option</span>
<span class="fa fa-check check-mark"></span>
</a>
</li>
<li data-original-index="1">
<a tabindex="0" class="" style="" data-tokens="null">
<span class="text"></span>
<span class="fa fa-check check-mark"></span>
</a>
</li>
<li data-original-index="2" class="selected">
<a tabindex="0" class="" style="" data-tokens="null">
<span class="text">option 0</span>
<span class="fa fa-check check-mark"></span>
</a>
</li>
<li data-original-index="3">
<a tabindex="0" class="" style="" data-tokens="null">
<span class="text">option 1</span>
<span class="fa fa-check check-mark"></span>
</a>
</li>
<li data-original-index="4">
<a tabindex="0" class="" style="" data-tokens="null">
<span class="text">option 2</span>
<span class="fa fa-check check-mark"></span>
</a>
</li>
<li data-original-index="5">
<a tabindex="0" class="" style="" data-tokens="null">
<span class="text">option 3</span>
<span class="fa fa-check check-mark"></span>
</a>
</li>
<li data-original-index="6">
<a tabindex="0" class="" style="" data-tokens="null">
<span class="text">option 4</span>
<span class="fa fa-check check-mark"></span>
</a>
</li>
<li data-original-index="7">
<a tabindex="0" class="" style="" data-tokens="null">
<span class="text">option 5</span>
<span class="fa fa-check check-mark"></span>
</a>
</li>
</ul>
</div>
<select name="example1" id="example1" class="selectpicker" tabindex="-98">
<option value>Select an option</option>
<option value label=""> </option>
<option value="option 0">option 0</option>
<option value="option 1">option 1</option>
<option value="option 2">option 2</option>
<option value="option 3">option 3</option>
<option value="option 4">option 4</option>
<option value="option 5">option 5</option>
</select>
</div>
grouped_options = { "North America" => [["United states","US"], "Canada"], "Europe" => ["Denmark","Germany","France"] }
ui_dropdown_select_field "example 1", option_tags: grouped_options_for_select(grouped_options)
<select name="example 1" id="example_1" class="select-field form-control">
<optgroup label="North America">
<option value="US">United states</option>
<option value="Canada">Canada</option></optgroup>
<optgroup label="Europe">
<option value="Denmark">Denmark</option>
<option value="Germany">Germany</option>
<option value="France">France</option>
</optgroup>
</select>
options_tags = options_for_select(5.times.map{ |i| ["option #{i}", i] })
ui_dropdown_select_field "example1", searchable: true, multiple: true, option_tags: option_tags
option_tags = options_for_select(5.times.map{ |i| ["option #{i}", i] })
ui_dropdown_select_field "example1", max_options: 3, multiple: true, option_tags: option_tags
values:
A comma delimited list of selected values (default)
count:
If one item is selected, then the option value is shown. If more than one is selected then the number of selected items is displayed, e.g. 2 of 6 selected
count > x:
Where x is the number of items selected when the display format changes from values to count
static:
Always show the select title (placeholder), regardless of selection
option_tags = options_for_select(5.times.map{ |i| ["option #{i}", i] })
ui_dropdown_select_field 'example1', multiple: true, selected_text_format: "count", option_tags: option_tags
option_tags = options_for_select(5.times.map{ |i| ["option #{i}", i] })
ui_dropdown_select_field 'example1', option_tags: select_options_tags, append: ui_glyph('pencil-alt'), prepend: ui_glyph('gem')
You can connect several components together using option connect.
With this option, you can determine the target, the trigger event, and the connection mode.
Output data, whether local or remote, must be written in this way:
[{ value: Integer||String, text: String||Integer, connect_option_id: Integer||String }, {...}] .
To have optgroup, format must be like that: { label1: [{ text: String||Integer, value: String||Integer, connect_option_id: Integer||String }, {...}], label2: [...]}
connect: {
event: String, # change|click|change click|... <default: 'change'>
mode: String, # local|remote <default: 'remote'>
target: {
selector: String, # .my-target|#my-target
data: Array, # [{ text: 'option', value: 1, connect_option_id: 2}]
url: String, # url_data.html'
}
}
You can connect an input to any other input:
| Select field | Dropdown select field | Multi column field | Multi select field | |
|---|---|---|---|---|
| Select field | ||||
| Dropdown select field | ||||
| Multi column field | ||||
| Multi select field |
options_tags = options_for_select(5.times.map{ |i| ["option #{i}", i] })
# First Dropdown Field
connect = { mode: "remote", target: { selector: "#example_1_target", url: components_forms_selects_select_field_path }}
ui_dropdown_select_field "example 1", { option_tags: select_options_tags, connect: connect }
# Second Dropdown Field
ui_dropdown_select_field "example 1 target", option_tags: select_options_tags
option_tags = options_for_select(5.times.map{ |i| ["option #{i}", i] })
# First Select input
options_data = 5.times.map{ |o| 5.times.map{ |i| { text: "New option #{ o }.#{ i }", value: i, connect_option_id: o }}}.flatten
connect = { mode: "local", target: { selector: "#example_2_target", data: options_data }}}
ui_dropdown_select_field "example 2", { option_tags: options_html, connect: connect }
# Second Select input
options_html = options_for_select(5.times.map{ |i| "option #{i}" })
ui_dropdown_select_field "example 2 target", option_tags: options_html
<select name='example 1' id='example_1' data-connect="{'type':'remote','target':{'selector':'#example_1_target','url':'/components/forms/selects/select-field'}}" class="ui-bibz-connect select-field form-control">
<option value="option 0">option 0</option>
<option value="option 1">option 1</option>
<option value="option 2">option 2</option>
<option value="option 3">option 3</option>
<option value="option 4">option 4</option></select>
<select name="example 1 target" id="example_1_target" class="select-field form-control">
<option value="option 0">option 0</option>
<option value="option 1">option 1</option>
<option value="option 2">option 2</option>
<option value="option 3">option 3</option>
<option value="option 4">option 4</option>
</select>
Option refresh take same arguments than connect option.
option_tags = options_for_select(5.times.map{ |i| ["option #{i}", i] })
refresh = { status: :primary, target: { url: components_forms_selects_select_field_path }}
ui_dropdown_select_field "example 3", option_tags: option_tags, refresh: refresh
<div class="input-group select-field-refresh">
<div class="btn-group bootstrap-select input-group-btn">
<button type="button" class="btn dropdown-toggle btn-secondary" data-toggle="dropdown" data-id="example_3_target" title="New option 2" aria-expanded="false">
<span class="filter-option pull-left">New option 2</span> <span class="bs-caret">
<span class="caret">
</span>
</span>
</button>
<div class="dropdown-menu open" style="max-height: 6169px; overflow: hidden; min-height: 0px;">
<ul class="dropdown-menu inner" role="menu" style="max-height: 6151px; overflow-y: auto; min-height: 0px;">
<li data-original-index="0" class="">
<a tabindex="0" class="" style="" data-tokens="null">
<span class="text">New option 1</span>
<span class="fa fa-check check-mark"></span>
</a>
</li>
<li data-original-index="1" class="selected">
<a tabindex="0" class="" style="" data-tokens="null">
<span class="text">New option 2</span>
<span class="fa fa-check check-mark"></span>
</a>
</li>
</ul>
</div>
<select name="example 3 target" id="example_3_target" class="selectpicker" tabindex="-98">
<option value="1">New option 1</option>
<option value="2">New option 2</option>
</select>
</div>
<span class="input-group-btn">
<button data-connect="{"events":"click","mode":"remote","target":{"selector":"#example_3_target","url":"/components/forms/selects/select-field","data":[]}}" class="btn-primary ui-bibz-connect input-refresh-button btn">
<i class="glyph fa fa-refresh"></i>
</button>
</span>
</div>
Ui Bibz is compatible with simple form.
You can use defaults inputs of Simple Form and defaults inputs of Ui Bibz like ui_dropdown_select_field. You can use Simple Form input options and Ui Bibz ui_dropdown_select_field options.
Then in your view, you can insert your input dropdown select field.
This simple form component is based on simple_form
collection
Collection inputs accept two other options beside collections:
ui_form_for(@user) do |f|
...
f.association :country, as: :ui_dropdown_select_field, collection: @countries, label_method: :name, value_method: :id
...
end
You can use `grouped: true` option to group collection
Grouped collection inputs accept the same :label_method and :value_method options, which will be used to retrieve label/value attributes for the option tags. Besides that, you can give:
ui_form_for(@search) do |f|
...
f.input :country, as: :ui_dropdown_select_field, grouped: true, collection: @collection, group_method: :last, label_method: :name, value_method: :value
...
end
You can add option refresh in your association.
ui_form_for(@user) do |f|
refresh = { status: :danger, target: { selector: 'form:last select[name="user[name]"]', url: components_forms_selects_select_field_path }}
connect = { target: { selector: '#company', url: components_forms_selects_select_field_path }}
...
f.association :country, as: :ui_dropdown_select_field, refresh: refresh, connect: connect, collection: @countries, label_method: :name, value_method: :id
...
end