• Element UI表格将两列数据放在一起显示


    提出问题

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

    代码

    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>Documenttitle>
        <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>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67

    解决方法

    1. 获取每一行的索引
      <template slot-scope="scope">
                  {{scope.$index}}
      template>
      
      • 1
      • 2
      • 3
    2. 放到同一列
       <el-table-column label="姓名" align="center">
           <template slot-scope="scope">
               {{tableData[scope.$index].lastName}}{{tableData[scope.$index].firstName}}
           template>      
       el-table-column>
      
      • 1
      • 2
      • 3
      • 4
      • 5

    解释

    • 标签
      • :data属性:表格绑定的数据
    • 标签
      • label属性:每列的标题
        在这里插入图片描述
    • prop:属性:该列的值在:data中的key
      例如:data=tableData,而prop=lastName,就会逐个取出tableData中的lastName
      如果列的值是自定义的,就必须删除prop属性
      tableData: [
          { lastName: "张", firstName: "三" },
          { lastName: "李", firstName: "四" },
          { lastName: "王", firstName: "五" },
      ]
      
      • 1
      • 2
      • 3
      • 4
      • 5

    代码

    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>Documenttitle>
        <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>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
  • 相关阅读:
    NR 5G RRC Setup Request
    一文带你学会 consul 基本使用和 Docker 部署
    ChatGPT Edu版本来啦:支持GPT-4o、自定义GPT、数据分析等
    R语言缺失时间序列的填充及合并:补齐时间序列数据中所有缺失的时间索引、使用merge函数合并日期补齐之后的时间序列数据和另外一个时间序列数据(补齐左侧数据)
    TCP是什么、UDP是什么,它们有什么区别
    基于Python+Django深度学习的身份证识别考勤系统设计与实现
    Python基础篇(07):高阶函数lambda、zip、map、filter、reduce和函数注解
    安达发|AI算法全方位打造制造业AI智能化工厂的超级大脑
    服务注册中心Eureka
    【python学习】基础篇-常用模块-re模块:正则表达式高效操作字符串
  • 原文地址:https://blog.csdn.net/m0_52733659/article/details/126130902