el-table实现嵌套表格的展示

这篇具有很好参考价值的文章主要介绍了el-table实现嵌套表格的展示。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

需求

一个表单中存在子表
el-table实现嵌套表格的展示,vue.js,elementui,javascript
列表返回格式
el-table实现嵌套表格的展示,vue.js,elementui,javascript
实现
el-table实现嵌套表格的展示,vue.js,elementui,javascript

实现思路

el-table中在嵌套一个el-table,这样数据格式就没问题了,主要就是样式

  1. 将共同的列放到一列中,通过渲染自定义表头render-header,将表头按照合适的宽度渲染出来
 <el-table-column
   prop="table"
   label="子表"
   class-name="has-child"
   :render-header="renderHeader"
   :width="childColumn.length * 120 + 2 + 'px'"
 >
   <template slot-scope="scope">
     <el-table
       border
       :data="scope.row.details"
       class="child-table"
       :show-header="false"
     >
       <el-table-column
         prop="startDate"
         align="center"
         label="起始日期"
         width="120px"
       ></el-table-column>
       <el-table-column
         prop="endDate"
         align="center"
         label="结束日期"
         width="120px"
       ></el-table-column>
       <el-table-column
         prop="applyDay"
         align="center"
         label="申请天数"
         width="120px"
       ></el-table-column>
       <el-table-column
         prop="destination"
         align="center"
         label="目的地"
         width="120px"
       ></el-table-column>
       <el-table-column
         prop="transportation"
         align="center"
         label="交通方式"
         width="120px"
       ></el-table-column>
       <el-table-column
         prop="businessTripReason"
         align="center"
         label="出差事由"
         width="120px"
       ></el-table-column>
       <el-table-column
         prop="returnDate"
         align="center"
         label="返程日期"
         width="120px"
       ></el-table-column>
       <el-table-column
         prop="actualDay"
         align="center"
         label="实际出差天数"
         width="120px"
       ></el-table-column>
       <el-table-column
         prop="subsidyDay"
         align="center"
         label="差补天数"
         width="120px"
       ></el-table-column>
       <el-table-column
         prop="remark"
         align="center"
         label="备注"
         width="120px"
       ></el-table-column>
     </el-table>
   </template>
 </el-table-column>

根据数据渲染表头

      childColumn: [
        {
          label: "起始日期",
          width: "120px",
        },
        {
          label: "结束日期",
          width: "120px",
        },
        {
          label: "申请天数",
          width: "120px",
        },
        {
          label: "目的地",
          width: "120px",
        },
        {
          label: "交通方式",
          width: "120px",
        },
        {
          label: "出差事由",
          width: "120px",
        },
        {
          label: "返程日期",
          width: "120px",
        },
        {
          label: "实际出差天数",
          width: "120px",
        },
        {
          label: "差补天数",
          width: "120px",
        },
        {
          label: "备注",
          width: "120px",
        },
      ],

    /**
     * @description 渲染子表
     */
    renderHeader(h, { column, $index }) {
      const childTable = this.childColumn.map((item) => {
        return h(
          "div",
          {
            style: {
              width: item.width,
              padding: "0 10px",
              textAlign: "center",
              flexShrink: 0,
              flexGrow: 0,
            },
          },
          item.label
        );
      });
      return h(
        "div",
        {
          class: ["child-head"],
        },
        childTable
      );
    },

调样式,慢慢调到合适就行文章来源地址https://www.toymoban.com/news/detail-833276.html

.has-child {
  padding: 0px !important;
  // display: none;
  & > .cell {
    padding: 0 !important;
  }
  ::before {
    height: 0;
  }
  .child-table {
    background-color: transparent;
    .cell{
      line-height: 34px;
    }
  }
  .el-table--enable-row-hover .el-table__body tr:hover > td.el-table__cell {
    background-color: transparent;
  }
  .el-table__body tr.current-row > td.el-table__cell,
  .el-table__body tr.selection-row > td.el-table__cell {
    background-color: transparent;
  }
  tr {
    background-color: transparent;
  }
  .child-head {
    display: flex;
  }
}

