Skip to content

Table - 表格

Table 在 Antd Table 上进行了一层封装,支持了一些预设,并且封装了一些行为。 Table 是为了解决项目中需要写很多 table 的样板代码的问题,所以在其中做了封装了很多常用的逻辑。 依托于 Form 的能力,Table 的搜索变得简单。request 是 Table 最重要的 API,request 会接收一个对象。对象中必须要有 data。 如果需要手动分页 total 也是必需的。 request 会接管 loading 的设置,同时在查询表单查询时和 params 参数发生修改时重新执行。 查询表单的值和 params 参数也会带入。


  • 不需要请求数据的话,可以直接填入参数 dataSource。
  • 表头 Columns title 默认使用字符串,Dom 形式可能影响到设置栏的“列设置”,或者使用 v-slot:headerCell="{title, column}" 自定义 Dom 结构。
  • 值得注意的是 sorter、filters 的变化不会触发请求,需要手动请求。

基本用法

显示代码
jsx
import { defineComponent } from 'vue'
import { Table, Action } from '@/components/table'

export default defineComponent({
    setup () {
        const columns = [
            {
                title: 'Name',
                search: true,
                initialValue: '123',
                dataIndex: 'name',
                formItemProps: {
                    required: true
                }
            },
            {
                title: 'Age',
                search: true,
                dataIndex: 'age',
                valueType: 'select',
                valueEnum: {
                    '1': '选项一',
                    '2': '选项二'
                },
            },
            {
                title: 'Address',
                search: true,
                dataIndex: 'address'
            },
            {
                title: 'Action',
                customRender: () => {
                    return (
                        <Action.Group>
                            <Action>操作</Action>
                        </Action.Group>
                    )
                }
            }
        ]

        function request (params, paginate, filter, sort) {
            return new Promise((resolve) => {
                console.log(params)

                const data = [
                    {
                        key: '1',
                        name: 'John Brown',
                        age: 32,
                        address: 'New York No. 1 Lake Park',
                    },
                    {
                        key: '2',
                        name: 'Jim Green',
                        age: 42,
                        address: 'London No. 1 Lake Park',
                    }
                ]

                setTimeout(() => {
                    resolve({ data: data })
                }, 1000)
            })
        }

        return () => {
            const tableProps = {
                columns: columns,
                request: request,
                search: { showCollapse: true }
            }

            return (
                <Table {...tableProps}/>
            )
        }
    }
})
显示代码
jsx
import { defineComponent } from 'vue'
import { Table } from '@/components/table'

export default defineComponent({
    setup () {
        const columns = [
            {
                title: 'Name',
                dataIndex: 'name'
            },
            {
                title: 'Age',
                dataIndex: 'age'
            },
            {
                title: 'Address',
                dataIndex: 'address'
            }
        ]

        function request (params, paginate, filter, sort) {
            return new Promise((resolve) => {
                console.log(params)

                const data = [
                    {
                        key: '1',
                        name: 'John Brown',
                        age: 32,
                        address: 'New York No. 1 Lake Park',
                    },
                    {
                        key: '2',
                        name: 'Jim Green',
                        age: 42,
                        address: 'London No. 1 Lake Park',
                    }
                ]

                setTimeout(() => {
                    resolve({ data: data })
                }, 1000)
            })
        }

        return () => {
            const tableProps = {
                search: false,
                columns: columns,
                request: request
            }

            return (
                <Table {...tableProps}/>
            )
        }
    }
})

隐藏工具栏

显示代码
jsx
import { defineComponent } from 'vue'
import { Table } from '@/components/table'

export default defineComponent({
    setup () {
        const columns = [
            {
                title: 'Name',
                search: true,
                initialValue: '123',
                dataIndex: 'name'
            },
            {
                title: 'Age',
                search: true,
                dataIndex: 'age',
                valueType: 'select',
                valueEnum: {
                    '1': '选项一',
                    '2': '选项二'
                },
            },
            {
                title: 'Address',
                dataIndex: 'address'
            },
        ]

        function request (params, paginate, filter, sort) {
            return new Promise((resolve) => {
                console.log(params)

                const data = [
                    {
                        key: '1',
                        name: 'John Brown',
                        age: 32,
                        address: 'New York No. 1 Lake Park',
                    },
                    {
                        key: '2',
                        name: 'Jim Green',
                        age: 42,
                        address: 'London No. 1 Lake Park',
                    }
                ]

                setTimeout(() => {
                    resolve({ data: data })
                }, 1000)
            })
        }

        return () => {
            const tableProps = {
                toolbar: false,
                columns: columns,
                request: request
            }

            return (
                <Table {...tableProps}/>
            )
        }
    }
})

