Skip to content

Vue VtTimePicker 时间选择 ​

时间列。与 VtDatePicker 同构,编辑态是原生 input[type=time]。

示例 ​

微应用尚未挂载。

源码 ​

点击查看源码
vue
<template>
  <div class="demo-wrapper">
    <h3 class="demo-title">Vue VtTimePicker 时间选择</h3>
    <div class="status-text">{{ status }}</div>
    <div style="width: 880px; height: 600px" class="demo-container">
      <VirtTableVue :columns="columns" :options="options" />
    </div>
  </div>
</template>

<script setup lang="ts">
import { VirtTableVue, type VueTableColumn, vtCellEditor } from '@virt-table/vue';
import { VtTimePicker } from '@virt-table/vanilla/components';

const ROW_COUNT = 5000;

const columns: VueTableColumn[] = [
  { key: 'id', title: 'ID', width: 80, align: 'center' },
  { key: 'name', title: '排班', width: 160 },
  VtTimePicker({ key: 'start', title: '开始', width: 150 }),
  VtTimePicker({ key: 'end', title: '结束', width: 150, clearable: true }),
  // step: 1 让原生控件出现秒位;min/max 限制可选范围
  VtTimePicker({ key: 'checkAt', title: '打卡(含秒)', width: 180, step: 1, min: '06:00', max: '23:00' }),
  VtTimePicker({ key: 'fixed', title: '只读', width: 150, disabled: true }),
];

const pad = (n: number) => String(n).padStart(2, '0');
const list = Array.from({ length: ROW_COUNT }, (_, i) => ({
  id: i,
  name: `班次 ${i}`,
  start: `${pad(8 + (i % 4))}:${pad((i * 15) % 60)}`,
  end: `${pad(17 + (i % 3))}:${pad((i * 20) % 60)}`,
  checkAt: `${pad(8 + (i % 4))}:${pad((i * 7) % 60)}:${pad((i * 13) % 60)}`,
  fixed: '09:00',
}));

const options = {
  list,
  itemKey: 'id',
  estimatedSize: 40,
  buffer: 4,
  border: true,
  plugins: [vtCellEditor()],
};

const status =
  'VtTimePicker:与 VtDatePicker 同构,用原生 input[type=time]。值就是控件的原始字符串(HH:mm,配了 step 则含秒),不做二次解析。清除「×」默认就有(原生 time 控件「怎么删回空」各家不一),传 clearable: false 关掉';
</script>

说明 ​

值就是原生控件的字符串格式('09:30',step 带秒时 '09:30:15'),不做二次解析 —— 表格拿到什么就存什么,格式化留给使用方的 render 或数据层。走 renderEditor,必须装 vtCellEditor 插件。

日期与区间那两个组件已经换成自绘面板(跟随暗色主题),时间列仍是原生控件:原生 input[type=time] 的下拉由操作系统渲染,吃不到 --vt-*。

清除「×」在控件右侧、默认就有(原生 time 控件「怎么删回空」各家不一,给个叉更确定), 清空写回 '';不需要的列传 clearable: false。

相关 API ​

字段作用
min / max可选范围
step步进(秒);给 1 出现秒位

全量选项表见 单元格组件;公共选项 (placeholder / disabled / clearable / onChange)、asCell()、三类交互模型见 同页上方;列的通用字段见 列配置。

编辑态需装 vtCellEditor,见 插件机制。

上面的字段在 Vue 端原样透传(实例方法通过组件 ref 调用)。封装层特有的部分 —— 组件 Props、渲染函数返回 VNode、ref 转发了哪些方法 —— 见 Vue API。