当前位置: 代码网 > it编程>前端脚本>Vue.js > element-ui el-table表格固定表头代码示例

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

2024年05月26日 Vue.js 我要评论
前提:table内容过高时页面滚动到下方后,表头看不见无法明确各列的含义1. 官网给出两种种属性来固定表头 height 以及 max-height;但是有个问题就是 height高度会被定死,max

前提: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固定表头内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com