element-ui el-table表格固定表头代码示例

 更新时间:2024年05月11日 08:31:43   作者:薛小胖And汤小懒  
Element-UI的el-table组件是一个非常强大的表格组件,它提供了多种常见的表格功能,如排序、筛选、分页、自定义列模板等,这篇文章主要给大家介绍了关于element-ui el-table表格固定表头的相关资料,需要的朋友可以参考下

前提:table内容过高时页面滚动到下方后,表头看不见无法明确各列的含义

1. 官网给出两种种属性来固定表头 height 以及 max-height;但是有个问题就是 height高度会被定死,max-height无法适应不同分辨率的情况

<el-table ref="multipleTable" :data="listData" height="200" class="img-tab">
      <el-table-column label="序号" prop="pagingCustomOrder" align="center" width="80" />
      <el-table-column align="center" label="报警时间" prop="warningTime" />
      <el-table-column align="center" label="企业名称" prop="companyName" />
</el-table>
<el-table ref="multipleTable" :data="listData" max-height="200" class="img-tab">
      <el-table-column label="序号" prop="pagingCustomOrder" align="center" width="80" />
      <el-table-column align="center" label="报警时间" prop="warningTime" />
      <el-table-column align="center" label="企业名称" prop="companyName" />
</el-table>

2. 可以进行样式控制表格高度,达到固定表头的需求,就可以避免高度定死的情况

<el-table ref="multipleTable" :data="listData" class="scroll-tab">
      <el-table-column label="序号" prop="pagingCustomOrder" align="center" width="80" />
      <el-table-column align="center" label="报警时间" prop="warningTime" />
      <el-table-column align="center" label="企业名称" prop="companyName" />
</el-table>
// table表头固定
.el-table.scroll-tab  {
  overflow: auto;
  max-height: calc(100% - 200px);
}
.scroll-tab .el-table__header-wrapper  {
  position: sticky;
  top: 0;//这个值根据实际情况而定
  z-index: 10;
}

3. 若是表格宽度也超出内容,需要横向滚动+竖向滚动

// table表头固定
.el-table.scroll-tab  {
  overflow: hidden;
  height: calc(100% - 200px);
}

.scroll-tab .el-table__header-wrapper  {
  position: sticky;
  top: 0;//这个值根据实际情况而定
  z-index: 10;
}

.scroll-tab .el-table__body-wrapper {
  height: calc(100% - 60px);
  overflow: auto;
}

附:el-table固定表头(设置height)出现内容过多时不能滚动问题

主要原因是el-table没有div包裹

解决:加一个div并设置其高度和overflow

我自己的主要代码

    <div class="contentTable">
      <el-table
        ref="table"
        :data="tableData"
        stripe
        @row-dblclick="onRowDblclick"
        height="100%"
      >
        <el-table-column
          type="index"
          align="center"
          label="序号"
          width="50"
        ></el-table-column>
        <el-table-column
          prop="templateName"
          align="center"
          label="模板名称"
          width="150"
        ></el-table-column>
        <el-table-column
          prop="mainContent"
          align="center"
          label="主要内容"
        ></el-table-column>
        <el-table-column
          prop="devContent"
          align="center"
          label="活动措施和设备状态"
        ></el-table-column>
        <el-table-column prop="operate" align="center" label="操作" width="80">
          <template slot-scope="scope">
            <el-button
              size="mini"
              class="delete-btn"
              @click="onDelete(scope.row)"
              title="删除"
              v-isLogin
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

css代码:

.contentTable {
  height: calc(100% - 50px) !important;
  overflow: scroll;
}
.contentTable >>> .el-table__body-wrapper::-webkit-scrollbar {
  width: 10px;
  height: 8px;
}

-webkit-scrollbar用来加滚动条的!!!

页面所有代码:

