Table 表格

大约 9 分钟

Table 表格

何时使用

  • 当有大量结构化的数据需要展现时;
  • 当需要对数据进行排序、搜索、分页、自定义操作等复杂行为时。

如何使用

指定表格的数据源 dataSource 为一个数组。

<template>
  <f-table :dataSource="dataSource" :columns="columns" />
</template>
<script>
  export default {
    setup() {
      return {
        dataSource: [
          {
            key: '1',
            name: '胡彦斌',
            age: 32,
            address: '西湖区湖底公园1号',
          },
          {
            key: '2',
            name: '胡彦祖',
            age: 42,
            address: '西湖区湖底公园1号',
          },
        ],

        columns: [
          {
            title: '姓名',
            dataIndex: 'name',
            key: 'name',
          },
          {
            title: '年龄',
            dataIndex: 'age',
            key: 'age',
          },
          {
            title: '住址',
            dataIndex: 'address',
            key: 'address',
          },
        ],
      };
    },
  };
</script>

基本用法

基本用法。


树形数据展示

表格支持树形数据的展示,当数据中有 children 字段时会自动展示为树形表格,如果不需要或配置为其他字段可以用 childrenColumnName 进行配置。 可以通过设置 indentSize 以控制每一层的缩进宽度。


可展开

当表格内容较多不能一次性完全展示时。


可操作

通过配置对表格的列,样式进行操作。


API

Table

参数说明类型默认值版本
fullScreen
新增
是否显示全屏显示操作booleanfalse
columnOperated
新增
是否显示列操作booleanfalse
columnDropdown
新增
显示列操作弹框的配置项,具体配置参照组件Dropdown配置object{trigger:['click'], placement:'bottomRight'}
sizeOperated
新增
是否显示表格尺寸操作booleanfalse
sizeDropdown
新增
显示表格尺寸操作弹框的配置项,具体配置参照组件Dropdown配置object{trigger:['click'], placement:'bottomRight'}
bodyCell个性化单元格v-slot:bodyCell="{text, record, index, column}"-3.0
bordered是否展示外边框和列边框booleanfalse
childrenColumnName指定树形结构的列名stringchildren
columns表格列的配置描述,具体项见下表array-
components覆盖默认的 table 元素object-
customFilterDropdown自定义筛选菜单,需要配合 column.customFilterDropdown 使用v-slot:customFilterDropdown="FilterDropdownProps"-3.0
customFilterIcon自定义筛选图标v-slot:customFilterIcon="{filtered, column}"-3.0
customHeaderRow设置头部行属性Function(columns, index)-
customRow设置行属性Function(record, index)-
dataSource数据数组object[]
defaultExpandAllRows初始时,是否展开所有行booleanfalse
defaultExpandedRowKeys默认展开的行string[]-
emptyText自定义空数据时的显示内容v-slot:emptyText-3.0
expandedRowKeys(v-model)展开的行,控制属性string[]-
expandedRowRender额外的展开行Function(record, index, indent, expanded):VNode | v-slot:expandedRowRender="{record, index, indent, expanded}"-
expandFixed控制展开图标是否固定,可选 true left rightboolean | stringfalse3.0
expandIcon自定义展开图标Function(props):VNode | v-slot:expandIcon="props"-
expandRowByClick通过点击行来展开子行booleanfalse
footer表格尾部Function(currentPageData)|v-slot:footer="currentPageData"
getPopupContainer设置表格内各类浮层的渲染节点,如筛选菜单(triggerNode) => HTMLElement() => TableHtmlElement1.5.0
headerCell个性化头部单元格v-slot:headerCell="{title, column}"-3.0
indentSize展示树形数据时,每层缩进的宽度,以 px 为单位number15
loading页面是否加载中boolean|objectfalse
locale默认文案设置,目前包括排序、过滤、空数据文案objectfilterConfirm: 确定
filterReset: 重置
emptyText: 暂无数据
pagination分页器,参考配置项pagination文档,设为 false 时不展示和进行分页object
rowClassName表格行的类名Function(record, index):string-
rowExpandable设置是否允许行展开(record) => boolean-3.0
rowKey表格行 key 的取值,可以是字符串或一个函数string|Function(record):string'key'
rowSelection列表项是否可选择,配置项objectnull
scroll表格是否可滚动,也可以指定滚动区域的宽、高,配置项object-
showExpandColumn设置是否展示行展开列booleantrue3.0
showHeader是否显示表头booleantrue
showSorterTooltip表头是否显示下一次排序的 tooltip 提示。当参数类型为对象时,将被设置为 Tooltip 的属性boolean | Tooltip propstrue3.0
size表格大小default | middle | smallmiddle
sortDirections支持的排序方式,取值为 ascend descendArray[ascend, descend]
sticky设置粘性头部和滚动条boolean | {offsetHeader?: number, offsetScroll?: number, getContainer?: () => HTMLElement}-3.0
summary总结栏v-slot:summary-3.0
tableLayout表格元素的 table-layoutopen in new window 属性,设为 fixed 表示内容不会影响列的布局- | 'auto' | 'fixed'
固定表头/列或使用了 column.ellipsis 时,默认值为 fixed
1.5.0
title表格标题Function(currentPageData)|v-slot:title="currentPageData"
transformCellText数据渲染前可以再次改变,一般用于空数据的默认配置,可以通过 ConfigProvider 全局统一配置Function({ text, column, record, index }) => any,此处的 text 是经过其它定义单元格 api 处理后的数据,有可能是 VNode | string | number 类型-1.5.4

