ion-select
Selects là các điều khiển biểu mẫu để chọn một tùy chọn, hoặc các tùy chọn, từ một tập hợp các tùy chọn, tương tự như một <select> phần tử gốc . Khi người dùng nhấn vào lựa chọn, một hộp thoại xuất hiện với tất cả các tùy chọn trong một danh sách lớn, dễ chọn.
Selects nên được sử dụng với <ion-select-option> các phần tử con . Nếu tùy chọn con không được cung cấp một value thuộc tính thì văn bản của nó sẽ được sử dụng làm giá trị.
Nếu value được đặt trên <ion-select>, tùy chọn Selects sẽ được chọn dựa trên giá trị đó.
interfaces
Theo mặc định, chọn mục đích sử dụng ion-alert để mở lớp phủ các tùy chọn trong một cảnh báo. Có thể thay đổi giao diện để sử dụng ion-action-sheet hoặc là ion-popover bằng cách chuyển action-sheet hoặc popover, tương ứng, đến thuộc tính interface. Đọc tiếp các phần khác để biết những hạn chế của các giao diện khác nhau.
Single Selection
Theo mặc định, lựa chọn cho phép người dùng chỉ chọn một tùy chọn. Giao diện cảnh báo hiển thị cho người dùng một danh sách tùy chọn theo kiểu nút radio. Giao diện trang hành động chỉ có thể được sử dụng với một lựa chọn giá trị duy nhất. Giá trị của thành phần được chọn nhận giá trị của giá trị của tùy chọn đã chọn.
Multiple Selection
Bằng cách thêm thuộc tính multiple để chọn, người dùng có thể chọn nhiều tùy chọn. Khi có thể chọn nhiều tùy chọn, lớp cảnh báo sẽ hiển thị cho người dùng một danh sách tùy chọn theo kiểu hộp kiểm. Giá trị của thành phần được chọn nhận một mảng gồm tất cả các giá trị tùy chọn đã chọn.
Lưu ý: giao diện action-sheetvà popoversẽ không hoạt động với nhiều lựa chọn.
Object Value References
Khi sử dụng các đối tượng cho các giá trị đã chọn, danh tính của các đối tượng này có thể thay đổi nếu chúng đến từ máy chủ hoặc cơ sở dữ liệu, trong khi danh tính của giá trị đã chọn vẫn giữ nguyên. Ví dụ, điều này có thể xảy ra khi một bản ghi hiện có với giá trị đối tượng mong muốn được tải vào vùng chọn, nhưng các tùy chọn chọn mới được truy xuất hiện có các danh tính khác nhau. Điều này sẽ dẫn đến việc lựa chọn dường như không có giá trị nào, mặc dù lựa chọn ban đầu vẫn còn nguyên vẹn.
Theo mặc định, lựa chọn sử dụng đẳng thức đối tượng ( ===) để xác định xem một tùy chọn có được chọn hay không. Điều này có thể được ghi đè bằng cách cung cấp tên thuộc tính hoặc một chức năng cho thuộc compareWithtính.
Select Buttons
Cảnh báo hỗ trợ hai nút: Cancel và OK. Văn bản của mỗi nút có thể được tùy chỉnh bằng cách sử dụng các thuộc tính cancelText và okText.
Các action-sheet và popover giao diện không có một nút OK nhấp vào bất kỳ tùy chọn sẽ tự động đóng các overlay và chọn giá trị đó. Các popover giao diện không có một nút Cancel nhấp vào bất kỳ sẽ đóng lớp phủ.
interface Option
Vì select sử dụng giao diện cảnh báo, trang hành động và cửa sổ bật lên, các tùy chọn có thể được chuyển cho các thành phần này thông qua thuộc interfaceOptionstính. Điều này có thể được sử dụng để chuyển một tiêu đề tùy chỉnh, tiêu đề phụ, lớp css và hơn thế nữa.
Lưu ý: interfaceOptions sẽ không ghi đè inputs hoặc buttons với alert giao diện.
Customization
Có hai đơn vị tạo nên thành phần Chọn và mỗi đơn vị cần được tạo kiểu riêng. Phần ion-selecttử được thể hiện trên chế độ xem bằng (các) giá trị đã chọn hoặc trình giữ chỗ nếu không có và biểu tượng thả xuống. Giao diện, được xác định trong interface ở trên, là hộp thoại mở ra khi nhấp vào ion-select. Giao diện chứa tất cả các tùy chọn được xác định bằng cách thêm ion-select-optioncác phần tử. Các phần sau sẽ xem xét sự khác biệt giữa các kiểu tạo kiểu này.
Styling select Element
Như đã đề cập, ion-select phần tử chỉ bao gồm (các) giá trị hoặc trình giữ chỗ và biểu tượng được hiển thị trên dạng xem. Để tùy chỉnh điều này, hãy tạo kiểu bằng cách sử dụng kết hợp CSS và thuộc tính CSS
Ngoài ra, tùy thuộc vào sự hỗ trợ cần thiết của trình duyệt , các phần bóng CSS có thể được sử dụng để tạo kiểu cho lựa chọn:
Lưu ý rằng bằng cách sử dụng ::part, bất kỳ thuộc tính CSS nào trên phần tử đều có thể được nhắm mục tiêu.
Usage
Single Selection
<ion-list>
<ion-list-header>
<ion-label>
Single Selection
</ion-label>
</ion-list-header>
<ion-item>
<ion-label>Gender</ion-label>
<ion-select placeholder="Select One">
<ion-select-option value="f">Female</ion-select-option>
<ion-select-option value="m">Male</ion-select-option>
</ion-select>
</ion-item>
<ion-item>
<ion-label>Hair Color</ion-label>
<ion-select value="brown" okText="Okay" cancelText="Dismiss">
<ion-select-option value="brown">Brown</ion-select-option>
<ion-select-option value="blonde">Blonde</ion-select-option>
<ion-select-option value="black">Black</ion-select-option>
<ion-select-option value="red">Red</ion-select-option>
</ion-select>
</ion-item>
</ion-list>Multiple Selection
<ion-list>
<ion-list-header>
<ion-label>
Multiple Selection
</ion-label>
</ion-list-header>
<ion-item>
<ion-label>Toppings</ion-label>
<ion-select multiple="true" cancelText="Nah" okText="Okay!">
<ion-select-option value="bacon">Bacon</ion-select-option>
<ion-select-option value="olives">Black Olives</ion-select-option>
<ion-select-option value="xcheese">Extra Cheese</ion-select-option>
<ion-select-option value="peppers">Green Peppers</ion-select-option>
<ion-select-option value="mushrooms">Mushrooms</ion-select-option>
<ion-select-option value="onions">Onions</ion-select-option>
<ion-select-option value="pepperoni">Pepperoni</ion-select-option>
<ion-select-option value="pineapple">Pineapple</ion-select-option>
<ion-select-option value="sausage">Sausage</ion-select-option>
<ion-select-option value="Spinach">Spinach</ion-select-option>
</ion-select>
</ion-item>
<ion-item>
<ion-label>Pets</ion-label>
<ion-select multiple="true" [value]="['bird', 'dog']">
<ion-select-option value="bird">Bird</ion-select-option>
<ion-select-option value="cat">Cat</ion-select-option>
<ion-select-option value="dog">Dog</ion-select-option>
<ion-select-option value="honeybadger">Honey Badger</ion-select-option>
</ion-select>
</ion-item>
</ion-list>Objects as Values
<ion-list>
<ion-list-header>
<ion-label>
Objects as Values (compareWith)
</ion-label>
</ion-list-header>
<ion-item>
<ion-label>Users</ion-label>
<ion-select [compareWith]="compareWith">
<ion-select-option *ngFor="let user of users" [value]="user">{{user.first + ' ' + user.last}}</ion-select-option>
</ion-select>
</ion-item>
</ion-list>import { Component } from '@angular/core';
interface User {
id: number;
first: string;
last: string;
}
@Component({
selector: 'select-example',
templateUrl: 'select-example.html',
styleUrls: ['./select-example.css'],
})
export class SelectExample {
users: User[] = [
{
id: 1,
first: 'Alice',
last: 'Smith',
},
{
id: 2,
first: 'Bob',
last: 'Davis',
},
{
id: 3,
first: 'Charlie',
last: 'Rosenburg',
}
];
compareWith(o1: User, o2: User) {
return o1 && o2 ? o1.id === o2.id : o1 === o2;
}
}Objects as Values with Multiple Selection
<ion-list>
<ion-list-header>
<ion-label>
Objects as Values (compareWith)
</ion-label>
</ion-list-header>
<ion-item>
<ion-label>Users</ion-label>
<ion-select [compareWith]="compareWith" multiple="true">
<ion-select-option *ngFor="let user of users" [value]="user">{{user.first + ' ' + user.last}}</ion-select-option>
</ion-select>
</ion-item>
</ion-list>import { Component } from '@angular/core';
interface User {
id: number;
first: string;
last: string;
}
@Component({
selector: 'select-example',
templateUrl: 'select-example.html',
styleUrls: ['./select-example.css'],
})
export class SelectExample {
users: User[] = [
{
id: 1,
first: 'Alice',
last: 'Smith',
},
{
id: 2,
first: 'Bob',
last: 'Davis',
},
{
id: 3,
first: 'Charlie',
last: 'Rosenburg',
}
];
compareWith(o1: User, o2: User | User[]) {
if (!o1 || !o2) {
return o1 === o2;
}
if (Array.isArray(o2)) {
return o2.some((u: User) => u.id === o1.id);
}
return o1.id === o2.id;
}
}Interface Options
<ion-list>
<ion-list-header>
<ion-label>
Interface Options
</ion-label>
</ion-list-header>
<ion-item>
<ion-label>Alert</ion-label>
<ion-select [interfaceOptions]="customAlertOptions" interface="alert" multiple="true" placeholder="Select One">
<ion-select-option value="bacon">Bacon</ion-select-option>
<ion-select-option value="olives">Black Olives</ion-select-option>
<ion-select-option value="xcheese">Extra Cheese</ion-select-option>
<ion-select-option value="peppers">Green Peppers</ion-select-option>
<ion-select-option value="mushrooms">Mushrooms</ion-select-option>
<ion-select-option value="onions">Onions</ion-select-option>
<ion-select-option value="pepperoni">Pepperoni</ion-select-option>
<ion-select-option value="pineapple">Pineapple</ion-select-option>
<ion-select-option value="sausage">Sausage</ion-select-option>
<ion-select-option value="Spinach">Spinach</ion-select-option>
</ion-select>
</ion-item>
<ion-item>
<ion-label>Popover</ion-label>
<ion-select [interfaceOptions]="customPopoverOptions" interface="popover" placeholder="Select One">
<ion-select-option value="brown">Brown</ion-select-option>
<ion-select-option value="blonde">Blonde</ion-select-option>
<ion-select-option value="black">Black</ion-select-option>
<ion-select-option value="red">Red</ion-select-option>
</ion-select>
</ion-item>
<ion-item>
<ion-label>Action Sheet</ion-label>
<ion-select [interfaceOptions]="customActionSheetOptions" interface="action-sheet" placeholder="Select One">
<ion-select-option value="red">Red</ion-select-option>
<ion-select-option value="purple">Purple</ion-select-option>
<ion-select-option value="yellow">Yellow</ion-select-option>
<ion-select-option value="orange">Orange</ion-select-option>
<ion-select-option value="green">Green</ion-select-option>
</ion-select>
</ion-item>
</ion-list>import { Component } from '@angular/core';
@Component({
selector: 'select-example',
templateUrl: 'select-example.html',
styleUrls: ['./select-example.css'],
})
export class SelectExample {
customAlertOptions: any = {
header: 'Pizza Toppings',
subHeader: 'Select your toppings',
message: '$1.00 per topping',
translucent: true
};
customPopoverOptions: any = {
header: 'Hair Color',
subHeader: 'Select your hair color',
message: 'Only select your dominant hair color'
};
customActionSheetOptions: any = {
header: 'Colors',
subHeader: 'Select your favorite color'
};
}Single Selection
<ion-list>
<ion-list-header>
<ion-label>
Single Selection
</ion-label>
</ion-list-header>
<ion-item>
<ion-label>Gender</ion-label>
<ion-select placeholder="Select One">
<ion-select-option value="f">Female</ion-select-option>
<ion-select-option value="m">Male</ion-select-option>
</ion-select>
</ion-item>
<ion-item>
<ion-label>Hair Color</ion-label>
<ion-select value="brown" ok-text="Okay" cancel-text="Dismiss">
<ion-select-option value="brown">Brown</ion-select-option>
<ion-select-option value="blonde">Blonde</ion-select-option>
<ion-select-option value="black">Black</ion-select-option>
<ion-select-option value="red">Red</ion-select-option>
</ion-select>
</ion-item>
</ion-list>Multiple Selection
<ion-list>
<ion-list-header>
<ion-label>
Multiple Selection
</ion-label>
</ion-list-header>
<ion-item>
<ion-label>Toppings</ion-label>
<ion-select multiple="true" cancel-text="Nah" ok-text="Okay!">
<ion-select-option value="bacon">Bacon</ion-select-option>
<ion-select-option value="olives">Black Olives</ion-select-option>
<ion-select-option value="xcheese">Extra Cheese</ion-select-option>
<ion-select-option value="peppers">Green Peppers</ion-select-option>
<ion-select-option value="mushrooms">Mushrooms</ion-select-option>
<ion-select-option value="onions">Onions</ion-select-option>
<ion-select-option value="pepperoni">Pepperoni</ion-select-option>
<ion-select-option value="pineapple">Pineapple</ion-select-option>
<ion-select-option value="sausage">Sausage</ion-select-option>
<ion-select-option value="Spinach">Spinach</ion-select-option>
</ion-select>
</ion-item>
<ion-item>
<ion-label>Pets</ion-label>
<ion-select id="multiple" multiple="true">
<ion-select-option value="bird">Bird</ion-select-option>
<ion-select-option value="cat">Cat</ion-select-option>
<ion-select-option value="dog">Dog</ion-select-option>
<ion-select-option value="honeybadger">Honey Badger</ion-select-option>
</ion-select>
</ion-item>
</ion-list>const select = document.querySelector('multiple');
select.value = ['bird', 'dog'];Objects as Values
<ion-list>
<ion-list-header>
<ion-label>
Objects as Values (compareWith)
</ion-label>
</ion-list-header>
<ion-item>
<ion-label>Users</ion-label>
<ion-select id="objectSelectCompareWith"></ion-select>
</ion-item>
</ion-list> let objectOptions = [
{
id: 1,
first: 'Alice',
last: 'Smith',
},
{
id: 2,
first: 'Bob',
last: 'Davis',
},
{
id: 3,
first: 'Charlie',
last: 'Rosenburg',
}
];
let compareWithFn = (o1, o2) => {
return o1 && o2 ? o1.id === o2.id : o1 === o2;
};
let objectSelectElement = document.getElementById('objectSelectCompareWith');
objectSelectElement.compareWith = compareWithFn;
objectOptions.forEach((option, i) => {
let selectOption = document.createElement('ion-select-option');
selectOption.value = option;
selectOption.textContent = option.first + ' ' + option.last;
objectSelectElement.appendChild(selectOption)
});
objectSelectElement.value = objectOptions[0];
}Interface Options
<ion-list>
<ion-list-header>
<ion-label>
Interface Options
</ion-label>
</ion-list-header>
<ion-item>
<ion-label>Alert</ion-label>
<ion-select id="customAlertSelect" interface="alert" multiple="true" placeholder="Select One">
<ion-select-option value="bacon">Bacon</ion-select-option>
<ion-select-option value="olives">Black Olives</ion-select-option>
<ion-select-option value="xcheese">Extra Cheese</ion-select-option>
<ion-select-option value="peppers">Green Peppers</ion-select-option>
<ion-select-option value="mushrooms">Mushrooms</ion-select-option>
<ion-select-option value="onions">Onions</ion-select-option>
<ion-select-option value="pepperoni">Pepperoni</ion-select-option>
<ion-select-option value="pineapple">Pineapple</ion-select-option>
<ion-select-option value="sausage">Sausage</ion-select-option>
<ion-select-option value="Spinach">Spinach</ion-select-option>
</ion-select>
</ion-item>
<ion-item>
<ion-label>Popover</ion-label>
<ion-select id="customPopoverSelect" interface="popover" placeholder="Select One">
<ion-select-option value="brown">Brown</ion-select-option>
<ion-select-option value="blonde">Blonde</ion-select-option>
<ion-select-option value="black">Black</ion-select-option>
<ion-select-option value="red">Red</ion-select-option>
</ion-select>
</ion-item>
<ion-item>
<ion-label>Action Sheet</ion-label>
<ion-select id="customActionSheetSelect" interface="action-sheet" placeholder="Select One">
<ion-select-option value="red">Red</ion-select-option>
<ion-select-option value="purple">Purple</ion-select-option>
<ion-select-option value="yellow">Yellow</ion-select-option>
<ion-select-option value="orange">Orange</ion-select-option>
<ion-select-option value="green">Green</ion-select-option>
</ion-select>
</ion-item>
</ion-list>var customAlertSelect = document.getElementById('customAlertSelect');
var customAlertOptions = {
header: 'Pizza Toppings',
subHeader: 'Select your toppings',
message: '$1.00 per topping',
translucent: true
};
customAlertSelect.interfaceOptions = customAlertOptions;
var customPopoverSelect = document.getElementById('customPopoverSelect');
var customPopoverOptions = {
header: 'Hair Color',
subHeader: 'Select your hair color',
message: 'Only select your dominant hair color'
};
customPopoverSelect.interfaceOptions = customPopoverOptions;
var customActionSheetSelect = document.getElementById('customActionSheetSelect');
var customActionSheetOptions = {
header: 'Colors',
subHeader: 'Select your favorite color'
};
customActionSheetSelect.interfaceOptions = customActionSheetOptions;Single Selection
import React, { useState } from 'react';
import { IonContent, IonItem, IonLabel, IonList, IonListHeader, IonSelect, IonSelectOption, IonPage, IonItemDivider } from '@ionic/react';
export const SingleSelection: React.FC = () => {
const [gender, setGender] = useState<string>();
const [hairColor, setHairColor] = useState<string>('brown');
return (
<IonPage>
<IonContent>
<IonList>
<IonListHeader>
<IonLabel>
Single Selection
</IonLabel>
</IonListHeader>
<IonItem>
<IonLabel>Gender</IonLabel>
<IonSelect value={gender} placeholder="Select One" onIonChange={e => setGender(e.detail.value)}>
<IonSelectOption value="female">Female</IonSelectOption>
<IonSelectOption value="male">Male</IonSelectOption>
</IonSelect>
</IonItem>
<IonItem>
<IonLabel>Hair Color</IonLabel>
<IonSelect value={hairColor} okText="Okay" cancelText="Dismiss" onIonChange={e => setHairColor(e.detail.value)}>
<IonSelectOption value="brown">Brown</IonSelectOption>
<IonSelectOption value="blonde">Blonde</IonSelectOption>
<IonSelectOption value="black">Black</IonSelectOption>
<IonSelectOption value="red">Red</IonSelectOption>
</IonSelect>
</IonItem>
<IonItemDivider>Your Selections</IonItemDivider>
<IonItem>Gender: {gender ?? '(none selected)'}</IonItem>
<IonItem>Hair Color: {hairColor}</IonItem>
</IonList>
</IonContent>
</IonPage>
);
};Multiple Selection
import React, { useState } from 'react';
import { IonContent, IonItem, IonLabel, IonList, IonListHeader, IonSelect, IonSelectOption, IonPage, IonItemDivider } from '@ionic/react';
export const MultipleSelection: React.FC = () => {
const [toppings, setToppings] = useState<string[]>([]);
const [pets, setPets] = useState<string[]>(['bird', 'dog']);
return (
<IonPage>
<IonContent>
<IonList>
<IonListHeader>
<IonLabel>
Multiple Selection
</IonLabel>
</IonListHeader>
<IonItem>
<IonLabel>Toppings</IonLabel>
<IonSelect value={toppings} multiple={true} cancelText="Nah" okText="Okay!" onIonChange={e => setToppings(e.detail.value)}>
<IonSelectOption value="bacon">Bacon</IonSelectOption>
<IonSelectOption value="olives">Black Olives</IonSelectOption>
<IonSelectOption value="xcheese">Extra Cheese</IonSelectOption>
<IonSelectOption value="peppers">Green Peppers</IonSelectOption>
<IonSelectOption value="mushrooms">Mushrooms</IonSelectOption>
<IonSelectOption value="onions">Onions</IonSelectOption>
<IonSelectOption value="pepperoni">Pepperoni</IonSelectOption>
<IonSelectOption value="pineapple">Pineapple</IonSelectOption>
<IonSelectOption value="sausage">Sausage</IonSelectOption>
<IonSelectOption value="Spinach">Spinach</IonSelectOption>
</IonSelect>
</IonItem>
<IonItem>
<IonLabel>Pets</IonLabel>
<IonSelect multiple={true} value={pets} onIonChange={e => setPets(e.detail.value)}>
<IonSelectOption value="bird">Bird</IonSelectOption>
<IonSelectOption value="cat">Cat</IonSelectOption>
<IonSelectOption value="dog">Dog</IonSelectOption>
<IonSelectOption value="honeybadger">Honey Badger</IonSelectOption>
</IonSelect>
</IonItem>
<IonItemDivider>Your Selections</IonItemDivider>
<IonItem>Toppings: {toppings.length ? toppings.reduce((curr, prev) => prev + ', ' + curr, '') : '(none selected)'}</IonItem>
<IonItem>Pets: {pets.length ? pets.reduce((curr, prev) => prev + ', ' + curr, '') : '(none selected)'}</IonItem>
</IonList>
</IonContent>
</IonPage>
);
};Objects as Values
import React, { useState } from 'react';
import { IonContent, IonItem, IonLabel, IonList, IonListHeader, IonSelect, IonSelectOption, IonPage, IonItemDivider } from '@ionic/react';
const users = [
{
id: 1,
first: 'Alice',
last: 'Smith'
},
{
id: 2,
first: 'Bob',
last: 'Davis'
},
{
id: 3,
first: 'Charlie',
last: 'Rosenburg',
}
];
type User = typeof users[number];
const compareWith = (o1: User, o2: User) => {
return o1 && o2 ? o1.id === o2.id : o1 === o2;
};
export const ObjectSelection: React.FC = () => {
const [selectedUsers, setSelectedUsers] = useState<User[]>([]);
return (
<IonPage>
<IonContent>
<IonList>
<IonListHeader>
<IonLabel>
Objects as Values (compareWith)
</IonLabel>
</IonListHeader>
<IonItem>
<IonLabel>Users</IonLabel>
<IonSelect compareWith={compareWith} value={selectedUsers} multiple onIonChange={e => setSelectedUsers(e.detail.value)}>
{users.map(user => (
<IonSelectOption key={user.id} value={user}>
{user.first} {user.last}
</IonSelectOption>
))}
</IonSelect>
</IonItem>
<IonItemDivider>Selected Users</IonItemDivider>
{selectedUsers.length ?
selectedUsers.map(user => <IonItem key={user.id}>{user.first} {user.last}</IonItem>) :
<IonItem>(none selected)</IonItem>
}
</IonList>
</IonContent>
</IonPage>
);
};Interface Options
import React, { useState } from 'react';
import { IonContent, IonItem, IonLabel, IonList, IonListHeader, IonSelect, IonSelectOption, IonPage, IonItemDivider } from '@ionic/react';
const customAlertOptions = {
header: 'Pizza Toppings',
subHeader: 'Select your toppings',
message: '$1.00 per topping',
translucent: true
};
const customPopoverOptions = {
header: 'Hair Color',
subHeader: 'Select your hair color',
message: 'Only select your dominant hair color'
};
const customActionSheetOptions = {
header: 'Colors',
subHeader: 'Select your favorite color'
};
export const InterfaceOptionsSelection: React.FC = () => {
const [toppings, setToppings] = useState<string[]>([]);
const [hairColor, setHairColor] = useState<string>('brown');
const [color, setColor] = useState<string>();
return (
<IonPage>
<IonContent>
<IonList>
<IonListHeader>
<IonLabel>
Interface Options
</IonLabel>
</IonListHeader>
<IonItem>
<IonLabel>Alert</IonLabel>
<IonSelect
interfaceOptions={customAlertOptions}
interface="alert"
multiple={true}
placeholder="Select One"
onIonChange={e => setToppings(e.detail.value)}
value={toppings}
>
<IonSelectOption value="bacon">Bacon</IonSelectOption>
<IonSelectOption value="olives">Black Olives</IonSelectOption>
<IonSelectOption value="xcheese">Extra Cheese</IonSelectOption>
<IonSelectOption value="peppers">Green Peppers</IonSelectOption>
<IonSelectOption value="mushrooms">Mushrooms</IonSelectOption>
<IonSelectOption value="onions">Onions</IonSelectOption>
<IonSelectOption value="pepperoni">Pepperoni</IonSelectOption>
<IonSelectOption value="pineapple">Pineapple</IonSelectOption>
<IonSelectOption value="sausage">Sausage</IonSelectOption>
<IonSelectOption value="Spinach">Spinach</IonSelectOption>
</IonSelect>
</IonItem>
<IonItem>
<IonLabel>Popover</IonLabel>
<IonSelect
interfaceOptions={customPopoverOptions}
interface="popover"
placeholder="Select One"
onIonChange={e => setHairColor(e.detail.value)}
value={hairColor}>
<IonSelectOption value="brown">Brown</IonSelectOption>
<IonSelectOption value="blonde">Blonde</IonSelectOption>
<IonSelectOption value="black">Black</IonSelectOption>
<IonSelectOption value="red">Red</IonSelectOption>
</IonSelect>
</IonItem>
<IonItem>
<IonLabel>Action Sheet</IonLabel>
<IonSelect
interfaceOptions={customActionSheetOptions}
interface="action-sheet"
placeholder="Select One"
onIonChange={e => setColor(e.detail.value)}
value={color}
>
<IonSelectOption value="red">Red</IonSelectOption>
<IonSelectOption value="purple">Purple</IonSelectOption>
<IonSelectOption value="yellow">Yellow</IonSelectOption>
<IonSelectOption value="orange">Orange</IonSelectOption>
<IonSelectOption value="green">Green</IonSelectOption>
</IonSelect>
</IonItem>
<IonItemDivider>Your Selections</IonItemDivider>
<IonItem>Toppings: {toppings.length ? toppings.reduce((curr, prev) => prev + ', ' + curr, '') : '(none selected)'}</IonItem>
<IonItem>Hair Color: {hairColor}</IonItem>
<IonItem>Color: {color ?? '(none selected)'}</IonItem>
</IonList>
</IonContent>
</IonPage>
);
};Single Selection
import { Component, h } from '@stencil/core';
@Component({
tag: 'select-example',
styleUrl: 'select-example.css'
})
export class SelectExample {
render() {
return [
<ion-list>
<ion-list-header>
<ion-label>
Single Selection
</ion-label>
</ion-list-header>
<ion-item>
<ion-label>Gender</ion-label>
<ion-select placeholder="Select One">
<ion-select-option value="f">Female</ion-select-option>
<ion-select-option value="m">Male</ion-select-option>
</ion-select>
</ion-item>
<ion-item>
<ion-label>Hair Color</ion-label>
<ion-select value="brown" okText="Okay" cancelText="Dismiss">
<ion-select-option value="brown">Brown</ion-select-option>
<ion-select-option value="blonde">Blonde</ion-select-option>
<ion-select-option value="black">Black</ion-select-option>
<ion-select-option value="red">Red</ion-select-option>
</ion-select>
</ion-item>
</ion-list>
];
}
}Multiple Selection
import { Component, h } from '@stencil/core';
@Component({
tag: 'select-example',
styleUrl: 'select-example.css'
})
export class SelectExample {
render() {
return [
<ion-list>
<ion-list-header>
<ion-label>
Multiple Selection
</ion-label>
</ion-list-header>
<ion-item>
<ion-label>Toppings</ion-label>
<ion-select multiple={true} cancelText="Nah" okText="Okay!">
<ion-select-option value="bacon">Bacon</ion-select-option>
<ion-select-option value="olives">Black Olives</ion-select-option>
<ion-select-option value="xcheese">Extra Cheese</ion-select-option>
<ion-select-option value="peppers">Green Peppers</ion-select-option>
<ion-select-option value="mushrooms">Mushrooms</ion-select-option>
<ion-select-option value="onions">Onions</ion-select-option>
<ion-select-option value="pepperoni">Pepperoni</ion-select-option>
<ion-select-option value="pineapple">Pineapple</ion-select-option>
<ion-select-option value="sausage">Sausage</ion-select-option>
<ion-select-option value="Spinach">Spinach</ion-select-option>
</ion-select>
</ion-item>
<ion-item>
<ion-label>Pets</ion-label>
<ion-select multiple={true} value={['bird', 'dog']}>
<ion-select-option value="bird">Bird</ion-select-option>
<ion-select-option value="cat">Cat</ion-select-option>
<ion-select-option value="dog">Dog</ion-select-option>
<ion-select-option value="honeybadger">Honey Badger</ion-select-option>
</ion-select>
</ion-item>
</ion-list>
];
}
}Objects as Values
import { Component, h } from '@stencil/core';
@Component({
tag: 'select-example',
styleUrl: 'select-example.css'
})
export class SelectExample {
private users: any[] = [
{
id: 1,
first: 'Alice',
last: 'Smith',
},
{
id: 2,
first: 'Bob',
last: 'Davis',
},
{
id: 3,
first: 'Charlie',
last: 'Rosenburg',
}
];
compareWith = (o1, o2) => {
return o1 && o2 ? o1.id === o2.id : o1 === o2;
};
render() {
return [
<ion-list>
<ion-list-header>
<ion-label>
Objects as Values (compareWith)
</ion-label>
</ion-list-header>
<ion-item>
<ion-label>Users</ion-label>
<ion-select compareWith={this.compareWith}>
{this.users.map(user =>
<ion-select-option value={user}>
{user.first + ' ' + user.last}
</ion-select-option>
)}
</ion-select>
</ion-item>
</ion-list>
];
}
}Interface Options
import { Component, h } from '@stencil/core';
@Component({
tag: 'select-example',
styleUrl: 'select-example.css'
})
export class SelectExample {
private customAlertOptions: any = {
header: 'Pizza Toppings',
subHeader: 'Select your toppings',
message: '$1.00 per topping',
translucent: true
};
private customPopoverOptions: any = {
header: 'Hair Color',
subHeader: 'Select your hair color',
message: 'Only select your dominant hair color'
};
private customActionSheetOptions: any = {
header: 'Colors',
subHeader: 'Select your favorite color'
};
render() {
return [
<ion-list>
<ion-list-header>
<ion-label>
Interface Options
</ion-label>
</ion-list-header>
<ion-item>
<ion-label>Alert</ion-label>
<ion-select interfaceOptions={this.customAlertOptions} interface="alert" multiple={true} placeholder="Select One">
<ion-select-option value="bacon">Bacon</ion-select-option>
<ion-select-option value="olives">Black Olives</ion-select-option>
<ion-select-option value="xcheese">Extra Cheese</ion-select-option>
<ion-select-option value="peppers">Green Peppers</ion-select-option>
<ion-select-option value="mushrooms">Mushrooms</ion-select-option>
<ion-select-option value="onions">Onions</ion-select-option>
<ion-select-option value="pepperoni">Pepperoni</ion-select-option>
<ion-select-option value="pineapple">Pineapple</ion-select-option>
<ion-select-option value="sausage">Sausage</ion-select-option>
<ion-select-option value="Spinach">Spinach</ion-select-option>
</ion-select>
</ion-item>
<ion-item>
<ion-label>Popover</ion-label>
<ion-select interfaceOptions={this.customPopoverOptions} interface="popover" placeholder="Select One">
<ion-select-option value="brown">Brown</ion-select-option>
<ion-select-option value="blonde">Blonde</ion-select-option>
<ion-select-option value="black">Black</ion-select-option>
<ion-select-option value="red">Red</ion-select-option>
</ion-select>
</ion-item>
<ion-item>
<ion-label>Action Sheet</ion-label>
<ion-select interfaceOptions={this.customActionSheetOptions} interface="action-sheet" placeholder="Select One">
<ion-select-option value="red">Red</ion-select-option>
<ion-select-option value="purple">Purple</ion-select-option>
<ion-select-option value="yellow">Yellow</ion-select-option>
<ion-select-option value="orange">Orange</ion-select-option>
<ion-select-option value="green">Green</ion-select-option>
</ion-select>
</ion-item>
</ion-list>
];
}
}Single Selection
<template>
<ion-list>
<ion-list-header>
<ion-label>
Single Selection
</ion-label>
</ion-list-header>
<ion-item>
<ion-label>Gender</ion-label>
<ion-select placeholder="Select One">
<ion-select-option value="f">Female</ion-select-option>
<ion-select-option value="m">Male</ion-select-option>
</ion-select>
</ion-item>
<ion-item>
<ion-label>Hair Color</ion-label>
<ion-select value="brown" ok-text="Okay" cancel-text="Dismiss">
<ion-select-option value="brown">Brown</ion-select-option>
<ion-select-option value="blonde">Blonde</ion-select-option>
<ion-select-option value="black">Black</ion-select-option>
<ion-select-option value="red">Red</ion-select-option>
</ion-select>
</ion-item>
</ion-list>
</template>
<script>
import {
IonItem,
IonLabel,
IonList,
IonListHeader,
IonSelect,
IonSelectOption
} from '@ionic/vue';
import { defineComponent } from 'vue';
export default defineComponent({
components: {
IonItem,
IonLabel,
IonList,
IonListHeader,
IonSelect,
IonSelectOption
}
});
</script>Multiple Selection
<template>
<ion-list>
<ion-list-header>
<ion-label>
Multiple Selection
</ion-label>
</ion-list-header>
<ion-item>
<ion-label>Toppings</ion-label>
<ion-select multiple="true" cancel-text="Nah" ok-text="Okay!">
<ion-select-option value="bacon">Bacon</ion-select-option>
<ion-select-option value="olives">Black Olives</ion-select-option>
<ion-select-option value="xcheese">Extra Cheese</ion-select-option>
<ion-select-option value="peppers">Green Peppers</ion-select-option>
<ion-select-option value="mushrooms">Mushrooms</ion-select-option>
<ion-select-option value="onions">Onions</ion-select-option>
<ion-select-option value="pepperoni">Pepperoni</ion-select-option>
<ion-select-option value="pineapple">Pineapple</ion-select-option>
<ion-select-option value="sausage">Sausage</ion-select-option>
<ion-select-option value="Spinach">Spinach</ion-select-option>
</ion-select>
</ion-item>
<ion-item>
<ion-label>Pets</ion-label>
<ion-select multiple="true" :value=['bird', 'dog']>
<ion-select-option value="bird">Bird</ion-select-option>
<ion-select-option value="cat">Cat</ion-select-option>
<ion-select-option value="dog">Dog</ion-select-option>
<ion-select-option value="honeybadger">Honey Badger</ion-select-option>
</ion-select>
</ion-item>
</ion-list>
</template>
<script>
import {
IonItem,
IonLabel,
IonList,
IonListHeader,
IonSelect,
IonSelectOption
} from '@ionic/vue';
import { defineComponent } from 'vue';
export default defineComponent({
components: {
IonItem,
IonLabel,
IonList,
IonListHeader,
IonSelect,
IonSelectOption
}
});
</script>Interface Options
<template>
<ion-list>
<ion-list-header>
<ion-label>
Interface Options
</ion-label>
</ion-list-header>
<ion-item>
<ion-label>Alert</ion-label>
<ion-select :interface-options="customAlertOptions" interface="alert" multiple="true" placeholder="Select One">
<ion-select-option value="bacon">Bacon</ion-select-option>
<ion-select-option value="olives">Black Olives</ion-select-option>
<ion-select-option value="xcheese">Extra Cheese</ion-select-option>
<ion-select-option value="peppers">Green Peppers</ion-select-option>
<ion-select-option value="mushrooms">Mushrooms</ion-select-option>
<ion-select-option value="onions">Onions</ion-select-option>
<ion-select-option value="pepperoni">Pepperoni</ion-select-option>
<ion-select-option value="pineapple">Pineapple</ion-select-option>
<ion-select-option value="sausage">Sausage</ion-select-option>
<ion-select-option value="Spinach">Spinach</ion-select-option>
</ion-select>
</ion-item>
<ion-item>
<ion-label>Popover</ion-label>
<ion-select :interface-options="customPopoverOptions" interface="popover" placeholder="Select One">
<ion-select-option value="brown">Brown</ion-select-option>
<ion-select-option value="blonde">Blonde</ion-select-option>
<ion-select-option value="black">Black</ion-select-option>
<ion-select-option value="red">Red</ion-select-option>
</ion-select>
</ion-item>
<ion-item>
<ion-label>Action Sheet</ion-label>
<ion-select :interface-options="customActionSheetOptions" interface="action-sheet" placeholder="Select One">
<ion-select-option value="red">Red</ion-select-option>
<ion-select-option value="purple">Purple</ion-select-option>
<ion-select-option value="yellow">Yellow</ion-select-option>
<ion-select-option value="orange">Orange</ion-select-option>
<ion-select-option value="green">Green</ion-select-option>
</ion-select>
</ion-item>
</ion-list>
</template>
<script>
import {
IonItem,
IonLabel,
IonList,
IonListHeader,
IonSelect,
IonSelectOption
} from '@ionic/vue';
import { defineComponent } from 'vue';
export default defineComponent({
components: {
IonItem,
IonLabel,
IonList,
IonListHeader,
IonSelect,
IonSelectOption
},
setup() {
const customAlertOptions: any = {
header: 'Pizza Toppings',
subHeader: 'Select your toppings',
message: '$1.00 per topping',
translucent: true
};
const customPopoverOptions: any = {
header: 'Hair Color',
subHeader: 'Select your hair color',
message: 'Only select your dominant hair color'
};
const customActionSheetOptions: any = {
header: 'Colors',
subHeader: 'Select your favorite color'
};
return {
customAlertOptions,
customPopoverOptions,
customActionSheetOptions
}
}
});
</script>Properties
cancelText | |
|---|---|
| Description | The text to display on the cancel button. |
| Attribute | cancel-text |
| Type | string |
| Default | 'Cancel' |
compareWith | |
| Description | A property name or function used to compare object values |
| Attribute | compare-with |
| Type | ((currentValue: any, compareValue: any) => boolean) | null | string | undefined |
disabled | |
| Description | If |
| Attribute | disabled |
| Type | boolean |
| Default | false |
interface | |
| Description | The interface the select should use: |
| Attribute | interface |
| Type | "action-sheet" | "alert" | "popover" |
| Default | 'alert' |
interfaceOptions | |
| Description | Any additional options that the Note: |
| Attribute | interface-options |
| Type | any |
| Default | {} |
mode | |
| Description | The mode determines which platform styles to use. |
| Attribute | mode |
| Type | "ios" | "md" |
multiple | |
| Description | If |
| Attribute | multiple |
| Type | boolean |
| Default | false |
name | |
| Description | The name of the control, which is submitted with the form data. |
| Attribute | name |
| Type | string |
| Default | this.inputId |
okText | |
| Description | The text to display on the ok button. |
| Attribute | ok-text |
| Type | string |
| Default | 'OK' |
placeholder | |
| Description | The text to display when the select is empty. |
| Attribute | placeholder |
| Type | null | string | undefined |
selectedText | |
| Description | The text to display instead of the selected option's value. |
| Attribute | selected-text |
| Type | null | string | undefined |
value | |
| Description | the value of the select. |
| Attribute | value |
| Type | any |
Events
| Name | Description |
|---|---|
ionBlur | Emitted when the select loses focus. |
ionCancel | Emitted when the selection is cancelled. |
ionChange | Emitted when the value has changed. |
ionFocus | Emitted when the select has focus. |
Methods
open | |
|---|---|
| Description | Open the select overlay. The overlay is either an alert, action sheet, or popover, depending on the |
| Signature | open(event?: UIEvent | undefined) => Promise<any> |
CSS Shadow Parts
| Name | Description |
|---|---|
icon | The select icon container. |
placeholder | The text displayed in the select when there is no value. |
text | The displayed value of the select. |
CSS Custom Properties
| Name | Description |
|---|---|
--padding-bottom | Bottom padding of the select |
--padding-end | Right padding if direction is left-to-right, and left padding if direction is right-to-left of the select |
--padding-start | Left padding if direction is left-to-right, and right padding if direction is right-to-left of the select |
--padding-top | Top padding of the select |
--placeholder-color | Color of the select placeholder text |
--placeholder-opacity | Opacity of the select placeholder text |

0 Nhận xét