Value Selection
The Select component can be used to select one or multiple values from a list. The values can be of any type, for example number, string or object as described in the data binding section.
Default value and default selection
The value and the default selection of the Select are two different things:
- Value - the actual value of the component. It is displayed in the input, returned as the selected value and passed to the
onChangeevent. Set an initial value with[(ngModel)](or a reactive form control). - Default selection - the
defaultSelectionoption. It is only a pre-selection inside the opened picker, shown while the component has no value. It is not set as the value: the input stays empty and nothing is returned until the user confirms the selection, for example with the Set button. Without it, the desktop UI pre-selects nothing, while the touch UI (seetouchUi) pre-selects the first item in the selection highlight line.
<!-- Initial value: shown in the input, returned as the value -->
<mbsc-select [data]="drinks" [(ngModel)]="selected"></mbsc-select>
<!-- Default selection: only pre-selected in the opened picker, the input stays empty -->
<mbsc-select [data]="drinks" defaultSelection="margarita"></mbsc-select>
import { MbscSelectData } from '@mobiscroll/angular';
@Component({...})
export class MyComponent {
drinks: MbscSelectData[] = [
{ text: 'Mohito', value: 'mohito'},
{ text: 'Long Island', value: 'longisland'},
{ text: 'Margarita', value: 'margarita'}
];
selected: string = 'margarita';
}
Single value selection
By default the Select operates in single value selection mode, but can be used to select multiple values from a list as well.
<mbsc-select [data]="drinks" [(ngModel)]="selected"></mbsc-select>
import { MbscSelectData } from '@mobiscroll/angular';
@Component({...})
export class MyComponent {
drinks: MbscSelectData[] = [
{ text: 'Mohito', value: 'mohito'},
{ text: 'Long Island', value: 'longisland'},
{ text: 'Margarita', value: 'margarita'}
];
selected: string | null = null;
}
Multiple value selection
Multiselection can be enabled using the selectMultiple option.
<mbsc-select [data]="bonuses" [selectMultiple]="true" [(ngModel)]="selected"></mbsc-select>
import { MbscSelectData } from '@mobiscroll/angular';
@Component({...})
export class MyComponent {
bonuses: MbscSelectData[] = ['Free ticket', 'Free Drink', 'Free Hug'];
selected: string[] = [];
}