事件

事件名称说明回调参数
change分页、排序、筛选变化时触发Function(pagination, filters, sorter, { currentDataSource })
expand点击展开图标时触发Function(expanded, record)
expandedRowsChange展开的行变化时触发Function(expandedRows)
resizeColumn拖动列时触发Function(width, column)

customRow 用法

适用于 customRow customHeaderRow customCell customHeaderCell。遵循Vue jsxopen in new window语法。

<Table
  customRow={(record) => {
    return {
      xxx... //属性
      onClick: (event) => {},       // 点击行
      onDblclick: (event) => {},
      onContextmenu: (event) => {},
      onMouseenter: (event) => {},  // 鼠标移入行
      onMouseleave: (event) => {}
    };
  }}
  customHeaderRow={(columns, index) => {
    return {
      onClick: () => {},        // 点击表头行
    };
  }}
/>

Column

列描述数据对象,是 columns 中的一项,Column 使用相同的 API。

参数说明类型默认值版本
align设置列的对齐方式left | right | centerleft
colSpan表头列合并,设置为 0 时,不渲染number
customCell设置单元格属性Function(record, rowIndex, column)-column add from 3.0
customFilterDropdown启用 v-slot:customFilterDropdown,优先级低于 filterDropdownbooleanfalse3.0
customHeaderCell设置头部单元格属性Function(column)-
customRender生成复杂数据的渲染函数,参数分别为当前行的值,当前行数据,行索引Function({text, record, index, column}) {}-
dataIndex列数据在数据项中对应的路径,支持通过数组查询嵌套路径string | string[]-
defaultFilteredValue默认筛选值string[]-1.5.0
defaultSortOrder默认排序顺序ascend | descend-
ellipsis超过宽度将自动省略,暂不支持和排序筛选一起使用。
设置为 true{ showTitle?: boolean } 时,表格布局将变成 tableLayout="fixed"
boolean | { showTitle?: boolean }false3.0
filterDropdown可以自定义筛选菜单,此函数只负责渲染图层,需要自行编写各种交互VNode-
filterDropdownVisible用于控制自定义筛选菜单是否可见boolean-
filtered标识数据是否经过过滤,筛选图标会高亮booleanfalse
filteredValue筛选的受控属性,外界可用此控制列的筛选状态,值为已筛选的 value 数组string[]-
filterIcon自定义 filter 图标。VNode | ({filtered: boolean, column: Column}) => vNodefalse
filterMode指定筛选菜单的用户界面'menu' | 'tree''menu'3.0
filterMultiple是否多选booleantrue
filters表头的筛选菜单项object[]-
filterSearch筛选菜单项是否可搜索Booleanfalse3.0
fixed列是否固定,可选 true(等效于 left) 'left' 'right'boolean|stringfalse
keyVue 需要的 key,如果已经设置了唯一的 dataIndex,可以忽略这个属性string-
maxWidth拖动列最大宽度,会受到表格自动调整分配宽度影响number-3.0
minWidth拖动列最小宽度,会受到表格自动调整分配宽度影响number503.0
resizable是否可拖动调整宽度,此时 width 必须是 number 类型boolean-3.0
responsive响应式 breakpoint 配置列表。未设置则始终可见。Breakpoint[]-3.0
showSorterTooltip表头显示下一次排序的 tooltip 提示, 覆盖 table 中 showSorterTooltipboolean | Tooltip propstrue
sortDirections支持的排序方式,取值为 'ascend' 'descend'Array['ascend', 'descend']1.5.0
sorter排序函数,本地排序使用一个函数(参考 Array.sortopen in new window 的 compareFunction),需要服务端排序可设为 trueFunction|boolean-
sortOrder排序的受控属性,外界可用此控制列的排序,可设置为 'ascend' 'descend' falseboolean|string-
title列头显示文字string-
width列宽度string|number-
onFilter本地模式下,确定筛选的运行函数, 使用 template 或 jsx 时作为filter事件使用Function-
onFilterDropdownVisibleChange自定义筛选菜单可见变化时调用,使用 template 或 jsx 时作为filterDropdownVisibleChange事件使用function(visible) {}-

