Vue 实现element 的 el-table 的表格类的样式绑定

<template>
// 遍历表格
    <div class="dormitoryData">
      <el-table
        ref="dormitoryTable"
        :data="dormitory"
        tooltip-effect="dark"
        stripe
        style="width: 100%">
        <el-table-column type="selection" width="45"></el-table-column>
        <el-table-column label="序号"  type="index" width="65"></el-table-column>
        <el-table-column label="人物" prop="people">
          <template slot-scope="scope">
            <div :class="scope.row.isEdit==0? 'one' :''"> {{ scope.row.people }}</div>
          </template>
        </el-table-column>
        <el-table-column label="关系" prop="relationship">
          <template slot-scope="scope">
            <div :class="scope.row.isEdit==0? 'one' :''"> {{ scope.row.relationship }}</div>
          </template>
        </el-table-column>
        <el-table-column label="初见" prop="meet">
          <template slot-scope="scope">
            <div :class="scope.row.isEdit==0? 'one' :''"> {{ scope.row.meet }}</div>
          </template>
        </el-table-column>
        <el-table-column label="地点" prop="place">
          <template slot-scope="scope">
            <div :class="scope.row.isEdit==0? 'one' :''"> {{ scope.row.place }}</div>
          </template>
        </el-table-column>
        <el-table-column label="昵称" prop="execg">
          <template slot-scope="scope">
            <div :class="scope.row.isEdit==0? 'one' :''"> {{ scope.row.execg }}</div>
          </template>
        </el-table-column>
        <el-table-column label="认识年限" prop="year">
          <template slot-scope="scope">
            <div :class="scope.row.isEdit==0? 'one' :''"> {{ scope.row.year }}</div>
          </template>
        </el-table-column>
        <el-table-column label="成名之作" prop="works">
          <template slot-scope="scope">
            <div :class="scope.row.isEdit==0? 'one' :''"> {{ scope.row.works }}</div>
          </template>
        </el-table-column>
      </el-table>
    </div>
</template>
<script>
 
  export default {
   
    data () {
      return {
        dormitory: [{
          people: '雷森',
          relationship: '大学室友',
          meet: '2010-09-02',
          place: '图书馆',
          execg: '胖子',
          year: '8年',
          works: '海阔天空',
          isEdit: '1'
        }, {
          people: '刘利伟',
          relationship: '大学室友',
          meet: '2010-09-02',
          place: '5#633',
          execg: '老大',
          year: '8年',
          works: '勇气',
          isEdit: '1'
        }, {
          people: '李金龙',
          relationship: '大学室友',
          meet: '2010-09-02',
          place: '5#633',
          execg: '二哥',
          year: '8年',
          works: '遇见',
          isEdit: '1'
        }, {
          people: '马康',
          relationship: '大学室友',
          meet: '2010-09-02',
          place: '餐饮大厦',
          execg: '康哥',
          year: '8年',
          works: '不再联系',
          isEdit: '1'
        }, {
          people: '牛光卫',
          relationship: '大学室友',
          meet: '2010-09-02',
          place: '图书馆',
          execg: '牛牛娃',
          year: '8年',
          works: '断点',
          isEdit: '1'
        }, {
          people: '陆兆攀',
          relationship: '大学室友',
          meet: '1991-07-27',
          place: '百浪',
          execg: '帅哥',
          year: '27年',
          works: '不再犹豫',
          isEdit: '0'
        }, {
          people: '小甜',
          relationship: '亲密的人',
          meet: '2016-10-05',
          place: '小寨',
          execg: '甜甜圈',
          year: '2年',
          works: 'Forever Love',
          isEdit: '0'
        }]
      }
    }
  }
</script>

<style lang="scss">
.one {
      background: #ff1493;
    }
</style>

 效果如下图所示,表格行的背景颜色会根据一行中的isEdit 这个值得属性改变

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Vue.js 是一个流行的JavaScript框架,它的数据绑定和组件化特性使得开发动态Web应用变得更加容易。其中,强大的UI组件库(如Element,iView等)能够极大地提高Web应用开发效率。而el-table是一种用于展示表格数据的组件,它拥有可排序、过滤、分页等多种功能。本文主要介绍如何使用Vue.js实现el-table表头自定义。 在Vue.js中使用el-table组件时,表头(thead)用于显示列名和控制排序、过滤等操作。默认情况下,el-table组件根据数据源中的列名自动生成表头。若需自定义表头,可通过以下方式实现: 1. 使用el-table-column组件 在el-table中使用el-table-column组件可以实现自定义表头。具体操作如下: ```html <el-table :data="tableData"> <el-table-column prop="date" label="日期"></el-table-column> <el-table-column prop="name" label="姓名"></el-table-column> <el-table-column prop="address" label="地址"></el-table-column> </el-table> ``` 上述代码中,我们为el-table添加了三个el-table-column组件,分别对应表格中的三列数据。同时,我们在每个el-table-column组件上指定了prop和label属性,其中prop属性指定了对应的数据源中的字段名,label属性指定了表头标题。 2. 使用Scoped Slots 如果需要实现更加复杂的表头,可以使用Scoped Slots进行自定义。具体操作如下: ```html <el-table :data="tableData"> <template slot="header"> <el-row> <el-col :span="8">日期</el-col> <el-col :span="8">姓名</el-col> <el-col :span="8">地址</el-col> </el-row> </template> <el-table-column prop="date"></el-table-column> <el-table-column prop="name"></el-table-column> <el-table-column prop="address"></el-table-column> </el-table> ``` 上述代码中,我们使用了el-table的header slot,它可以让我们自定义表头,即在表头中添加任意HTML代码。在header slot中我们使用了el-row和el-col组件创建了一个表头行,然后通过span属性设置每列所占的宽度,最终实现了自定义表头。 总结 以上就是Vue.js实现el-table表头自定义的两种方式。使用el-table-column组件可以快速地实现简单的自定义表头,而使用Scoped Slots可以实现更加复杂的表头需求。选择合适的方式,可以大大提高开发效率和表格的可读性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值