Element UI表格将两列数据放在一起显示
element
A Vue.js 2.0 UI Toolkit for Web
项目地址:https://gitcode.com/gh_mirrors/eleme/element
·
提出问题
正常表格的每一列是按照数据库中的字段来显示的(有多少个字段就有多少列)
例如数据库中某表有两个字段,姓和名,默认情况下是这样显示的
虽然字段可以分开存储,但更合理的是,姓和名一起显示(姓名)
代码
<!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>Document</title>
<style>
.el-table .warning-row {
background: oldlace;
}
.el-table .success-row {
background: #f0f9eb;
}
</style>
</head>
<div id="app">
<!-- 表格 -->
<template>
<el-table :data="tableData" style="width: 100%" :row-class-name="tableRowClassName">
<el-table-column prop="lastName" label="last Name(姓)" align="center">
</el-table-column>
<el-table-column prop="firstName" label="first Name(名)" align="center">
</el-table-column>
</el-table>
</template>
</div>
<body>
<!-- 引入 Vue -->
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<!-- 引入样式 -->
<link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
<!-- 引入组件库 -->
<script src="https://unpkg.com/element-ui/lib/index.js"></script>
<script>
new Vue({
el: "#app",
methods: {
tableRowClassName({ row, rowIndex }) {
if (rowIndex === 1) {
return 'warning-row';
} else if (rowIndex === 3) {
return 'success-row';
}
return '';
}
},
data() {
return {
tableData: [
{ lastName: "张", firstName: "三" },
{ lastName: "李", firstName: "四" },
{ lastName: "王", firstName: "五" },
]
}
}
})
</script>
</body>
</html>
解决方法
- 获取每一行的索引
<template slot-scope="scope"> {{scope.$index}} </template> - 将
姓和名放到同一列<el-table-column label="姓名" align="center"> <template slot-scope="scope"> {{tableData[scope.$index].lastName}}{{tableData[scope.$index].firstName}} </template> </el-table-column>
解释
- 标签
:data属性:表格绑定的数据
<el-table-column>标签label属性:每列的标题
prop:属性:该列的值在:data中的key
例如:data=tableData,而prop=lastName,就会逐个取出tableData中的lastName
如果列的值是自定义的,就必须删除prop属性tableData: [ { lastName: "张", firstName: "三" }, { lastName: "李", firstName: "四" }, { lastName: "王", firstName: "五" }, ]
代码
<!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>Document</title>
<style>
.el-table .warning-row {
background: oldlace;
}
.el-table .success-row {
background: #f0f9eb;
}
</style>
</head>
<div id="app">
<!-- 表格 -->
<template>
<el-table :data="tableData" style="width: 100%" :row-class-name="tableRowClassName">
<el-table-column label="姓名" align="center">
<template slot-scope="scope">
{{tableData[scope.$index].lastName}}{{tableData[scope.$index].firstName}}
</template>
</el-table-column>
</el-table>
</template>
</div>
<body>
<!-- 引入 Vue -->
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<!-- 引入样式 -->
<link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
<!-- 引入组件库 -->
<script src="https://unpkg.com/element-ui/lib/index.js"></script>
<script>
new Vue({
el: "#app",
methods: {
tableRowClassName({ row, rowIndex }) {
if (rowIndex === 1) {
return 'warning-row';
} else if (rowIndex === 3) {
return 'success-row';
}
return '';
}
},
data() {
return {
tableData: [
{ lastName: "张", firstName: "三" },
{ lastName: "李", firstName: "四" },
{ lastName: "王", firstName: "五" },
]
}
}
})
</script>
</body>
</html>
A Vue.js 2.0 UI Toolkit for Web
最近提交(Master分支:4 个月前 )
c345bb45
1 年前
a07f3a59
* Update transition.md
* Update table.md
* Update transition.md
* Update table.md
* Update transition.md
* Update table.md
* Update table.md
* Update transition.md
* Update popover.md 1 年前
AtomGit 是由开放原子开源基金会联合 CSDN 等生态伙伴共同推出的新一代开源与人工智能协作平台。平台坚持“开放、中立、公益”的理念,把代码托管、模型共享、数据集托管、智能体开发体验和算力服务整合在一起,为开发者提供从开发、训练到部署的一站式体验。
更多推荐


所有评论(0)