Element-UI表格数据遍历 <template slot-scope=“scope“>列表渲染

博客介绍了Vue中el-table的数据绑定与渲染。:data绑定数据集,el-table-column用prop绑定列数据,table按数组长度生成行、按el-table-column数量生成列。还提到template的固定用法,以及通过scope可获取row、column等数据用于渲染表格。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在el-table中,:data="tableData"是数据集。每个el-table-column,我们只需要使用prop="date",就可以将该列的数据绑定为该数组所有的对象中的“date”属性,我们可以理解为对于tableData,这里始终取的是tableData[$index].date。

table按照tableData这个数组的长度来生成多少行,按照有多少个el-table-column来生成多少列。

 

tableDate 为返回的json数据

按照我们前面的理解,按照有多少个el-table-column来生成列,因此这里没有使用prop="date",生成的单元格也就是空白的一个单元格。

template(模版) 在这里属于一个固定用法: <template slot-scope="scope">

我们主要说一下这个scope是个什么东西,按照element上的提示:

通过 Scoped slot 可以获取到 row, column, $index 和 store(table 内部的状态管理)的数据

{{scope.row.deptno}} 表示把对应行的数据渲染到表格中1

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>员工信息</title> </head> <link rel="stylesheet" href="element-ui/index.css"> <script src="./js/vue.js"></script> <script src="./element-ui/index.js"></script> <script src="./js/axios-0.18.0.js"></script> <body> <h1 align="center">员工信息列表展示</h1> <div id="app"> <el-table :data="tableData" style="width: 100%" stripe border > <el-table-column prop="name" label="姓名" align="center" min-width="20%"></el-table-column> <el-table-column prop="age" label="年龄" align="center" min-width="20%"></el-table-column> <el-table-column label="图像" align="center" min-width="20%"> <template slot-scope="scope"> <el-image :src="scope.row.image" style="width: 80px; height: 50px;"></el-image> </template> </el-table-column> <el-table-column prop="gender" label="性别" align="center" min-width="20%"></el-table-column> <el-table-column prop="job" label="职位" align="center" min-width="20%"></el-table-column> </el-table> </div> </body> <style> .el-table .warning-row { background: oldlace; } .el-table .success-row { background: #f0f9eb; } </style> <script> new Vue({ el: "#app", data() { return { tableData: [] } }, mounted(){ axios.get('/listEmp').then(res=>{ if(res.data.code){ this.tableData = res.data.data; } }); }, methods: { } }); </script> </html> 帮我追加两条入职时间和更新时间
最新发布
06-24
const HeadItemData = ref([ { byteNo: 1, len: 1, name: '包头1', dataType: '2', range: '0-255', itemApp: '1', verifyType: '', default: '21930', //55AA startRange: '', endRange: '' }, { byteNo: 2, len: 1, name: '包头2', dataType: '2', range: '0-255', itemApp: '1', verifyType: '', default: '21930', startRange: '', endRange: '' }, { byteNo: 3, len: 1, name: '目标地址', dataType: '2', range: '0-255', itemApp: '3', verifyType: '', default: '21930', startRange: '', endRange: '' }, { byteNo: 4, len: 1, name: '源地址1', dataType: '2', range: '0-255', itemApp: '2', verifyType: '', default: '21930', startRange: '', endRange: '' }])<el-table :data="HeadItemData" style="width: 100%" max-height="400" >> <el-table-column label="字节序号" width="100" prop="byteNo" ></el-table-column> <el-table-column label="字节数" width="100" prop="len" ></el-table-column> <el-table-column label="内容" width="150" prop="name"> <template v-slot="scope"> <el-input v-model="scope.row.name" placeholder="请输入内容" ></el-input> </template ></el-table-column> <el-table-column label="数据类型" width="120" prop="dataType"> <template v-slot="scope"> <!-- 数据区不选择数据类型 --> <el-select v-model="scope.row.dataType" placeholder="请选择数据类型" :disabled="scope.row.itemApp === '8'" > <el-option label="int8" value="1"></el-option> <el-option label="uint8" value="2"></el-option> <el-option label="int16" value="3"></el-option> <el-option label="uint16" value="4"></el-option> <el-option label="int32" value="5"></el-option> <el-option label="uint32" value="6"></el-option> </el-select> </template ></el-table-column> <el-table-column label="范围" width="210" prop="range" ><template v-slot="scope"> <div class="range-inputs"> <el-input v-model="scope.row.startRange" placeholder="起始值" clearable style="width: 80px" ></el-input> <span style="margin: 0 5px">-</span> <el-input v-model="scope.row.endRange" placeholder="结束值" clearable style="width: 80px" ></el-input></div></template ></el-table-column> <el-table-column label="数据项应用类型" width="180" prop="itemApp"> <template v-slot="scope"> <el-select v-model="scope.row.itemApp" :disabled=" scope.$index === 0 || scope.$index === HeadItemData.length - 1 " placeholder="请选择数据项应用类型" @change="handleAppTypeChange(scope.row, scope.value)" > <el-option v-for="opt in getOptions(scope.$index)" :key="opt.value" :label="opt.label" :value="opt.value" /> </el-select> </template ></el-table-column></el-table> 让数据项应用类型一样的行,数据类型和范围必须一样
03-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值