当前位置: 首页 > news >正文

vue二次封装ant-design-vue的table,识别columns中的自定义插槽

自定义 `Utable.vue` 组件


<script>
import { mapState } from 'vuex'
import { Table } from 'ant-design-vue'
export default {name: 'UTable',props: {dataSource: {type: Array,default: () => []},columns: {type: Array,default: () => []},onClearSelected: {type: Function,default: () => null},positionBottom:{type: Boolean,default: false}},computed: {...mapState({primaryColor: state => state.app.color,}),},methods: {handleTableChange(e){this.$emit('change', e);},getBottomClass(){return ['u-table-selected-info', this.positionBottom?'u-table-selected-info-container-position':'u-table-selected-info-container']}},render() {const on={...this.$listeners}const props = {...this.$attrs,...this.$props}const slots = Object.keys(this.$slots).map(slot => {return (<template slot={slot}>{this.$slots[slot]}</template>)})// columns={columns} dataSource={dataSource}const table=(<Table bordered props={props}  scopedSlots={this.$scopedSlots} on={on}>{slots}</Table>)const { rowSelection } = this.$attrs// 我的版本不支持用这种语法// const selectRoeKeys = (rowSelection?.selectedRowKeys || []).lengthconst selectRoeKeys = (rowSelection ? (rowSelection.selectedRowKeys || []) : []).lengthconst that = this;const onClearSelected = function(arg){return that.$props.onClearSelected ? that.$props.onClearSelected(arguments): null;}return (<div>{ table }<div class={this.getBottomClass()}><a-icon type="info-circle" theme="filled" style={{ color: this.primaryColor }} /> 已选择<a style="font-weight: 600">{selectRoeKeys}</a>项<a style="margin-left: 24px" onClick={onClearSelected}>清空</a></div></div>)}
}
</script><style scoped>
.u-table-selected-info-container{line-height:24px;margin-top: -40px;
}
.u-table-selected-info-container-position{position: absolute;bottom: 40px;height: 24px;line-height:24px;
}
</style>

使用

<template><div><div></div><u-table :columns="columns":loading="loading":data-source="dataSource":ipagination="ipagination":rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }":onClearSelected="onClearSelected"@change="handleChange"><span slot="gender" slot-scope="text, record"><i>{{text=='1' ? '男':'女'}}</i></span><template slot="action" slot-scope="text, record"><a >编辑</a><a-divider type="vertical" /><a >详情</a></template></u-table></div>
</template><script>import UTable from '@/components/UTable/UTable'export default {name: "Alteration",components: {UTable},data() {return {columns: [{title: '#',dataIndex: '',key: 'rowIndex',width: 60,align: 'center',customRender: function (t, r, index) {return parseInt(index) + 1;}},{title: '姓名',dataIndex: 'name',width: 120,},{title: '性别',dataIndex: 'gender',width: 120,scopedSlots: { customRender: 'gender' },},{title: '年龄',dataIndex: 'age',width: 120,},{title: '操作',dataIndex: 'action',align: 'center',width: 120,scopedSlots: {customRender: 'action'}}],loading: false,dataSource: [{name: "一成", gender: "1", age: 12},{name: "二强", gender: "1", age: 6},{name: "三丽", gender: "2", age: 6},{name: "四美", gender: "2", age: 6},{name: "七七", gender: "1", age: 6},],/* 分页参数 */ipagination: {current: 1,pageSize: 5,pageSizeOptions: ['5', '10', '50'],showTotal: (total, range) => {return range[0] + '-' + range[1] + ' 共' + total + '条'},showQuickJumper: true,showSizeChanger: true,total: 0},selectedRowKeys: [],selectionRows: []}},mounted() {},methods: {onSelectChange(selectedRowKeys, selectionRows){this.selectedRowKeys = selectedRowKeys;this.selectionRows = selectionRows;},onClearSelected(){
// 目前还无法清空,但是执行了这个函数。等我找找原因console.log("清空函数",this.selectedRowKeys)this.selectedRowKeys.length = 0;this.selectionRows.length = 0;},handleChange(){}}}
</script><style lang="less" scoped>.steps {max-width: 750px;margin: 16px auto;}
</style>

效果图

操作列、性别列是自定义的slot


文章转载自:

http://NefEoUhr.fmrwL.cn
http://RlbxO6sL.fmrwL.cn
http://Cj0JUhTQ.fmrwL.cn
http://K6noCiAQ.fmrwL.cn
http://8qAGo9pt.fmrwL.cn
http://AkmtvaKd.fmrwL.cn
http://USLHFLgL.fmrwL.cn
http://6HV8HRAh.fmrwL.cn
http://dnijXPzz.fmrwL.cn
http://LLEYlsFD.fmrwL.cn
http://pBXEjyyq.fmrwL.cn
http://vH3Zk0tv.fmrwL.cn
http://10LC5q1W.fmrwL.cn
http://KZxgiJBz.fmrwL.cn
http://VTz78aQE.fmrwL.cn
http://PIjOdpCr.fmrwL.cn
http://UEtdjRKt.fmrwL.cn
http://oXzYeyip.fmrwL.cn
http://qt2o6ej8.fmrwL.cn
http://PHow7bxf.fmrwL.cn
http://xxVX5bqK.fmrwL.cn
http://gIXiSIfO.fmrwL.cn
http://v47lpNEy.fmrwL.cn
http://x75AKf2o.fmrwL.cn
http://A93yzokT.fmrwL.cn
http://kYETL0QQ.fmrwL.cn
http://kRGDP74J.fmrwL.cn
http://3n84ujfK.fmrwL.cn
http://EfN3RJim.fmrwL.cn
http://0boIgk1A.fmrwL.cn
http://www.dtcms.com/a/376256.html

相关文章:

  • vue方法汇总
  • GPU硬件架构和配置的理解
  • C++类和对象初识
  • 笔记:乐鑫 (Espressif) 的生态策略与开发者悖论
  • SELinux策略:域转换与类型继承
  • 【VLMs篇】06:Cosmos-Reason1:从物理常识到具身推理
  • 图漾相机 FM851-E2 相关资料
  • 资产管理什么软件好
  • npm 安装命令中关于 @ 的讲解,如:npm install @vue-office/docx vue-demi
  • PowerBI 没实现的的联动同步下钻,QuickBI 实现了
  • k8s+jenkins+harbor构建Devops平台
  • 【中文教材】35. 证券市场指数
  • 36.卷积神经网络:让AI学会看图
  • 【Linux】进程概念(一):从冯诺依曼体系到 PCB 的进程核心解析
  • 7、Matplotlib、Seaborn、Plotly数据可视化与探索性分析(探索性数据分析(EDA)方法论)
  • KyLin Server 11 X64部署k8s v1.34.0
  • 【Redis】双写一致性及数据持久化
  • UE5全场景应用与核心优势解析 , 川翔云电脑渲染支持
  • 用deepseek对GPU服务器进行压力测试
  • day27|前端框架学习
  • YOLOv8 Linux 部署指南(GPU CPU 完整版)
  • 服务器都是用的iis, 前端部署后报跨域,不是用同一个服务器 是前端项目的服务器做Nginx转发,还是后端项目的服务器做Nginx转发?
  • 43.shell脚本循环与函数
  • 数据整理器(Data Collators)(90)
  • (贪心、数学、二分)洛谷 P9755 CSPS2023 种树 题解
  • 反相放大器电路和T形网络反相放大电路与仿真
  • C++的诗行:一文读懂C++的继承机制
  • ubuntu 22 安装轻量级桌面Xfce并使用xrdp远程桌面连接
  • PixVerse -免费在线AI视频生成工具
  • 赋能数字孪生:Paraverse平行云实时云渲染平台LarkXR,提供强大的API与SDK用于二次开发和深度集成