提出问题

正常表格的每一列是按照数据库中的字段来显示的(有多少个字段就有多少列)
例如数据库中某表有两个字段,,默认情况下是这样显示的
在这里插入图片描述
虽然字段可以分开存储,但更合理的是,一起显示(姓名)
在这里插入图片描述

代码

<!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>

解决方法

  1. 获取每一行的索引
    <template slot-scope="scope">
                {{scope.$index}}
    </template>
    
  2. 放到同一列
     <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>
GitHub 加速计划 / eleme / element
54.06 K
14.63 K
下载
A Vue.js 2.0 UI Toolkit for Web
最近提交(Master分支:1 个月前 )
c345bb45 5 个月前
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 5 个月前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