显示扩展栏

显示代码
jsx
import { defineComponent } from 'vue'
import { Card } from 'ant-design-vue'
import { Table, Action } from '@/components/table'
import { default as Descs } from '@/components/descriptions'

export default defineComponent({
    setup () {
        const columns = [
            {
                title: 'Name',
                search: true,
                initialValue: '123',
                dataIndex: 'name',
                formItemProps: {
                    required: true
                }
            },
            {
                title: 'Age',
                search: true,
                dataIndex: 'age',
                valueType: 'select',
                valueEnum: {
                    '1': '选项一',
                    '2': '选项二'
                },
            },
            {
                title: 'Address',
                search: true,
                dataIndex: 'address'
            },
            {
                title: 'Action',
                customRender: () => {
                    return (
                        <Action.Group>
                            <Action>操作</Action>
                        </Action.Group>
                    )
                }
            }
        ]

        function request (params, paginate, filter, sort) {
            return new Promise((resolve) => {
                console.log(params)

                const data = [
                    {
                        key: '1',
                        name: 'John Brown',
                        age: 32,
                        address: 'New York No. 1 Lake Park',
                    },
                    {
                        key: '2',
                        name: 'Jim Green',
                        age: 42,
                        address: 'London No. 1 Lake Park',
                    }
                ]

                setTimeout(() => {
                    resolve({ data: data })
                }, 1000)
            })
        }

        return () => {
            const tableProps = {
                columns: columns,
                request: request,
                search: { showCollapse: true }
            }

            return (
                <Table {...tableProps} v-slots={{
                    extra: ({ pageData }) => {
                        return (
                            <Card bodyStyle={{ padding: '24px 24px 17px' }}>
                                <Descs size={'small'} column={3}>
                                    <Descs.Item label={'Row'}>{pageData.length}</Descs.Item>
                                    <Descs.Item label={'Created'}>Cole</Descs.Item>
                                    <Descs.Item label={'Association'}>
                                        <a>Dublin No. 2 Lake Park</a>
                                    </Descs.Item>
                                    <Descs.Item label={'Creation Time'}>
                                        2024-04-09
                                    </Descs.Item>
                                    <Descs.Item label={'Effective Time'}>
                                        2024-04-09
                                    </Descs.Item>
                                </Descs>
                            </Card>
                        )
                    }
                }}/>
            )
        }
    }
})

可选择的

显示代码
jsx
import { defineComponent } from 'vue'
import { Table } from '@/components/table'

export default defineComponent({
    setup () {
        const columns = [
            {
                title: 'Name',
                search: true,
                initialValue: '123',
                dataIndex: 'name'
            },
            {
                title: 'Age',
                search: true,
                dataIndex: 'age',
                valueType: 'select',
                valueEnum: {
                    '1': '选项一',
                    '2': '选项二'
                },
            },
            {
                title: 'Address',
                dataIndex: 'address'
            }
        ]

        function request (params, paginate, filter, sort) {
            return new Promise((resolve) => {
                console.log(params)

                const data = [
                    {
                        key: '1',
                        name: 'John Brown',
                        age: 32,
                        address: 'New York No. 1 Lake Park',
                    },
                    {
                        key: '2',
                        name: 'Jim Green',
                        age: 42,
                        address: 'London No. 1 Lake Park',
                    }
                ]

                setTimeout(() => {
                    resolve({ data: data })
                }, 1000)
            })
        }

        return () => {
            const tableProps = {
                columns: columns,
                request: request,
                rowSelection: true
            }

            return (
                <Table {...tableProps} v-slots={{
                    alertOptions: ({ keys, rows, cleanSelected }) => {
                        return <a>批量操作</a>
                    }
                }}/>
            )
        }
    }
})
显示代码
jsx
import { defineComponent } from 'vue'
import { Select, Text } from '@/components/form'
import { Action, BaseSearch, Table } from '@/components/table'