Breakpoint

type Breakpoint = 'xxxl' | 'xxl' | 'xl' | 'lg' | 'md' | 'sm' | 'xs';

ColumnGroup

参数说明类型默认值
title列头显示文字string|slot-

pagination

分页的配置项。

参数说明类型默认值
position指定分页显示的位置, 取值为topLeft | topCenter | topRight |bottomLeft | bottomCenter | bottomRightArray[bottomRight]

更多配置项,请查看 Pagination

rowSelection

选择功能的配置。

参数说明类型默认值版本
checkStrictlycheckable 状态下节点选择完全受控(父子数据选中状态不再关联)booleantrue3.0
columnTitle自定义列表选择框标题string|VNode-
columnWidth自定义列表选择框宽度string|number-
fixed把选择框列固定在左边boolean-
getCheckboxProps选择框的默认属性配置Function(record)-
hideDefaultSelections去掉『全选』『反选』两个默认选项booleanfalse
hideSelectAll隐藏全选勾选框与自定义选择项booleanfalse3.0
preserveSelectedRowKeys当数据被删除时仍然保留选项的 keyboolean-3.0
selectedRowKeys指定选中项的 key 数组,需要和 onChange 进行配合string[][]
selections自定义选择项 配置项, 设为 true 时使用默认选择项object[] | booleantrue
type多选/单选,checkbox or radiostringcheckbox
onChange选中项发生变化时的回调Function(selectedRowKeys, selectedRows)-
onSelect用户手动选择/取消选择某列的回调Function(record, selected, selectedRows, nativeEvent)-
onSelectAll用户手动选择/取消选择所有列的回调Function(selected, selectedRows, changeRows)-
onSelectInvert用户手动选择反选的回调Function(selectedRows)-
onSelectNone用户清空选择的回调function()-3.0

scroll

参数说明类型默认值
scrollToFirstRowOnChange当分页、排序、筛选变化后是否滚动到表格顶部boolean-
x设置横向滚动,也可用于指定滚动区域的宽,可以设置为像素值,百分比,true 和 'max-content'open in new windowstring | number | true-
y设置纵向滚动,也可用于指定滚动区域的高,可以设置为像素值string | number-

selection

自定义选择配置项

参数说明类型默认值
keyVue 需要的 key,建议设置string-
text选择项显示的文字string|VNode-
onSelect选择项点击回调Function(changeableRowKeys)-

FilterDropdownProps

interface FilterDropdownProps {
  prefixCls: string;
  setSelectedKeys: (selectedKeys: Key[]) => void;
  selectedKeys: Key[];
  confirm: (param?: FilterConfirmProps) => void;
  clearFilters?: () => void;
  filters?: ColumnFilterItem[];
  visible: boolean;
  column: ColumnType;
}

FAQ

设置了sticky属性,会发现table出现双滚动条怎么办?

这种情况需要找到当前滚动的容器(overflow: auto|scroll)的元素,使用getContainer设置该元素,就可以解决该问题。
示例参照:可操作

上次编辑于:
贡献者: Xiou.Wang,Noah.Su,luke