Element UI 表格中使用作用域插槽访问数据 | <el-table-column> 中使用 <el-table> 数据
<template #default="scope"> 中添加的 <el-table-column>中使用<el-table>中的数据,可以通过作用域插槽(scoped slot)的方式来实现。
首先,在<el-table-column>中添加一个作用域插槽,指定一个名称,例如slot-scope="scope"。
然后,在作用域插槽中可以通过scope.row来访问当前行的数据,scope.column来访问当前列的配置信息。
以下是一个示例代码:
<template>
<el-table :data="tableData">
<el-table-column label="姓名">
<template slot-scope="scope">
{{ scope.row.name }}
</template>
</el-table-column>
<el-table-column label="年龄">
<template slot-scope="scope">
{{ scope.row.age }}
</template>
</el-table-column>
</el-table>
</template>
<script>
export default {
data() {
return {
tableData: [
{ name: '张三', age: 20 },
{ name: '李四', age: 25 },
{ name: '王五', age: 30 },
]
};
},
};
</script>
在上面的代码中,<el-table>的data属性绑定了一个数组tableData,每个对象都有name和age属性。
在<el-table-column>中,使用作用域插槽获取到当前行的数据,然后通过scope.row.name和scope.row.age来渲染对应的值。
这样就可以在<el-table-column>中使用<el-table>中的数据了。
原文地址: https://www.cveoy.top/t/topic/qeui 著作权归作者所有。请勿转载和采集!