<template>
  <el-dialog
    :title="title"
    :visible.sync="visible"
    class="template-query"
    @opened="openInit"
    append-to-body
    width="80%"
  >
    <el-form
      :model="form"
      ref="form"
      :inline="true"
      style="text-align: right"
      size="small"
    >
      <el-form-item label="模板名称:" prop="templateName">
        <el-input
          v-model="form.templateName"
          maxlength="20"
          v-special-code
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onQuery">查询</el-button>
        <el-button type="primary" @click="onReset">重置</el-button>
      </el-form-item>
    </el-form>
    <div class="contentTable">
      <el-table
        ref="table"
        :data="tableData"
        stripe
        @row-dblclick="onRowDblclick"
        height="100%"
      >
        <el-table-column
          type="index"
          align="center"
          label="序号"
          width="50"
        ></el-table-column>
        <el-table-column
          prop="templateName"
          align="center"
          label="模板名称"
          width="150"
        ></el-table-column>
        <el-table-column
          prop="mainContent"
          align="center"
          label="主要内容"
        ></el-table-column>
        <el-table-column
          prop="devContent"
          align="center"
          label="活动措施和设备状态"
        ></el-table-column>
        <el-table-column prop="operate" align="center" label="操作" width="80">
          <template slot-scope="scope">
            <el-button
              size="mini"
              class="delete-btn"
              @click="onDelete(scope.row)"
              title="删除"
              v-isLogin
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </el-dialog>
</template>
<script>
import { listTemplatesByType } from "@/api/template.js";
import { removeTemplate } from "@/api/template.js";
import { getBizcodeList } from "@/api/common.js";
export default {
  props: {
    templateQueryVisible: {
      type: Boolean,
      default: false,
    },
    type: {
      type: String,
      default: "",
    },
    typeName: {
      type: String,
      default: "",
    },
  },
  data() {
    return {
      title: "",
      form: {
        templateName: "",
      },
      headField: [], //表头信息
      tableData: [], //表格数据
    };
  },
  computed: {
    visible: {
      get() {
        return this.templateQueryVisible;
      },
      set(val) {
        this.$emit("update:templateQueryVisible", val);
      },
    },
  },
  mounted() {},
  methods: {
    //打开窗口初始化
    openInit() {
      this.title = this.typeName + "模板管理";
      this.form.templateName = "";
      //根据type查询表头信息
      // listGridHeadByType({ type: this.type }).then(async (res) => {
      //   var headFieldList = JSON.parse(res.data.data);
      //   for (var i = 0; i < headFieldList.length; i++) {
      //     if ("codeType" in headFieldList[i]) {
      //       await getBizcodeList(headFieldList[i].codeType).then((res) => {
      //         headFieldList[i]["codeList"] = res.data.data;
      //       });
      //     }
      //   }
      //   this.headField = headFieldList;
      // });

      //查询模板数据
      this.onQuery();
    },
    //删除
    onDelete(row) {
      var that = this;
      this.$confirm("确定删除该模板?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        removeTemplate(row.id).then((res) => {
          if (res.data.code == "1") {
            that.$message({
              type: "success",
              message: "删除模板成功!",
            });
            that.onQuery();
          } else {
            that.$message({
              type: "error",
              message: "保存模板失败!",
            });
          }
        });
      });
    },

    //双击行加载模板数据
    onRowDblclick(row) {
      if (row.id) {
        delete row.id;
      }

      if (row.ID) {
        delete row.ID;
      }
      this.$emit("loadTemplateData", row);
    },

    //查询
    onQuery() {
      //根据type查询模板数据
      listTemplatesByType({
        type: this.type,
        name: this.form.templateName,
      }).then((res) => {
        var resData = res.data.data;
        var tableData = [];
        console.log(resData);
        if (resData) {
          for (var i = 0; i < resData.length; i++) {
            var content = JSON.parse(resData[i].content);
            let res = {
              id: resData[i].id,
              templateName: resData[i].name,
              mainContent: content.mainContent,
              devContent: content.devContent,
            };
            tableData.push(res);
          }
          this.tableData = tableData;
        } else {
          this.tableData = [];
        }
      });
    },

    //重置
    onReset() {
      if (this.$refs.form) {
        this.$refs.form.resetFields();
        this.onQuery();
      }
    },

    //渲染表格列
    itemFormatter(cellValue, codeList) {
      if (codeList && cellValue) {
        // return this.$common.renderCodeId(cellValue, codeList);
        return this.$common.renderCode(cellValue, "ID", "TEXT", codeList);
      } else {
        return cellValue;
      }
    },
  },
};
</script>
<style scoped>
.template-query >>> .el-dialog__body {
  height: 600px;
}

