Vue 学习随笔系列十二 -- 表格内容渲染方法

表格内容渲染方法

文章目录

  • 表格内容渲染方法
    • 1、使用 formatter 函数
    • 2、 使用 render 函数
    • 3、使用 template 自定义
    • 4、使用 slot 插槽
    • 5、注意


1、使用 formatter 函数

示例代码1

<el-table-column prop="status" label="状态" align="center" :formatter="scopeIdFormat" width="auto" />methods:{//大类显示对应状态scopeIdFormat(row, column, cellValue){return row.status == '0' ? "开启" : "关闭"}
}

示例代码2

	<el-tablesize="small"borderv-loading="loading":data="tableData"@selection-change="select"><el-table-columntype="selection"width="55"></el-table-column><el-table-columnv-for="(item, index) in columns":key="index"v-bind="item"align="center"><el-table-column></el-table>columns: [{type:"index",label: "序号“,width: "55",align: "center"},{label: "状态",prop: "status",formatter: (value, column) => {     //写上formatter函数return value == '0' ? "开启" : "关闭"},
]

2、 使用 render 函数

	<el-tablesize="small"borderv-loading="loading":data="tableData"@selection-change="select"><el-table-columntype="selection"width="55"></el-table-column><el-table-columnv-for="(item, index) in columns":key="index"v-bind="item"align="center"><el-table-column></el-table>columns: [{type:"index",label: "序号“,width: "55",align: "center"},{key: "name",title: "名称",minWidth: 250,align: "center",render: (h, params) => {return h("div", [h( "a",{props: {type: "primary",size: "small",},on: {click: () => {this.clickRow(params.row); //添加点击事件},},},params.row.name // 展示内容),]);},},{key: "itemNum",title: "总数",minWidth: 150,align: "center",render: (h, params) => {return h("div",this.formaterNum(params.row.itemNum) // 调用数据处理的方法);}},
],methods: {// 点击事件clickRow(row) {console.log(111, row)},// 自定义数据处理方法formaterAmount(data) {if (!data) return '0.00'// 将数据分割,保留两位小数data= data.toFixed(2)// 获取整数部分const intPart = Math.trunc(data)// 整数部分处理,增加,const intPartFormat = intPart.toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1,')// 预定义小数部分let floatPart = '.00'// 将数据分割为小数部分和整数部分const newArr = data.toString().split('.')if (newArr.length === 2) { // 有小数部分floatPart = newArr[1].toString() // 取得小数部分return intPartFormat + '.' + floatPart}return intPartFormat + floatPart}
}

3、使用 template 自定义

<el-tablesize="small"borderv-loading="loading":data="tableData"><el-table-columnv-for="(item, index) in columns":key="index"v-bind="item"align="center"><template slot-scope="scope"><span v-if="item.prop==='status'"><el-imageclass="icon-img":src="'../../start.img"></el-image><span>{{ scope.row[item.prop] }}</span></span><span v-else>{{ scope.row[item.prop] }}</span></template></el-table-column></el-table>

4、使用 slot 插槽

<el-tablesize="small"borderv-loading="loading":data="tableData"><el-table-columnv-for="(item, index) in columns":key="index"v-bind="item"align="center"><template slot-scope="scope" slot="status"><span>{{ scope.row.status == '0' ? "开启" : "关闭"}}</span></template></el-table-column></el-table>columns: [{type:"index",label: "序号“,width: "55",align: "center"},{label: "状态",prop: "status",slot: "status",}
]

5、注意

使用template 自定义时,columns中的序号列显示不出来,需要在代码中重新编写,
例:

<el-tablesize="small"borderv-loading="loading":data="tableData"><el-table-columnlabel="序号"type="index"></el-table-column><el-table-columnv-for="(item, index) in columns":key="index"v-bind="item"align="center"><template slot-scope="scope" slot="status"><span>{{ scope.row.status == '0' ? "开启" : "关闭"}}</span></template></el-table-column></el-table>columns: [// {// 	type:"index",// 	label: "序号“,// 	width: "55",// 	align: "center"// },{label: "状态",prop: "status",slot: "status",}
]

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/590083.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

一文解决IDea中Springboot 热部署:IDEA中Spring Boot应用热部署的几种方式及操作步骤

在快节奏的软件开发过程中&#xff0c;热部署&#xff08;Hot Deployment&#xff09;功能对于提升开发效率至关重要。尤其是在使用Spring Boot框架进行开发时&#xff0c;能够实现代码的即时更新和应用的无需重启&#xff0c;极大地节省了开发时间&#xff0c;提高了迭代速度。…

MFC 获取程序版本信息

在 MFC 应用程序中&#xff0c;可以使用 GetFileVersionInfo 函数来获取程序版本信息。这个函数能够获取一个指定文件的版本信息&#xff0c;并将其存储在一个缓冲区中。然后&#xff0c;可以使用 VerQueryValue 函数来解析版本信息并提取所需的信息。 #include <afxver_.h&…

浅谈分布式光伏电站的运维管理

摘要&#xff1a;随着近些年我国对节能降耗关注力度的持续加大&#xff0c;为满足人们不断增长的电能需求&#xff0c;光伏发电产业得到迅猛发展&#xff0c;其中分布式光伏发电的比重持续增长。在打赢脱贫攻坚战的大背景下&#xff0c;国家电网公司探索出一条“阳光扶贫”的扶…

vue实现导出列表为xlsx文件

1.安装依赖 npm install --save xlsx file-saver 2.引入依赖 import FileSaver from file-saver; import * as XLSX from xlsx; 3.代码实现 <el-button type"primary" click"exportData">导出数据</el-button><el-tableid"table_ex…

IoT数采平台4:测试

IoT数采平台1&#xff1a;开篇IoT数采平台2&#xff1a;文档IoT数采平台3&#xff1a;功能IoT数采平台4&#xff1a;测试 Modbus RTU串口测试 OPC测试 HTTP测试 MQTT透传测试 MQTT网关测试及数据上报 TCP / UDP 监听&#xff0c;客户端连上后发送信息&#xff0c;客户端上报数据…

Making Anti-Palindromes

题目链接 Codeforces Round 867 (Div. 3) E. Making Anti-Palindromes 挺好的一道鸽巢原理题。 思路&#xff1a; 贪心地来想&#xff0c;我们没必要动本来就不同的一对&#xff0c;而对相同的对&#xff0c;我们可以让它们互相之间进行交换&#xff0c;这样一次交换就可以…

java面试题(3)|解释 null 和 “null“ 之间的区别,并举例说明它们在编程中的使用场景

null 和 "null" 之间的区别主要在于语义和数据类型上&#xff1a; null 是一个特殊的值&#xff0c;通常用于表示缺少有效值或未定义的变量。在许多编程语言中&#xff0c;null是一个关键字&#xff0c;表示空值。例如&#xff0c;在Java中&#xff0c;当一个对象尚…

excel统计分析——协方差分析的作用

参考资料&#xff1a;生物统计学 1、协变量与试验因素的区别 如果把协方差分析资料中的协变量看作多因素方差分析资料中的一个因素&#xff0c;则两类资料有相似之处&#xff0c;但两类资料有本质的不同。在方差分析中&#xff0c;各因素的水平时人为控制的&#xff0c;即使是…