Skip to content

Table 表格

用于展示多列结构化数据、财务报表、进度对比的表格控件,支持横向滚动、按列排序、斑马纹、带边框与单元格插槽。


代码示例

1. 基础数据表格 (Basic Table)

设置 columns 列配置数组与 data 数据列表,开启 bordered 边框与 striped 斑马纹。

vue
<template>
  <mu-table :columns="columns" :data="data" bordered striped />
</template>

<script setup>
import { ref } from 'vue'

const columns = ref([
  { title: '姓名', prop: 'name', width: 140 },
  { title: '部门', prop: 'department', width: 180 },
  { title: '职级', prop: 'role', width: 140 },
  { title: '城市', prop: 'city', width: 140 }
])

const data = ref([
  { name: '张三', department: '前端架构部', role: 'P7', city: '杭州' },
  { name: '李四', department: '终端UI组', role: 'P6', city: '上海' },
  { name: '王五', department: '移动端开发', role: 'P8', city: '北京' }
])
</script>

2. 按列排序与自定义单元格插槽 (Sort & Slots)

在列配置中指定 sortable: true 开启列头排序功能。使用 #cell-[prop] 来自定义具体列的单元格渲染。

vue
<template>
  <mu-table :columns="columns" :data="data" @row-click="onRowClick">
    <template #cell-status="{ row }">
      <mu-tag :type="row.status === '正常' ? 'success' : 'warning'" size="small">
        {{ row.status }}
      </mu-tag>
    </template>
  </mu-table>
</template>

<script setup>
import { ref } from 'vue'

const columns = ref([
  { title: '项目名称', prop: 'projectName', width: 220 },
  { title: '进度', prop: 'progress', sortable: true, width: 140 },
  { title: '状态', prop: 'status', width: 140 }
])

const data = ref([
  { projectName: 'MuzhiyuUI 组件库 1.0', progress: 100, status: '正常' },
  { projectName: 'VisionOS 拟态交互', progress: 75, status: '处理中' }
])

const onRowClick = ({ row }) => {
  console.log('点击行:', row)
}
</script>

Column 列表配置属性 (Column Object)

参数名说明类型可选值默认值
title列名文本String-''
prop / key对应数据项的字段属性键名String-''
width列宽度 (支持 '160rpx' 或数字)Number | String-'160rpx'
align单元格对齐方式Stringleft | center | right'left'
headerAlign表头文字对齐方式 (默认同 align)Stringleft | center | right-
sortable是否开启该列的升序/降序排序Booleantrue | falsefalse
fixed是否固定该列在左右侧String | Booleanleft | right | truefalse
formatter自定义格式化函数Function(row: Object) => String-

API 属性说明 (Props)

参数名说明类型可选值默认值
columns表格列定义配置数组Array-[]
data表格数据源数组Array-[]
bordered是否带有外边框与纵向分隔线条Booleantrue | falsefalse
striped是否开斑马纹隔行变色Booleantrue | falsefalse
defaultSort默认排序配置项 (`{ prop, order: 'asc''desc' }`)Object-
minWidth表格总体最小最小宽度Number | String-''

事件说明 (Events)

事件名说明回调参数
sort-change排序字段或排序方向改变时触发({ column: Object, index: Number, direction: 'asc' | 'desc' })
row-click点击整行时触发({ row: Object, index: Number })

基于 MIT 协议开源发行

H5 模拟器 (table)☀️🔄