Data binding
The Select component can be initialized on an input or a select html element.
-
Using a select element can be beneficial when having only a small amount of options the user can choose from. Also when initializing on a select element, the displayed text and value are limited to strings.
-
Using an input element is easier when the data must be loaded dynamically or if you have large data sets. The selection list can hold any data, not just strings.
Using a select
<select id="mySelect">
<option value="1">Marketing</option>
<option value="2">Sales</option>
<option value="3">Support</option>
<option value="4">Development</option>
</select>
mobiscroll.select('#mySelect', {
// additional options here
});
Using an input
The Mobiscroll Select component has a data option, that is used to define the selection options.
The data option receives an array of strings or an array of objects containing a text, a value and optionally a group property.
The text must be a string, which will show up on the wheels. The value can be any kind of object (string, number, object, etc.), that will be the selected value when selecting it on the select.
The group property must be a string, that is used to group together a number of options. The options that have the same string specified by the group property will be grouped together. When groups are specified in the data, a header will be shown at the top of each group with the text specified in the group property. Additionally, when using the showGroupWheel option, an additional wheel will be shown, with the groups to help navigation. More on that in the Grouping section below.
When an array of string is passed to the data option, it is treated as both the text and the value would be the particular array item.
const strArray = ['USA', 'Canada', 'Mexico'];
// is the same as:
const objArray = [{ text: 'USA', value: 'USA'}, { text: 'Canada', value: 'Canada'}, { text: 'Mexico', value: 'Mexico' }];
In the above example the two arrays will show the same 3 countries to select from, and the selected value will also be the country's name as string.
Example with numbers as values
<input id="myInput" />
mobiscroll.select('#myInput', {
data: [
{ text: 'Marketing', value: 1 },
{ text: 'Sales', value: 2 },
{ text: 'Support', value: 3 },
{ text: 'Development', value: 4 },
],
});
Example with complex objects as values
<input id="myInput" />
mobiscroll.select('#myInput', {
data: [
{ text: 'Alice', value: { id: 123, fullName: 'Alice Cooper' }},
{ text: 'Brandon', value: { id: 456, fullName: 'Brandon Lee' }},
{ text: 'Louisa', value: { id: 789, fullName: 'Louisa Crawford'}},
],
});
Group options
Through the data option, with each item can be passed an optional group property. This group property is a string, that defines the group of the item. When the group is passed to any of the items, the whole data is treated as grouped and a header will be rendered above each group on the Select wheels. The header will contain the group name as specified in the group property.
const myCountries = [{
text: 'United Kingdom',
value: 'GB',
}, {
text: 'France',
value: 'FR',
group: 'Europe'
}, {
text: 'Hungary',
value: 'HU',
group: 'Europe'
}, {
text: 'United States of America',
value: 'US',
group: 'America'
}];
Furthermore an additional "group wheel" can be also rendered with the showGroupWheel option set to true. The group wheel items will be the group property values or the optgroup label attributes, and will help in navigating the option wheel.
<input id="myInput" />
mobiscroll.select('#myInput', {
showGroupWheel: true,
data: [{
text: 'France',
value: 'FR',
group: 'Europe'
}, {
text: 'Hungary',
value: 'HU',
group: 'Europe'
} // ...
],
});
Data through markup
The Mobiscroll Select component can be initialized on a <select> element, in which case it does not require the data option to be passed. The data will be read from the <option> elements.
<label>Gender
<select id="gender">
<option value="female">Female</option>
<option value="male">Male</option>
</select>
</label>
mobiscroll.select('#gender');
<label>Country
<select id="countries">
<optgroup label="Europe">
<option value="fr">France</option>
<option value="hu">Hungary</option>
</optgroup>
<optgroup label="Asia">
<option value="ch">China</option>
<option value="ja">Japan</option>
</optgroup>
</select>
</label>
mobiscroll.select('#countries');