123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- // components/picker/index.js
- const { formatTime } = require("../../utils/util");
- Component({
- options: {
- observers: true,
- lifetimes: true
- },
- /**
- * 组件的属性列表
- */
- properties: {
- // picker类型
- type: {
- type: String,
- value: ""
- },
- // 未选择时提示
- placeholder: {
- type: Array,
- value: ["请选择"]
- },
- // 数据列表
- list: {
- type: Array,
- value: []
- },
- // 显示参数key
- rangeKey: {
- type: String,
- value: ""
- },
- // 选择下标
- index: {
- value: null
- },
- // 日期时间选择器
- dateTime: {
- type: String,
- value: null
- },
- delay: {
- type: Number,
- value: null
- },
- isAudit: {
- type: Boolean,
- value: false
- }
- },
- /**
- * 组件的初始数据
- */
- data: {
- index: null,
- //普通选择器下标
- date: null,
- //日期
- time: null //时间
- },
- /**
- * 组件的方法列表
- */
- lifetimes: {
- attached: function () {
- if (this.data.type === "date-time" && this.data.dateTime === null) {
- this.getTime();
- }
- },
- detached: function () {
- // 在组件实例被从页面节点树移除时执行
- }
- },
- observers: {
- dateTime: function (dateTime) {
- if (this.data.type === "date-time") {
- this.getTime();
- }
- }
- },
- methods: {
- // 获得传入时间或默认当前时间
- getTime() {
- var { date, time, dateTime, delay } = this.data;
- var newDateTime = null;
- if (dateTime) {
- newDateTime = dateTime.replaceAll("-", "/");
- } else if (delay != null) {
- var nowDateTime = Date.now() + delay;
- newDateTime = nowDateTime;
- } else {
- return;
- }
- date = formatTime(newDateTime, "YYYY-MM-DD");
- time = formatTime(newDateTime, "HH:NN");
- this.setData({
- date,
- time
- });
- if (date && time) {
- this.triggerEvent("change", date + " " + time + ":00");
- }
- },
- // 选择普通项
- chooseItem(e) {
- var { index } = this.data;
- index = e.detail.value;
- this.setData({
- index
- });
- this.triggerEvent("change", this.data.list[index]);
- },
- // 选择日期
- chooseDateTime(e) {
- var { date, time } = this.data;
- switch (e.currentTarget.dataset.type) {
- case "date":
- date = e.detail.value;
- this.setData({
- date
- });
- break;
- case "time":
- time = e.detail.value;
- this.setData({
- time
- });
- break;
- default:
- break;
- }
- if (date && time) {
- this.triggerEvent("change", date + " " + time + ":00");
- }
- }
- }
- });
|