微信小程序 vant 封装DatetimePicker时间选择器

6 篇文章 0 订阅
4 篇文章 0 订阅

效果:
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

1.在components文件下新建dateTimePicker文件
在这里插入图片描述

在index.wxml文件中编写:

<view class="container" bindtap="showPop">
    <van-popup show="{{ isShowpopup }}" bind:close="onClose"
     position="bottom">
 <van-datetime-picker
  type="date"
  value="{{ currentDate }}"
  bind:input="onInput"
  bind:confirm="onConfirm"
  bind:cancel="onCancel"
  min-date="{{ minDate }}"
  formatter="{{ formatter }}"
/>
</van-popup>
</view>

index.json文件中编写方法,属性

Component({
    /**
     * 组件的属性列表
     */
    properties: {
        isShowpopup: {
            type: Boolean,
        },
    },

    /**
     * 组件的初始数据
     */
    data: {
        currentDate: new Date().getTime(),
        minDate: new Date(1940, 0, 1).getTime(),
        formatter(type, value) {
            if (type === 'year') {
                return `${value}年`;
            }
            if (type === 'month') {
                return `${value}月`;
            }
            return value;
        },
    },

    /**
     * 组件的方法列表
     */
    methods: {
        // 出生日期
        showPop(data) {
            this.setData({
                isShowpopup: data
            })
        },
        // 选择日期
        onInput(event) {
            this.setData({
                currentDate: event.detail,
            });
        },
        // 选择日期 确认按钮
        onConfirm(val) {
          let a = this._timeFormat(val.detail);
            // this.setData({
            //     isShowpopup: false
            // })
            this.triggerEvent('showing',{
                selectDate:a,
                isShowpopup: false
            })
        },
        // 取消按钮
        onCancel(){
            this.triggerEvent('canceling',{
                isShowpopup: false
            })
        },
        _timeFormat(time) { // 时间格式化 2019-09-08
            console.log(time,"++")
            var time = new Date(time);
            let year = time.getFullYear();
            let month = time.getMonth() + 1;
            let day = time.getDate();
            let h = time.getHours(); h = h < 10 ? ('0' + h) : h;
            let minutes = time.getMinutes(); minutes = minutes < 10 ? ('0' + minutes) : minutes;					
            return year + '-' + month + '-' + day 
        }

    }
})

使用该组件的页面中引用:
在这里插入图片描述
index.json中引用,注册

{
  "usingComponents": {
  "v-dateTimePicker":"../../components/dateTimePicker/index"
  }
}

index .wxml中使用

 <view>
    <v-dateTimePicker id="page" bind:showing="onConfirm"  bind:canceling="onCancel"
     wx:if="{{isShowpopup}}"/>
</view>

index.js文件 定义方法,数据

data中定义:

  birthDate: '请选择出生日期', // 出生日期
  isShowpopup: false,
    showBirthDate() {
        this.setData({
            isShowpopup: true
        })
        // 父组件调用子组件中的方法
        this.selectComponent("#page").showPop('isShowpopup')
    },
    // 确认按钮
    onConfirm(e) {
        this.setData({
            isShowpopup: e.detail.isShowpopup,
            birthDate: e.detail.selectDate
        })
    },
    // 取消按钮
    onCancel(e) {
        this.setData({
            isShowpopup: e.detail.isShowpopup,

        })
    },

完整代码,做一个小记录~

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
微信小程序vant weapp提供了TreeSelect组件来实现三级分类选择。要实现三级分类选择,我们需要先获取分类数据,并将其转换为树状结构。 首先,在小程序页面的json文件中引入vant组件库,并设置所需的TreeSelect组件。 ```json { "usingComponents": { "van-tree-select": "/path/to/vant/weapp/dist/tree-select/index" } } ``` 然后,在小程序页面的wxml文件中添加TreeSelect组件,并绑定所需的属性和事件。 ```html <van-tree-select items="{{ treeData }}" main-active-index="{{ mainActiveIndex }}" activeId="{{ activeId }}" bind:click-nav="handleClickNav" bind:click-item="handleClickItem" /> ``` 在小程序页面的js文件中,定义相关数据和方法。 ```javascript Page({ data: { treeData: [], // 分类数据 mainActiveIndex: 0, // 主选项卡索引 activeId: '', // 选中的分类id }, onLoad() { // 获取分类数据,并将其转换为树状结构 const data = this.getCategoryData(); const treeData = this.convertToTree(data); this.setData({ treeData: treeData }); }, getCategoryData() { // 从接口或本地获取分类数据 // 返回分类数据数组 }, convertToTree(data) { // 将分类数据转换为树状结构 // 返回树状结构的数据 }, handleClickNav(event) { // 切换主选项卡时的回调函数 this.setData({ mainActiveIndex: event.detail.index }); }, handleClickItem(event) { // 选择子分类时的回调函数 this.setData({ activeId: event.detail.id }); } }); ``` 通过以上步骤,我们就可以使用微信小程序vant weapp实现三级treeselect分类选择功能了。需要注意的是,具体的分类数据获取和转换还需要根据实际情况进行处理。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值