完整代码

<template>
  <div>
    <Search :config="searchConfig" @search="search">
      <el-button type="primary" @click="addItem" icon="el-icon-plus"
        >新增</el-button
      >
    </Search>
    <el-table
      border
      :data="tableData"
      v-loading="loading"
      class="el-child-table"
    >
      <el-table-column
        prop="applyDate"
        label="申请日期"
        align="center"
        width="120px"
      >
      </el-table-column>
      <el-table-column
        prop="nickName"
        label="申请人"
        align="center"
        width="140px"
      >
      </el-table-column>
      <el-table-column
        prop="deptName"
        label="申请部门"
        align="center"
        width="120px"
      >
      </el-table-column>
      <el-table-column
        prop="table"
        label="子表"
        class-name="has-child"
        :render-header="renderHeader"
        :width="childColumn.length * 120 + 2 + 'px'"
      >
        <template slot-scope="scope">
          <el-table
            border
            :data="scope.row.details"
            class="child-table"
            :show-header="false"
          >
            <el-table-column
              prop="startDate"
              align="center"
              label="起始日期"
              width="120px"
            ></el-table-column>
            <el-table-column
              prop="endDate"
              align="center"
              label="结束日期"
              width="120px"
            ></el-table-column>
            <el-table-column
              prop="applyDay"
              align="center"
              label="申请天数"
              width="120px"
            ></el-table-column>
            <el-table-column
              prop="destination"
              align="center"
              label="目的地"
              width="120px"
            ></el-table-column>
            <el-table-column
              prop="transportation"
              align="center"
              label="交通方式"
              width="120px"
            ></el-table-column>
            <el-table-column
              prop="businessTripReason"
              align="center"
              label="出差事由"
              width="120px"
            ></el-table-column>
            <el-table-column
              prop="returnDate"
              align="center"
              label="返程日期"
              width="120px"
            ></el-table-column>
            <el-table-column
              prop="actualDay"
              align="center"
              label="实际出差天数"
              width="120px"
            ></el-table-column>
            <el-table-column
              prop="subsidyDay"
              align="center"
              label="差补天数"
              width="120px"
            ></el-table-column>
            <el-table-column
              prop="remark"
              align="center"
              label="备注"
              width="120px"
            ></el-table-column>
          </el-table>
        </template>
      </el-table-column>
      <el-table-column
        prop="processStatusName"
        width="120px"
        label="状态"
        align="center"
      >
      </el-table-column>
      <el-table-column label="操作" align="center" width="100px" fixed="right">
        <template slot-scope="{ row }">
          <!-- 1、状态为被退回  2、申请人为当前登陆人 -->
          <el-button
            v-if="row.approveJurisdiction == 1"
            type="text"
            @click="approval(row)"
            >审批</el-button
          >
          <el-button
            type="text"
            @click="check(row)"
            v-if="row.checkJurisdiction == 1"
            >核定</el-button
          >
          <el-button
            v-if="
              row.processStatusName == '被退回' &&
              row.nickName === $store.state.user.nickName
            "
            type="text"
            @click="editItem(row)"
            >修改</el-button
          >
          <el-button type="text" @click="viewItem(row)">查看</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      :total="pageInfo.total"
      :page.sync="pageInfo.pageNum"
      :limit.sync="pageInfo.pageSize"
      @pagination="getList"
    />
    <DialogForm ref="dialogFormRef" @success="submitSuccess"></DialogForm>
  </div>
</template>

<script>
import Search from "@/components/Search/index.vue";
import DialogForm from "./components/form.vue";
import { checkRole } from "@/utils/permission";
import {
  apiGetBusinessTripList,
  apiGetStatusOption,
} from "@/api/businessTrip/index";

