//rowKey是一个函数 赞 踩 这里涉及到作用域插槽的解构赋值、别名,详见这里。 在Table中, 测试验证下,效果如下图所示,ok。 Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。
antd Table组件的使用
loading
:loading
,页面是否加载中,置为false
时不显示加载中。pagination
:pagination
是分页器,置为false
时不展示和进行分页,但通常情况下,pigination
是一个对象,包含current
、total
、pageSize
等属性。{
current:1,
pageSize:10,
total:100
}
slots
<template #cover="{text:cover}">
</template>
change事件
@change
,分页、排序、筛选时会触发change事件,回调函数如下,
Function(pagination, filters, sorter, { currentDataSource })
,
其中,第一个参数是pagination对象,包括current
、pageSize
和total
属性。key
:data-source
与:dataSource
等效,:row-key
与rowKey
等效。
:row-key
,表格行key
的取值,可以是一个字符串,也可以是一个函数。//rowKey是一个字符串
<a-table :columns="columns" :row-key="uid"></a-table>
//rowKey是一个函数
<a-table :columns="columns" :row-key="record => record.uid"></a-table>
dataSource
和columns
里的数据都需要指定key
值。对于dataSource
默认将每列数据的key
作为唯一标识。
如果数据中没有这个属性,务必使用rowKey
来指定数据列的主键。如果没有指定,控制台会出现缺少key
的提示,表格组件也会出现各类奇怪的错误。
举个例子吧。<template>
<a-table :columns="columns" :data-source="data"/>
</template>
<script lang="ts">
import { defineComponent } from 'vue';
const columns = [
{
title: 'Name',
dataIndex: 'name',
key: 'name',
},
{
title: 'Age',
dataIndex: 'age',
key: 'age',
width: '12%',
},
{
title: 'Address',
dataIndex: 'address',
width: '30%',
key: 'address',
},
];
interface DataItem {
name: string;
age: number;
address: string;
children?: DataItem[];
}
const data: DataItem[] = [
{
name: 'John Brown sr.',
age: 60,
address: 'New York No. 1 Lake Park',
children: [
{
name: 'John Brown',
age: 42,
address: 'New York No. 2 Lake Park',
},
{
name: 'John Brown jr.',
age: 30,
address: 'New York No. 3 Lake Park',
children: [
{
name: 'Jimmy Brown',
age: 16,
address: 'New York No. 3 Lake Park',
},
],
},
{
name: 'Jim Green sr.',
age: 72,
address: 'London No. 1 Lake Park',
children: [
{
name: 'Jim Green',
age: 42,
address: 'London No. 2 Lake Park',
children: [
{
name: 'Jim Green jr.',
age: 25,
address: 'London No. 3 Lake Park',
},
{
name: 'Jimmy Green sr.',
age: 18,
address: 'London No. 4 Lake Park',
},
],
},
],
},
],
},
{
name: 'Joe Black',
age: 32,
address: 'Sidney No. 1 Lake Park',
},
];
export default defineComponent({
setup() {
return {
data,
columns
};
},
});
</script>
看到奇怪的地方么?点击收起第三级时,却将所有级别收起。
解决方法有二,dataSource
和columns
的每条数据增加key。
<template>
<a-table :columns="columns" :data-source="data" />
</template>
<script lang="ts">
import { defineComponent } from 'vue';
const columns = [
{
title: 'Name',
dataIndex: 'name',
key: 'name',
},
{
title: 'Age',
dataIndex: 'age',
key: 'age',
width: '12%',
},
{
title: 'Address',
dataIndex: 'address',
width: '30%',
key: 'address',
},
];
interface DataItem {
key: number;
name: string;
age: number;
address: string;
children?: DataItem[];
}
const data: DataItem[] = [
{
key: 1,
name: 'John Brown sr.',
age: 60,
address: 'New York No. 1 Lake Park',
children: [
{
key: 11,
name: 'John Brown',
age: 42,
address: 'New York No. 2 Lake Park',
},
{
key: 12,
name: 'John Brown jr.',
age: 30,
address: 'New York No. 3 Lake Park',
children: [
{
key: 121,
name: 'Jimmy Brown',
age: 16,
address: 'New York No. 3 Lake Park',
},
],
},
{
key: 13,
name: 'Jim Green sr.',
age: 72,
address: 'London No. 1 Lake Park',
children: [
{
key: 131,
name: 'Jim Green',
age: 42,
address: 'London No. 2 Lake Park',
children: [
{
key: 1311,
name: 'Jim Green jr.',
age: 25,
address: 'London No. 3 Lake Park',
},
{
key: 1312,
name: 'Jimmy Green sr.',
age: 18,
address: 'London No. 4 Lake Park',
},
],
},
],
},
],
},
{
key: 2,
name: 'Joe Black',
age: 32,
address: 'Sidney No. 1 Lake Park',
},
];
export default defineComponent({
setup() {
return {
data,
columns
};
},
});
</script>
:row-key="record => record.name"
。
<template>
<a-table :columns="columns" :data-source="data" :rowey="record => record.name" />
</template>
<script lang="ts">
import { defineComponent } from 'vue';
const columns = [
{
title: 'Name',
dataIndex: 'name',
key: 'name',
},
{
title: 'Age',
dataIndex: 'age',
key: 'age',
width: '12%',
},
{
title: 'Address',
dataIndex: 'address',
width: '30%',
key: 'address',
},
];
interface DataItem {
name: string;
age: number;
address: string;
children?: DataItem[];
}
const data: DataItem[] = [
{
name: 'John Brown sr.',
age: 60,
address: 'New York No. 1 Lake Park',
children: [
{
name: 'John Brown',
age: 42,
address: 'New York No. 2 Lake Park',
},
{
name: 'John Brown jr.',
age: 30,
address: 'New York No. 3 Lake Park',
children: [
{
name: 'Jimmy Brown',
age: 16,
address: 'New York No. 3 Lake Park',
},
],
},
{
name: 'Jim Green sr.',
age: 72,
address: 'London No. 1 Lake Park',
children: [
{
name: 'Jim Green',
age: 42,
address: 'London No. 2 Lake Park',
children: [
{
name: 'Jim Green jr.',
age: 25,
address: 'London No. 3 Lake Park',
},
{
name: 'Jimmy Green sr.',
age: 18,
address: 'London No. 4 Lake Park',
},
],
},
],
},
],
},
{
name: 'Joe Black',
age: 32,
address: 'Sidney No. 1 Lake Park',
},
];
export default defineComponent({
setup() {
return {
data,
columns
};
},
});
</script>