(三)ant design vue对(a-table)内部封装进行分析(非分页)
1:#name="{ text }"是v-slot:name="{text}"的简写,name是插槽名称,text是子组件提供的数据(运用了插槽作用域传值),{text:xxyy}还可以这样写,这样下面就要改成<a>{{ xxyy}}</a>了
注意:(#name="{text}"是子组件提供的当前行的当前列的数据,#name="{record}"是子组件提供的当前行的数据 )
<template #name="{ text }">
<a>{{ text }}</a>
</template>
2:title,customRender在<a-table>内部的分析:
title: 'customTitle',当调用<a-table>的时候,<a-table>会根据title是否有值在name列的表头创建名字为customTitle的插槽,供父组件传递对应的该列的表头结构
customRender: 'name',当调用<a-table>的时候,<a-table>会根据customRender是否有值在name列的每一个td里面创建一个名字为name的插槽,并且根据插槽作用域
提供一个text(当前列的值),record(当前行的对象)给父组件,父组件拿到后根据text或者record传入对应结构做渲染
slots{
title: 'customTitle',
customRender: 'name',
}3:定义数据列,呢里的dataIndex必须和3里面的数据行的列名对应上,
{
dataIndex: 'name',
width: 40,
align: 'left',
ellipsis: true ,
slots: {
title: 'customTitle',
customRender: 'name',
},
},
4:返回的数据行
{
name: 'John Brown',
age: 32,
address: 'New York No. 1 Lake Park',
tags: ['nice', 'developer'],
},
4:以下是完整代码示例:
<template>
<a-table :columns="columns" :data-source="data" :pagination="false">
<template #name="{ text }">
<a>{{ text }}</a>
</template>
<template #customTitle>
<span>
<smile-outlined />
Name1
</span>
</template>
<template #tagsy="{ text:xxyy}">
<span>
<a-tag
v-for="tag in xxyy"
: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>
import { SmileOutlined,DownOutlined } from '@ant-design/icons-vue';
const columns = [
{
dataIndex: 'name',
width: 40,
align: 'left',
ellipsis: true ,
slots: {
title: 'customTitle',
customRender: 'name',
},
},
{
title: 'Age',
dataIndex: 'age',
width: 40,
align: 'left',
ellipsis: true ,
slots: {customRender: 'age'}
},
{
title: 'Address',
dataIndex: 'address',
width: 80,
align: 'left',
ellipsis: true ,
},
{
title: 'tags',
dataIndex: 'tags',
width: 80,
align: 'left',
ellipsis: true ,
slots: {
customRender: 'tagsy',
},
},
{
title: 'Action',
key: 'action',
width: 80,
align: 'left',
ellipsis: true ,
slots: {
customRender: 'action',
},
},
];
const data = [
{
name: 'John Brown',
age: 32,
address: 'New York No. 1 Lake Park',
tags: ['nice', 'developer'],
},
{
name: 'Jim Green',
age: 42,
address: 'London No. 1 Lake Park',
tags: ['loser'],
},
{
name: 'Joe Black',
age: 32,
address: 'Sidney No. 1 Lake Park',
tags: ['cool', 'teacher'],
},
];
export default({
name:"WW",
setup() {
return {
data,
columns,
};
},
components: {
SmileOutlined,
DownOutlined
},
});
</script>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现