提出问题
正常表格的每一列是按照数据库中的字段来显示的(有多少个字段就有多少列) 例如数据库中某表有两个字段,姓 和名 ,默认情况下是这样显示的 虽然字段可以分开存储,但更合理的是,姓 和名 一起显示(姓名)
代码
<!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>
<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>
解释
- 标签
<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>
<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>
|