export default {
  name: "BusinessTrip",
  components: {
    Search,
    DialogForm,
  },
  data() {
    return {
      searchConfig: [
        {
          label: "申请日期",
          prop: "publishTime",
          type: "daterange",
        },
        {
          type: "select",
          label: "状态",
          prop: "processStatus",
          option: [],
        },
        {
          type: "input",
          label: "模糊查询",
          prop: "queryCondition",
        },
      ],
      pageInfo: {
        pageNum: 1,
        pageSize: 10,
        total: 0,
      },
      tableData: [],
      childColumn: [
        {
          label: "起始日期",
          width: "120px",
        },
        {
          label: "结束日期",
          width: "120px",
        },
        {
          label: "申请天数",
          width: "120px",
        },
        {
          label: "目的地",
          width: "120px",
        },
        {
          label: "交通方式",
          width: "120px",
        },
        {
          label: "出差事由",
          width: "120px",
        },
        {
          label: "返程日期",
          width: "120px",
        },
        {
          label: "实际出差天数",
          width: "120px",
        },
        {
          label: "差补天数",
          width: "120px",
        },
        {
          label: "备注",
          width: "120px",
        },
      ],
      disabled: true,
      currentItem: {},
      searchForm: {},
      loading: false,
    };
  },
  mounted() {
    this.getStatusOption();
    this.toApproval();
  },
  activated() {
    this.getList();
    this.toApproval();
  },
  methods: {
    checkRole,
    /**
     * @description 从首页过来自动打开弹窗
     */
    toApproval() {
      const id = this.$route.query.approvalId;
      const jumpType = this.$route.query.jumpType;
      this.$nextTick(() => {
        if (jumpType == 21) {
          this.$refs.dialogFormRef.open("审批", id);
        } else if (jumpType == 22) {
          this.$refs.dialogFormRef.open("核定", id);
        }
      });
    },
    search(form) {
      this.searchForm = form;
      this.getList();
    },
    getList() {
      this.loading = true;
      const params = {
        ...this.searchForm,
        ...this.pageInfo,
        startApplyDate:
          this.searchForm.publishTime && this.searchForm.publishTime[0],
        endApplyDate:
          this.searchForm.publishTime && this.searchForm.publishTime[1],
      };
      Reflect.deleteProperty(params, "publishTime");
      apiGetBusinessTripList(params)
        .then((response) => {
          this.tableData = response.data.records;
          this.pageInfo.total = response.data.total;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    /**
     * @description 获取流程状态下拉
     */
    getStatusOption() {
      apiGetStatusOption().then((response) => {
        this.searchConfig[1].option = response.data.map((item) => {
          return {
            label: item.name,
            value: item.id,
          };
        });
      });
    },
    addItem() {
      this.$refs.dialogFormRef.open("新增");
    },
    approval(row) {
      this.$refs.dialogFormRef.open("审批", row.id);
    },
    check(row) {
      this.$refs.dialogFormRef.open("核定", row.id);
    },
    editItem(row) {
      this.$refs.dialogFormRef.open("修改", row.id);
    },
    viewItem(row) {
      this.$refs.dialogFormRef.open("详情", row.id);
    },
    /**
     * @description 渲染子表
     */
    renderHeader(h, { column, $index }) {
      const childTable = this.childColumn.map((item) => {
        return h(
          "div",
          {
            style: {
              width: item.width,
              padding: "0 10px",
              textAlign: "center",
              flexShrink: 0,
              flexGrow: 0,
            },
          },
          item.label
        );
      });
      return h(
        "div",
        {
          class: ["child-head"],
        },
        childTable
      );
    },
    submitSuccess() {
      this.$router.replace({ path: "/businessTrip", query: {} });
      this.getList();
    },
  },
};
</script>

<style lang="scss" scoped></style>

到了这里,关于el-table实现嵌套表格的展示的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包