el-table 实现自定义列

el-table 实现自定义列

在这里插入图片描述

 <el-table-column width="100" v-for="info in rightHeader" :key="info.key" :property="info.key" :label="info.label">
            <template slot-scope="scope">
              <!-- <template v-for="item in scope.row[scope.column.property]" >
              {{item}}
            </template> -->
              <!-- {{scope.row[scope.column.property]['type']}} -->
              <!-- {{scope.row[scope.column.property].get}} -->
              <el-button v-if="scope.row[scope.column.property] == 'info'" style="background: #c6c6d1" plain circle></el-button>
              <el-button v-if="scope.row[scope.column.property] == 'danger'" style="background: #f87070" plain circle></el-button>
              <el-button v-if="scope.row[scope.column.property] == 'success'" style="background: #91e1b1" plain circle @click="test(scope)"></el-button>
            </template>
          </el-table-column>

表格列遍历的数据来源

  rightHeader: [
    {
      label: "自定义1",
      key: "Jan",
    },

    {
      label: "自定义2",
      key: "Feb",
    },
    {
      label: "自定义3",
      key: "Mar",
    },
    {
      label: "自定义4",
      key: "Apr",
    },

    {
      label: "自定义5",
      key: "May",
    },

    {
      label: "自定义6",
      key: "Jun",
    },
    {
      label: "自定义7",
      key: "Jul",
    },
    {
      label: "自定义8",
      key: "Aug",
    },
    {
      label: "自定义9",
      key: "Sep",
    },
    {
      label: "自定义10",
      key: "Oct",
    },
    {
      label: "自定义11",
      key: "Nov",
    },
    {
      label: "自定义12",
      key: "Dec",
    },
  ],

表格自定义字段的数据的格式化

 let date = new Date();
      let y;
      if (this.qryForm.planYear != null && this.qryForm.planYear != "")
        y = this.qryForm.planYear;
      else
        y = date.getFullYear();
      for (let index = 1; index < 13; index++) {
        let time = y + "年" + index + "月";
        this.rightHeader[index - 1].label = time;
      }
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可以实现更加复杂的表头需求。选择合适的方式,可以大大提高开发效率和表格的可读性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值