在Ant Design Vue中提供table组件供我们使用
这是一个基础表格的组件,他的源代码是:
<template>
<a-table :columns="columns" :data-source="data">
<template #name="{ text }">
<a>{{ text }}</a>
</template>
<template #customTitle>
<span>
<smile-outlined />
Name
</span>
</template>
<template #tags="{ text: tags }">
<span>
<a-tag
v-for="tag in tags"
:key="tag"
:color="tag === 'loser' ? 'volcano' : tag.length > 5 ? 'geekblue' : 'green'"
>
{{ tag.toUpperCase() }}
</a-tag>
</span>
</template>
<template #action="{ record }">
<span>
<a>Invite 一 {{ record.name }}</a>
<a-divider type="vertical" />
<a>Delete</a>
<a-divider type="vertical" />
<a class="ant-dropdown-link">
More actions
<down-outlined />
</a>
</span>
</template>
</a-table>
</template>
<script lang="ts" setup>
import { SmileOutlined, DownOutlined } from '@ant-design/icons-vue';
const columns = [
{
dataIndex: 'name',
key: 'name',
slots: { title: 'customTitle', customRender: 'name' },
},
{
title: 'Age',
dataIndex: 'age',
key: 'age',
},
{
title: 'Address',
dataIndex: 'address',
key: 'address',
},
{
title: 'Tags',
key: 'tags',
dataIndex: 'tags',
slots: { customRender: 'tags' },
},
{
title: 'Action',
key: 'action',
slots: { customRender: 'action' },
},
];
const data = [
{
key: '1',
name: 'John Brown',
age: 32,
address: 'New York No. 1 Lake Park',
tags: ['nice', 'developer'],
},
{
key: '2',
name: 'Jim Green',
age: 42,
address: 'London No. 1 Lake Park',
tags: ['loser'],
},
{
key: '3',
name: 'Joe Black',
age: 32,
address: 'Sidney No. 1 Lake Park',
tags: ['cool', 'teacher'],
},
];
</script>
在此代码中<a-table>标签中的:data-source是数据源,需要定义一个数据集合来作为数据源,
:colums是对列的定义,我们也需要定义一个对象的集合去配置列,类似代码中title表示此表格的列标题,dataindex表示数据的来源,需对应:data-source中的所需展示的数据的名称,除此之外,colums的配置还支持插槽,可以将我们所需的功能自己添加上去
<template #action="{ record,index }" >
<a-button type="primary" @click="handleActionminone(record,index)" style="margin-left: 8px">-1</a-button>
<a-button type="primary" @click="handleActionminthree(record,index)" style="margin-left: 8px">-3</a-button>
<a-button type="primary" @click="handleActionplusone(record,index)" style="margin-left: 8px; " :disabled="record.age>=100">+1</a-button>
<a-button type="primary" @click="handleActionplusthree(record,index)" style="margin-left: 8px" :disabled="record.age>=97">+3</a-button>
<a-button type="primary" v-if="record.reform==false" style="margin-left: 8px" @click="handleActionreform(record,index)">关店整改</a-button>
<a-button type="primary" v-else danger style="margin-left: 8px" @click="handleActioncancel(record,index)">取消整改</a-button>
<a-button type="primary" @click="showModal(record,index)" style="margin-left: 8px" > 删除此号</a-button>
</template>