Vue - el-table 自定义表头(下拉框或输入框等标签)

ElementUI官网:https://element.eleme.cn/#/zh-CN/component/table#zi-ding-yi-biao-tou

效果预览

在这里插入图片描述

代码实现

<template>
  <div class="page">
    <el-table
      class="tabel"
      :data="tableData"
      :span-method="arraySpanMethod"
      :header-cell-style="{ background: '#029ef1', color: '#ffffff' }"
      border
    >
      <el-table-column prop="id" label="ID" width="180" align="center">
        <el-table-column align="center" show-overflow-tooltip>
          <template slot="header" slot-scope="scope">
            <span v-show="false">{{ scope.row }}</span>
            <el-select
              v-model="params.id"
              filterable
              clearable
              placeholder="请选择ID"
            >
              <el-option label="12987122" value="12987122"> </el-option>
              <el-option label="12987123" value="12987123"> </el-option>
              <el-option label="12987124" value="12987124"> </el-option>
              <el-option label="12987125" value="12987125"> </el-option>
              <el-option label="12987126" value="12987126"> </el-option>
            </el-select>
          </template>
          <template slot-scope="scope">
            {{ scope.row.id }}
          </template>
        </el-table-column>
      </el-table-column>
      <el-table-column prop="name" label="姓名" align="center">
        <el-table-column align="center" show-overflow-tooltip>
          <template slot="header" slot-scope="scope">
            <span v-show="false">{{ scope.row }}</span>
            <el-input v-model="params.name" placeholder="请输入姓名"></el-input>
          </template>
          <template slot-scope="scope">
            {{ scope.row.name }}
          </template>
        </el-table-column>
      </el-table-column>
      <el-table-column prop="amount1" label="数值 1" align="center">
      </el-table-column>
      <el-table-column prop="amount2" label="数值 2" align="center">
      </el-table-column>
      <el-table-column prop="amount3" label="数值 3" align="center">
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tableData: [
        {
          id: "12987122",
          name: "王小虎1",
          amount1: "234",
          amount2: "3.2",
          amount3: 10,
        },
        {
          id: "12987123",
          name: "王小虎2",
          amount1: "165",
          amount2: "4.43",
          amount3: 12,
        },
        {
          id: "12987124",
          name: "王小虎3",
          amount1: "324",
          amount2: "1.9",
          amount3: 9,
        },
        {
          id: "12987125",
          name: "王小虎4",
          amount1: "621",
          amount2: "2.2",
          amount3: 17,
        },
        {
          id: "12987126",
          name: "王小虎5",
          amount1: "539",
          amount2: "4.1",
          amount3: 15,
        },
      ],
      params: {
        id: "",
        name: "",
      },
    };
  },
};
</script>

<style scoped>
.page {
  width: 100%;
  height: 100%;
  padding: 20px;
  box-sizing: border-box;
  background: #e9e9e9;
}
.tabel {
  width: 60%;
}
.tabel >>> td {
  background-color: #92c2f1;
  color: #040404;
}
</style>
  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
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可以实现更加复杂的表头需求。选择合适的方式,可以大大提高开发效率和表格的可读性。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值