table.md 5.07 KB

表格组件

功能强大的数据表格组件,支持排序、筛选、分页、行选择等功能。

基础用法

<template>
  <Table
    :columns="columns"
    :data-source="dataSource"
    :loading="loading"
    @change="handleChange"
  />
</template>

<script setup lang="ts">
import { ref } from 'vue';

const columns = [
  {
    title: '姓名',
    dataIndex: 'name',
    key: 'name',
  },
  {
    title: '年龄',
    dataIndex: 'age',
    key: 'age',
    sorter: true,
  },
  {
    title: '地址',
    dataIndex: 'address',
    key: 'address',
  },
  {
    title: '操作',
    key: 'action',
    width: 200,
  },
];

const dataSource = ref([
  {
    id: '1',
    name: '张三',
    age: 32,
    address: '北京市朝阳区',
  },
  {
    id: '2',
    name: '李四',
    age: 28,
    address: '上海市浦东新区',
  },
]);

const loading = ref(false);

const handleChange = (pagination, filters, sorter) => {
  console.log('表格变化', { pagination, filters, sorter });
};
</script>

列配置

基础列配置

const columns = [
  {
    title: '列标题',
    dataIndex: 'field',
    key: 'field',
    width: 200,
    align: 'center',
    fixed: 'left',
  },
];

自定义渲染

<template>
  <Table :columns="columns" :data-source="dataSource">
    <template #bodyCell="{ column, record }">
      <template v-if="column.key === 'status'">
        <Tag :color="record.status === 1 ? 'green' : 'red'">
          {{ record.status === 1 ? '启用' : '禁用' }}
        </Tag>
      </template>

      <template v-if="column.key === 'action'">
        <Button @click="handleEdit(record)">编辑</Button>
        <Button @click="handleDelete(record)">删除</Button>
      </template>
    </template>
  </Table>
</template>

排序和筛选

排序

const columns = [
  {
    title: '年龄',
    dataIndex: 'age',
    key: 'age',
    sorter: true, // 启用排序
  },
];

const handleChange = (pagination, filters, sorter) => {
  const { field, order } = sorter;
  // order: 'ascend' | 'descend' | undefined
  fetchData({ sortField: field, sortOrder: order });
};

筛选

const columns = [
  {
    title: '状态',
    dataIndex: 'status',
    key: 'status',
    filters: [
      { text: '启用', value: 1 },
      { text: '禁用', value: 0 },
    ],
  },
];

分页

<template>
  <Table
    :columns="columns"
    :data-source="dataSource"
    :pagination="pagination"
    @change="handleChange"
  />
</template>

<script setup lang="ts">
const pagination = ref({
  current: 1,
  pageSize: 10,
  total: 100,
  showSizeChanger: true,
  showQuickJumper: true,
  showTotal: (total) => `共 ${total} 条`,
});

const handleChange = (pag) => {
  pagination.value.current = pag.current;
  pagination.value.pageSize = pag.pageSize;
  fetchData();
};
</script>

行选择

<template>
  <Table
    :columns="columns"
    :data-source="dataSource"
    :row-selection="rowSelection"
  />
</template>

<script setup lang="ts">
const selectedRowKeys = ref([]);

const rowSelection = computed(() => ({
  selectedRowKeys: selectedRowKeys.value,
  onChange: (keys, rows) => {
    selectedRowKeys.value = keys;
    console.log('选中的行:', rows);
  },
  getCheckboxProps: (record) => ({
    disabled: record.disabled,
  }),
}));
</script>

可展开行

<template>
  <Table
    :columns="columns"
    :data-source="dataSource"
    :expandable="expandable"
  >
    <template #expandedRowRender="{ record }">
      <div>{{ record.description }}</div>
    </template>
  </Table>
</template>

<script setup lang="ts">
const expandable = {
  expandedRowKeys: ref([]),
  onExpand: (expanded, record) => {
    console.log('展开/收起', expanded, record);
  },
};
</script>

虚拟滚动

处理大量数据时使用虚拟滚动提升性能:

<template>
  <Table
    :columns="columns"
    :data-source="dataSource"
    :scroll="{ y: 600 }"
    virtual
  />
</template>

API

Table Props

属性 说明 类型 默认值
columns 列配置 Column[] -
dataSource 数据源 any[] -
loading 加载状态 boolean false
pagination 分页配置 `Pagination \ false`
rowSelection 行选择配置 RowSelection -
scroll 滚动配置 { x?: number, y?: number } -
virtual 是否启用虚拟滚动 boolean false

Column

属性 说明 类型 默认值
title 列标题 string -
dataIndex 数据字段 string -
key 列的唯一标识 string -
width 列宽度 `number \ string`
align 对齐方式 `'left' \ 'center' \
fixed 固定列 `'left' \ 'right'`
sorter 是否可排序 `boolean \ Function`
filters 筛选配置 Filter[] -

Events

事件名 说明 参数
change 表格变化 (pagination, filters, sorter) => void
select 行选择变化 (selectedRowKeys, selectedRows) => void