element-plus el-time-picker 时分数据回显

<template>
  <div>
    <el-table :data="tableData">
      <el-table-column label="名称" prop="name"></el-table-column>
      <el-table-column label="时间范围">
        <template #default="scope">
          <el-time-picker
            v-model="scope.row.times"
            is-range
            range-separator="至"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
            format="HH:mm"
            @change="changeTimeRange(scope.row, scope.$index)"
            :default-value="defaultTime2"
          />
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script setup>
import { ref, watch } from 'vue';
import { parseTime } from './utils'; // 假设你有一个工具函数来解析时间

const tableData = ref([
  { name: '政策1', times: ['09:00', '12:00'] },
  { name: '政策2', times: ['10:00', '13:00'] }
]);

// 设置默认选中的时间范围
const defaultTime2 = ref([
  new Date('2023-01-01T09:00:00'),
  new Date('2023-01-01T12:00:00')
]);

// 监听 tableData 的变化,确保 times 是 Date 对象
watch(tableData, (newVal) => {
  newVal.forEach((item) => {
    if (Array.isArray(item.times) && item.times.length === 2) {
      item.times = item.times.map(timeStr => parseTime(timeStr));
    }
  });
}, { immediate: true });

const changeTimeRange = (item, index) => {
  console.log('Time range changed for item:', item, 'at index:', index);
  // 你可以在这里执行其他操作,例如发送到后端
};

// 工具函数:将时间字符串转换为 Date 对象
function parseTime(timeStr) {
  const [hours, minutes] = timeStr.split(':').map(Number);
  return new Date(2023, 0, 1, hours, minutes);
}
</script>
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值