uniapp内置组件picker-view赋默认值

使用picker-view组件实现日期选择

需求:默认选中已知的年月日

<picker-view indicator-style="height:40px" :value="value" @change="bindChange" class="picker-view">
    <picker-view-column>
        <view class="item" v-for="(item, index) in years" :key="index" :class="{ 'activeOn': index == value[0] }">{{ item }}年</view>
    </picker-view-column>
    <picker-view-column>
        <view class="item" v-for="(item, index) in months" :key="index" :class="{ 'activeOn': index == value[1] }">{{ item }}月</view>
    </picker-view-column>
    <picker-view-column>
        <view class="item" v-for="(item, index) in days" :key="index" :class="{ 'activeOn': index == value[2] }">{{ item }}日</view>
    </picker-view-column>
</picker-view>
data() {
    const date = new Date()
    const years = []
    const year = date.getFullYear()
    const months = []
    const month = date.getMonth() + 1
    const days = []
    const day = date.getDate()
    for (let i = 1950; i <= date.getFullYear(); i++) {
        years.push(i)
    }
    for (let i = 1; i <= 12; i++) {
        months.push(i)
    }
    for (let i = 1; i <= 31; i++) {
        days.push(i)
    }
    return {
        years,
        year,
        months,
        month,
        days,
        day,
        value: [9999, month - 1, day - 1],  // 每一列的索引,这里是当前日期
        Birthday: '',
        Gender: null,
        WordCityIds: '',
        WordCityId: '',
        WorkCityName: '',
        bir: ''
    };
}
onLoad(e) {
    let birVal = e.bir.split('-');  // 从上个页面传过来的值 '2002-04-13'
    this.updataDefaultBir(birVal)
},
methods: {
    updataDefaultBir(arr) {
        let yearIndex = this.years.findIndex(item => item == arr[0]);
        let monthsIndex = this.months.findIndex(item => item == arr[1]);
        let daysIndex = this.days.findIndex(item => item == arr[2]);
        this.value = [yearIndex, monthsIndex, daysIndex];
    },
    bindChange: function (e) {
        const val = e.detail.value
        this.year = this.years[val[0]]
        this.month = this.months[val[1]]
        this.day = this.days[val[2]]
        this.value = val;
    }
}
.picker-view {
    width: 100%;
    height: rpx(300);
    margin-top: rpx(20);
}
.item {
    line-height: rpx(40);
    text-align: center;
}
.activeOn {
    color: #ffaa21;
}

效果图 

以下是在uniapp中使用picker-view组件设置时和分的代码示例: 1. 在页面的.vue文件中添加以下代码: ``` <template> <view class="content"> <picker-view :value="time" @change="onTimeChange"> <picker-view-column> <view v-for="(hour, index) in hours" :key="index">{{hour}}</view> </picker-view-column> <picker-view-column> <view v-for="(minute, index) in minutes" :key="index">{{minute}}</view> </picker-view-column> </picker-view> </view> </template> <script> export default { data() { return { time: [0, 0], // 默认选中的时间 hours: [], // 小时 minutes: [] // 分钟 } }, mounted() { // 初始化小时和分钟 this.initTime() }, methods: { initTime() { // 初始化小时 for (let i = 0; i < 24; i++) { this.hours.push(i < 10 ? '0' + i : '' + i) } // 初始化分钟 for (let i = 0; i < 60; i++) { this.minutes.push(i < 10 ? '0' + i : '' + i) } }, onTimeChange(e) { // 获取选中的时间 const time = e.detail.value console.log('选中的时间:', time) } } } </script> <style> .content { display: flex; justify-content: center; align-items: center; height: 100%; } </style> ``` 2. 在上述代码中,我们首先初始化了小时和分钟的数组,然后在模板中使用picker-view组件来展示小时和分钟的选择器。我们设置picker-view组件的value属性为time,表示默认选中的时间,然后通过@change事件来获取选中的时间。 3. 在onTimeChange方法中,我们可以通过e.detail.value来获取选中的时间,然后进行后续处理。 4. 最后,在样式中我们设置了.content的样式,让picker-view组件居中显示。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值