Setting values
Values can change in a couple of different ways: through defaults, interacting with the UI or programmatically. The select defaults to null (nothing is selected), which can be easily overridden with the defaultSelection option. The values are set by interacting with the component and making a selection or it can be done programmatically. Use the buttons option for showing/hiding set, cancel or add custom buttons.
Demo
The select has no selected value by default. You can however manually set a default value through the
defaultSelection option. This value will show as selected when opening the control, but it needs to be submitted to be accepted as a selected value.
html<mbsc-select
[data]="myData">
</mbsc-select>
tsmyData = [
{ text: 'Atlanta', value: 'atl' },
{ text: 'Berlin', value: 'ber' },
{ text: 'Boston', value: 'bos' },
{ text: 'Chicago', value: 'chi' },
{ text: 'London', value: 'lon' }
];
html<mbsc-select
[data]="myData"
[defaultSelection]="myDefault">
</mbsc-select>
tsmyData = [
{ text: 'Atlanta', value: 'atl' },
{ text: 'Berlin', value: 'ber' },
{ text: 'Boston', value: 'bos' },
{ text: 'Chicago', value: 'chi' },
{ text: 'London', value: 'lon' }
];
myDefault = 'ber';
While the selected value can be changed by interacting with the select, you can set the model value to change it programmatically.
htmlhtml<mbsc-button (click)="setBoston()">Boston</mbsc-button>
<mbsc-button (click)="setLondon()">London</mbsc-button>
<mbsc-select
[data]="myData"
[(ngModel)]="customVal">
</mbsc-select>
tssetBoston() {
this.customVal = 'bos';
}
setLondon() {
this.customVal = 'lon';
}
The button API provides shortcuts to value selection and confirmation. Add a custom button and/or remove the 'Set' button to activate auto-set, which automatically closes the dialog if a value is selected.
htmlhtml<mbsc-select
[data]="myData"
[buttons]="['cancel']">
</mbsc-select>
tsmyData = [
{ text: 'Atlanta', value: 'atl' },
{ text: 'Berlin', value: 'ber' },
{ text: 'Boston', value: 'bos' },
{ text: 'Chicago', value: 'chi' },
{ text: 'London', value: 'lon' }
];
import { CommonModule } from '@angular/common';
import { Component, ViewChild } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { MbscModule, setOptions } from '@mobiscroll/angular';
setOptions({
locale: ,
theme: ' ',
themeVariant: ' '
});
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
standalone: true,
imports: [CommonModule, FormsModule, MbscModule],
})
export class AppComponent {
@ViewChild('selectInst', { static: false })
selectInst: any;
myData = [
{ text: 'Atlanta', value: 'atl' },
{ text: 'Berlin', value: 'ber' },
{ text: 'Boston', value: 'bos' },
{ text: 'Chicago', value: 'chi' },
{ text: 'London', value: 'lon' },
];
myDefault = 'ber';
customVal = '';
customButtons = [
{
text: 'Custom',
handler: () => {
this.selectInst.setTempVal('chi');
},
},
'set',
'cancel',
];
setBoston(): void {
this.customVal = 'bos';
}
setLondon(): void {
this.customVal = 'lon';
}
}
<mbsc-page>
<div class="mbsc-form-group">
<div class="mbsc-form-group-title">Controlling the default value</div>
<mbsc-select [data]="myData" label="Default" inputStyle="outline" labelStyle="stacked" placeholder="Please select..."></mbsc-select>
<mbsc-select
[data]="myData"
[defaultSelection]="myDefault"
label="Custom default"
inputStyle="outline"
labelStyle="stacked"
placeholder="Please select..."
></mbsc-select>
</div>
<div class="mbsc-form-group">
<div class="mbsc-form-group-title">Setting a custom value</div>
<div class="mbsc-button-group-block">
<mbsc-button (click)="setBoston()">Boston</mbsc-button>
<mbsc-button (click)="setLondon()">London</mbsc-button>
</div>
<mbsc-select [(ngModel)]="customVal" [data]="myData" display="inline"></mbsc-select>
</div>
<div class="mbsc-form-group">
<div class="mbsc-form-group-title">Buttons API</div>
<mbsc-select
[data]="myData"
#selectInst
[buttons]="customButtons"
label="Custom"
inputStyle="outline"
labelStyle="stacked"
placeholder="Please select..."
></mbsc-select>
<mbsc-select
[data]="myData"
[buttons]="['cancel']"
label="Auto set"
inputStyle="outline"
labelStyle="stacked"
placeholder="Please select..."
></mbsc-select>
</div>
</mbsc-page>