微信小程序原生,tdesign时间选择器,t-date-time-picker封装成组件,开始时间结束时间

首先,在项目的 components 目录下创建一个新的文件夹,例如 date-picker,并在其中创建以下文件:

  • date-picker.wxml
  • date-picker.wxss
  • date-picker.js
  • date-picker.json

2.date-picker.wxml

html 复制代码
<view class="date-picker">
  <t-cell
    title="{{title}}"
    hover
    note="{{dateText || '请选择'}}"
    arrow
    bindtap="showPicker"
    t-class="panel-item"
  />
  
  <t-date-time-picker
    auto-close
    title="选择日期"
    visible="{{visible}}"
    mode="date"
    default-value="{{date}}"
    format="YYYY-MM-DD"
    start="{{start}}"
    end="{{end}}"
    filter="{{filter}}"
    popup-props="{{popupProps}}"
    bindchange="onConfirm"
    bindpick="onColumnChange"
    bindcancel="hidePicker"
    bindclose="handleClose"
  />
</view>

date-picker.js

TypeScript 复制代码
Component({
  properties: {
    title: { type: String, value: '请选择日期' }, // 标题
    date: { type: String, value: '' }, // 默认为空,将在 attached 中设置
    start: { type: String, value: '' },
  },

  data: {
    visible: false, // 选择器是否可见
    dateText: '',    // 显示的日期文本
    // 指定选择区间起始值
    end: '2030-09-09 12:12:12',
    filter(type, options) {
      if (type === 'year') {
        return options.sort((a, b) => b.value - a.value);
      }
      return options;
    },
    popupProps: {
      usingCustomNavbar: true,
    },
  },
  observers: {
    'date': function (newVal) {
      this.setData({ dateText: newVal || '' });
    },
    'start': function(newVal) {
      if (newVal && this.data.end && newVal > this.data.end) {
        this.setData({ end: newVal });
      }
    },
    'end': function(newVal) {
      if (newVal && this.data.start && newVal < this.data.start) {
        wx.showToast({
          title: '结束日期必须大于或等于开始日期',
          icon: 'none'
        });
        this.setData({ end: '' });
      }
    }
  },

  methods: {
    // 显示选择器
    showPicker() {
      this.setData({ visible: true });
    },

    // 隐藏选择器
    hidePicker() {
      this.setData({ visible: false });
    },

    // 确认选择
    onConfirm(e) {
      const selectedDate = e.detail.value;
      if (this.data.title === '结束日期' && this.data.start && selectedDate < this.data.start) {
        wx.showToast({
          title: '结束日期必须大于或等于开始日期',
          icon: 'none'
        });
        return;
      }
      this.setData({
        date: selectedDate,
        visible: false
      });
      this.triggerEvent('datechange', selectedDate); // 触发事件传递选择的日期
    },

    // 列变化(可选)
    onColumnChange(e) {
      // 处理列变化逻辑(可选)
    },

    // 关闭处理(可选)
    handleClose() {
      this.hidePicker();
    }
  }
});

date-picker.json

TypeScript 复制代码
{
  "component": true,
  "usingComponents": {
    "t-cell": "tdesign-miniprogram/cell/cell",
    "t-date-time-picker": "tdesign-miniprogram/date-time-picker/date-time-picker"
  }
}

使用组件

在需要使用日期选择器的页面中,引入并使用该组件。

开始时间为今天时间,结束时间大于等于开始时间。

TypeScript 复制代码
{
  "usingComponents": {
    "date-picker":"/components/date-picker/date-picker",
  }
}
html 复制代码
 <view class="title">
          <date-picker title="开始日期" start="{{today}}" date="{{startDate}}" bind:datechange="onStartDateChange" />
        </view>
        <view class="title">
          <date-picker title="结束日期" start="{{startDate}}" date="{{endDate}}" bind:datechange="onEndDateChange" />
        </view>
javascript 复制代码
data:{
    //日期选择
    startDate: '',
    endDate: '',
    today: '', // 在 onLoad 中设置
}

 onLoad(options) {
    this.setTodayDate();
  },


 //日期选择
  setTodayDate() {
    const today = new Date();
    const year = today.getFullYear();
    const month = String(today.getMonth() + 1).padStart(2, '0');
    const day = String(today.getDate()).padStart(2, '0');
    const formattedDate = `${year}-${month}-${day}`;
    this.setData({ today: formattedDate, startDate: formattedDate });
  },
  onStartDateChange(e) {
    this.setData({ startDate: e.detail });
  },

  onEndDateChange(e) {
    this.setData({ endDate: e.detail });
  },
  
相关推荐
h_654321017 分钟前
微信小程序点击按钮跳转链接并显示
微信小程序·小程序
银迢迢3 小时前
微信小程序的开发及问题解决
微信小程序·小程序
liyinchi19883 小时前
原生微信小程序 textarea组件placeholder无法换行的问题解决办法
微信小程序·小程序
说私域5 小时前
基于开源链动2+1模式AI智能名片S2B2C商城小程序的低集中度市场运营策略研究
人工智能·小程序·开源·零售
少恭写代码8 小时前
duxapp 2025-03-29 更新 编译结束的复制逻辑等
react native·小程序·taro
suncentwl9 小时前
答题pk小程序道具卡的获取与应用
小程序·答题小程序·知识竞赛
bysjlwdx9 小时前
uniapp婚纱预约小程序
小程序·uni-app
ALLSectorSorft9 小时前
代驾小程序订单系统框架搭建
小程序·代驾小程序
qq_12498707539 小时前
原生小程序+springboot+vue+协同过滤算法的音乐推荐系统(源码+论文+讲解+安装+部署+调试)
java·spring boot·后端·小程序·毕业设计·课程设计·协同过滤
前端极客探险家19 小时前
微信小程序全解析:从入门到实战
微信小程序·小程序