export default defineComponent({
    setup () {
        const columns = [
            {
                title: 'Name',
                dataIndex: 'name'
            },
            {
                title: 'Text',
                dataIndex: 'text'
            },
            {
                title: 'Address',
                dataIndex: 'address'
            },
            {
                title: 'Action',
                customRender: () => {
                    return (
                        <Action.Group>
                            <Action>操作</Action>
                        </Action.Group>
                    )
                }
            }
        ]

        function request (params, paginate, filter, sort) {
            return new Promise((resolve) => {
                console.log(params)

                const data = [
                    {
                        key: '1',
                        name: 'John Brown',
                        text: '1',
                        address: 'New York No. 1 Lake Park',
                    },
                    {
                        key: '2',
                        name: 'Jim Green',
                        text: '2',
                        address: 'London No. 1 Lake Park',
                    }
                ]

                setTimeout(() => {
                    resolve({ data: data })
                }, 1000)
            })
        }

        return () => {
            const tableProps = {
                columns: columns,
                request: request
            }

            return (
                <Table {...tableProps} v-slots={{
                    search: (searchProps) => (
                        <BaseSearch {...searchProps}>
                            <Select
                                label={'Age'}
                                name={['data', 'age']}
                                valueEnum={{
                                    '1': '选项一',
                                    '2': '选项二',
                                }}
                            />
                            <Text
                                label={'Text'}
                                name={'text'}
                            />
                        </BaseSearch>
                    )
                }}/>
            )
        }
    }
})

API

属性

属性说明类型默认值
title表单 Titlestring | Slot({ loading, pageData, pagination })-
rowSelection行选择object | booleanfalse
scroll滚动设置object{ x: 'max-content' }
emptyText数据为空时的展示string-
search搜索表单, false 为不展示object | Slot(props) | false-
manualRequest是否需要手动触发首次请求booleanfalse
request数据的获取(params, paginate, filter, sort) => Promise.resolve({ data, success, total })-
paramsrequest 的参数 修改之后会触发更新object-
beforeSearchSubmit表单提交前的数据处理(values) => object-
postData对 request 获取的数据进行处理(data, params, paginate, filter, sort) => array-
toolbar是否显示工具栏booleantrue
options设置栏的参数, false 为不展示object | false-
actions工具栏的扩展项, 显示在右侧, 设置栏的左侧({ loading, pageData, pagination }) => VNode | Slot({ loading, pageData, pagination })-
settings自定义设置栏({ loading, pageData, pagination }) => VNode | Slot({ loading, pageData, pagination })-
extraTable 上面的扩展栏({ loading, pageData, pagination }) => VNode | Slot({ loading, pageData, pagination })-
alert自定义的, 选中后操作栏的左侧({ keys, rows, cleanSelected }) => VNode | Slot({ keys, rows, cleanSelected })-
alertOptions选中后操作栏的右侧({ keys, rows, cleanSelected }) => VNode | Slot({ keys, rows, cleanSelected })-

Options

属性说明类型默认值
reload显示刷新按钮booleantrue
export显示导出按钮booleanfalse
density显示大小设置按钮booleantrue
setting自定表头设置按钮booleantrue

事件

事件名称说明回调参数
onChangeTable 数据变化的回调function(paginate, filters, sorter, extra)
onPaginateChange分页变化的回调function(paginate)
onFilterChange过滤的回调function(filter)
onSortChange排序变化的回调function(sort)
onLoadingChangeloading 变化的回调function(value)
onExport点击导出的回调function(params)
onSizeChangesize 变化的回调function(size)
onColumnsChange表头变化的回调function(columns)
onLoad数据请求成功的回调function(data)
onRequestError数据请求失败的回调function(error)
onFinish表单提交回调function(values)
onReset表单重置回调function(params)

方法

名称描述参数
sizesize-
columnscolumns-
reload重置表单 参数为 true 时分页回到 第一页(resetCurrent?: boolean ) => void
getQueryData获取筛选项的数据() => ({ params, paginate, filter, sort })
cleanSelected取消选中() => void

Columns 列定义

名称描述类型默认值
title列头显示文字string-
dataIndex列数据在数据项中对应的路径string-
keydataIndex 的变体string-
customRender渲染函数function(text, record, number, column)-
filters表头的筛选菜单项object[]-
sorter排序函数function | boolean-
onFilter作为 filter 事件使用function-
fixed固定列'left' | 'right'-
width列宽度string | number-
ellipsis自动省略boolean | { showTitle?: boolean }false
copyable可复制的booleanfalse
disable禁用表头的 显示隐藏booleanfalse
search是否为搜索项booleanfalse
hideInTable在 Table 中隐藏booleanfalse
valueType搜索的输入框类型string'text'
initialValue初始值all-
valueEnum选择框的枚举值 同时也根据 value 返回对应项object-
fieldProps输入框的 propsobject-
formItemPropsForm.Item 的 propsobject-

Action.Group

属性说明类型默认值
max子元素超过 max 将生成下拉菜单number2
size间距number8

Action

属性说明类型默认值
type类型'primary' | 'warning' | 'error''primary'