.template-query >>> .el-form-item__label {
  width: 85px !important;
}

.delete-btn {
  background-image: url("~@/assets/imgs/delete.png");
  width: 23px;
  height: 23px;
  padding-left: 5px;
  cursor: pointer;
  background-repeat: no-repeat;
}
.contentTable {
  height: calc(100% - 50px) !important;
  overflow: scroll;
}
.contentTable >>> .el-table__body-wrapper::-webkit-scrollbar {
  width: 10px;
  height: 8px;
}
</style>

总结 

到此这篇关于element-ui el-table表格固定表头的文章就介绍到这了,更多相关element-ui el-table固定表头内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • vue-router传参的四种方式超详细讲解

    vue-router传参的四种方式超详细讲解

    这篇文章主要介绍了vue-router传参的四种方式超详细,有router-link路由导航方式传参,调用$router.push实现路由传参,通过路由属性name匹配路由,再根据params传递参数等等,结合示例代码讲解的非常详细,需要的朋友可以参考下
    2022-12-12
  • Vue条件判断之循环举例详解

    Vue条件判断之循环举例详解

    在Vue进行前端开发中,条件判断主要用于根据不同的条件来决定显示或隐藏,或者进行视图之间的切换,这篇文章主要给大家介绍了关于Vue条件判断之循环举例详解的相关资料,需要的朋友可以参考下
    2024-07-07
  • Vue.js结合SortableJS实现树形数据拖拽

    Vue.js结合SortableJS实现树形数据拖拽

    本文主要介绍了Vue.js结合SortableJS实现树形数据拖拽,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-05-05
  • 如何解决el-checkbox选中状态更改问题

    如何解决el-checkbox选中状态更改问题

    这篇文章主要介绍了如何解决el-checkbox选中状态更改问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-07-07
  • vue实现codemirror代码编辑器中的SQL代码格式化功能

    vue实现codemirror代码编辑器中的SQL代码格式化功能

    这篇文章主要介绍了vue实现codemirror代码编辑器中的SQL代码格式化功能,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
    2019-08-08
  • IDEA安装vue插件图文详解

    IDEA安装vue插件图文详解

    这篇文章主要为大家详细介绍了IDEA安装vue插件图文,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-09-09
  • vue实现鼠标悬浮框效果

    vue实现鼠标悬浮框效果

    这篇文章主要为大家详细介绍了vue实现鼠标悬浮框,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-03-03
  • Vue多种方法实现表头和首列固定的示例代码

    Vue多种方法实现表头和首列固定的示例代码

    本篇文章主要介绍了Vue多种方法实现表头和首列固定的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-02-02
  • vue3获取当前路由地址的两种方法

    vue3获取当前路由地址的两种方法

    近期在做ve3的项目时因为功能需要,需要获取当前路由的地址,下面这篇文章主要给大家介绍了关于vue3获取当前路由地址的两种方法,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
    2022-10-10
  • Vue设置页面全屏实例代码

    Vue设置页面全屏实例代码

    文章介绍了如何在Vue中设置页面全屏,包括下载插件、全屏、退出全屏、全屏切换等操作,同时,还分享了在若依框架中实现主页面内容全屏的方法,包括配置全局变量和在布局文件中进行设置
    2025-02-02

最新评论