ionic4 ion-picker用法

ion-picker实际开发中肯定多处使用,所以封装成服务的形式调用

新建picker.service服务模块   ionic g service picker 

import { Injectable, Component, OnInit } from '@angular/core';
import { PickerController } from '@ionic/angular';

@Injectable({
    providedIn: 'root'
})
export class PickerService {
    constructor(public pickercontroller: PickerController) {}
    async openPicker(numColumns = 1, numOptions = 5, multiColumnOptions,callback) {
        const picker = await this.pickercontroller.create({
            columns: this.getColumns(numColumns, numOptions, multiColumnOptions),
            buttons: [
                {
                    text: '取消',
                    role: 'cancel'
                },
                {
                    text: '確定',
                    handler: value => {
                        // console.log(`Got Value ${value}`);
                        callback(JSON.stringify(value))
                    }
                }
            ]
        });
        await picker.present();
    }
    getColumns(numColumns, numOptions, columnOptions) {
        let columns = [];
        for (let i = 0; i < numColumns; i++) {
            columns.push({
                name: `col-${i}`,
                options: this.getColumnOptions(i, numOptions, columnOptions)
            });
        }
        return columns;
    }
    getColumnOptions(columnIndex, numOptions, columnOptions) {
        let options = [];
        for (let i = 0; i < numOptions; i++) {
            options.push({
                text: columnOptions[columnIndex][i % numOptions],
                value: i
            });
        }
        return options;
    }
}

在组件中使用:

html中:

ts中:

import { Component, OnInit } from '@angular/core';
import { PickerController } from '@ionic/angular';
import { PickerService } from '../picker/picker.service';

@Component({
    selector: 'app-head1',
    templateUrl: './head1.page.html',
    styleUrls: ['./head1.page.scss']
})
export class Head1Page implements OnInit {
    Options: any = {
        header: '開放身份',
        subHeader: 'Select your favorite color'
    };
    public roleOptions = [['全部', '老師', '學生', '家長']];
    public roleText = '開放身份'; //选择的角色
    public subjectOptions = [['全部學科', '體育', '科學', '語文', '數學', '英語', '音樂', '美術', '品德與生活', '信息技術', '計算機']];
    public subjectText = '全部學科'; //选择的学科
    constructor(public pickercontroller: PickerController, public pickerService: PickerService) {}

    ngOnInit() {}
    pickerFn($start, $length, $option, type) {
        let that = this;
        this.pickerService.openPicker($start, $length, $option, function(result) {
            let vals = JSON.parse(result)['col-0'].text;
            switch (type) {
                case 'role':
                    that.roleText = vals;
                    break;
                case 'subject':
                    that.subjectText = vals;
                    break;
                default:
                    break;
            }
        });
    }
}

 

转载于:https://www.cnblogs.com/mary-123/p/10912755.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值