返回介绍

Ionic4 选择框组件 ion-select

发布于 2019-11-22 18:04:18 字数 7504 浏览 2672 评论 0 收藏 0

Ionic4项目中我们可以使用Ionic4选择框组件ion-select对项目进行布局。

ion-select官方文档地址:https://ionicframework.com/docs/api/select

Selects are form controls to select an option, or options, from a set of options, similar to a native <select> element. When a user taps the select, a dialog appears with all of the options in a large, easy to select list.

A select should be used with child <ion-select-option> elements. If the child option is not given a value attribute then its text will be used as the value.

If value is set on the <ion-select>, the selected option will be chosen based on that value. Otherwise, the selected attribute can be used on the <ion-select-option>.

Interfaces

By default, select uses the AlertController API to open up the overlay of options in an alert. The interface can be changed to use the ActionSheetController API or PopoverController API by passing action-sheet or popover, respectively, to the interface property. Read on to the other sections for the limitations of the different interfaces.

Single Selection

By default, the select allows the user to select only one option. The alert interface presents users with a radio button styled list of options. The action sheet interface can only be used with a single value select. The select component's value receives the value of the selected option's value.

Multiple Selection

By adding the multiple attribute to select, users are able to select multiple options. When multiple options can be selected, the alert overlay presents users with a checkbox styled list of options. The select component's value receives an array of all of the selected option values.

Note: the action-sheet and popover interfaces will not work with multiple selection.

Object Value References

When using objects for select values, it is possible for the identities of these objects to change if they are coming from a server or database, while the selected value's identity remains the same. For example, this can occur when an existing record with the desired object value is loaded into the select, but the newly retrieved select options now have different identities. This will result in the select appearing to have no value at all, even though the original selection in still intact.

By default, the select uses object equality (===) to determine if an option is selected. This can be overridden by providing a property name or a function to the compareWith property.

Select Buttons

By default, the alert has two buttons: Cancel and OK. Each button's text can be customized using the cancelText and okText properties.

The action-sheet and popover interfaces do not have an OK button, clicking on any of the options will automatically close the overlay and select that value. The popover interface does not have a Cancel button, clicking on the backdrop will close the overlay.

Interface Options

Since select uses the alert, action sheet and popover interfaces, options can be passed to these components through the interfaceOptions property. This can be used to pass a custom header, subheader, css class, and more. See the AlertController API docs, ActionSheetController API docs, and PopoverController API docs for the properties that each interface accepts.

ion-select 用法(Usage)

Single Selection


  Single Selection

  
    Gender
    
      Female
      Male
    
  

  
    Hair Color
    
      Brown
      Blonde
      Black
      Red
    
  

</ion-list>

Multiple Selection


  Multiple Selection

  
    Toppings
    
      Bacon
      Black Olives
      Extra Cheese
      Green Peppers
      Mushrooms
      Onions
      Pepperoni
      Pineapple
      Sausage
      Spinach
    
  

  
    Pets
    
      Bird
      Cat
      Dog
      Honey Badger
    
  
</ion-list>

Objects as Values


  Objects as Values (compareWith)

  
    Users
    
      {{user.first + ' ' + user.last}}
    
  
</ion-list>
import { Component } from '@angular/core';

@Component({
  selector: 'select-example',
  templateUrl: 'select-example.html',
  styleUrls: ['./select-example.css'],
})
export class SelectExample {
  users: any[] = [
    {
      id: 1,
      first: 'Alice',
      last: 'Smith',
    },
    {
      id: 2,
      first: 'Bob',
      last: 'Davis',
    },
    {
      id: 3,
      first: 'Charlie',
      last: 'Rosenburg',
    }
  ];

  compareWithFn = (o1, o2) => {
    return o1 && o2 ? o1.id === o2.id : o1 === o2;
  };

  compareWith = compareWithFn;
}

Interface Options


  Interface Options

  
    Alert
    
      Bacon
      Black Olives
      Extra Cheese
      Green Peppers
      Mushrooms
      Onions
      Pepperoni
      Pineapple
      Sausage
      Spinach
    
  

  
    Popover
    
      Brown
      Blonde
      Black
      Red
    
  

  
    Action Sheet
    
      Red
      Purple
      Yellow
      Orange
      Green
    
  

</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


  Single Selection

  
    Gender
    
      Female
      Male
    
  

  
    Hair Color
    
      Brown
      Blonde
      Black
      Red
    
  

</ion-list>

Multiple Selection


  Multiple Selection

  
    Toppings
    
      Bacon
      Black Olives
      Extra Cheese
      Green Peppers
      Mushrooms
      Onions
      Pepperoni
      Pineapple
      <spa
                  
                  
                

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文