基于element-ui table组件二次封装

// 二次封装table组件
<template>
  <div class="lby_table">
    <el-table
      :data="dataSource"
      :border="border"
      v-loading="tableLoading"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        v-if="isSelection"
        type="selection"
        width="55"
      />
      <template v-for="column in columns">
        <el-table-column
          v-if="column.slot"
          :key="column.prop"
          :label="column.label"
          :prop="column.prop"
          :min-width="column.width"
          :width="column.width"
        >
          <template slot-scope="scope">
            <slot
              :name="column.slot"
              :row="scope.row"
              :column="column"
            ></slot>
          </template>
        </el-table-column>
        <el-table-column
          v-else
          :key="column.prop"
          :label="column.label"
          :prop="column.prop"
          :min-width="column.width"
          :width="column.width"
          :class-name="column.className"
        >
        </el-table-column>
      </template>
    </el-table>
    <el-pagination
      v-if="pagination"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page.sync="pagination.pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pagination.pageSize"
      :total="total"
      layout="total, sizes, prev, pager, next, jumper"
    ></el-pagination>
  </div>
</template>

export default {
  name: "CustomTable",
  props: {
    columns: {
      type: Array,
      required: true,
      default: () => [],
    },
    dataSource: {
      type: Array,
      required: true,
      default: () => [],
    },
    pagination: {
      type: Object,
      default: () => {
        return {
          pageIndex: 1,
          pageSize: 10
        };
      },
    },
    total: {
      type: Number,
      default: 0
    },
    border: {
      type: Boolean,
      default: false
    },
    tableLoading: {
      type: Boolean,
      default: false
    },
    isSelection: {
      type: Boolean,
      default: false
    }
  },
  methods: {
    handleSizeChange (pageSize) {
      this.$emit("onSizeChange", pageSize);
    },
    handleCurrentChange (pageIndex) {
      this.$emit("onCurrentChange", pageIndex);
    },
    // 复选框选中
    handleSelectionChange (selection) {
      this.$emit('onSelection', selection)
    }
  },
};
<style lang="scss" scoped>
.lby_table {
  width: 100%;
  border-radius: 4px;
  background: #ffffff;
  height: 0;
  flex: 1;
  display: flex;
  flex-direction: column;

  ::v-deep .el-table {
    height: calc(100% - 52px);
    .el-table__body-wrapper {
      height: calc(100% - 44px);
      overflow-y: auto;
    }
    thead {
      height: 40px;
      border-radius: 4px;
      font-weight: 400;
      color: rgba(28, 31, 35, 0.6);

      tr {
        th {
          border-bottom: 0;
          font-size: 14px;
          color: rgba(28, 31, 35, 0.6);
          font-weight: 400;
          background: #ffffff;
        }
      }
    }
    tbody {
      th {
        border-bottom: 0;
        font-size: 14px;
        color: rgba(28, 31, 35, 0.8);
        font-weight: 400;
      }
      .el-table__cell {
        border-bottom: 0;
      }
      .abnormal {
        color: rgba(227, 79, 72, 1);
      }
    }
    .cell {
      line-height: 24px;
      padding-left: 16px;
    }
  }
  ::v-deep .el-pagination {
    padding: 12px 32px;
    text-align: right;
    box-shadow: 0px 4px 14px 0px rgba(0, 0, 0, 0.1);
  }
}
</style>



// 调用
<template>
    <CustomTable
      :total='total'
      :columns='columns'
      :pagination='page'
      :isSelection='true'
      :dataSource='dataSource'
      :tableLoading="tableLoading"
      @onSelection='handleSelection'
      @onSizeChange='handleSizeChange'
      @onCurrentChange='handleCurrentChange'
    >
      <template v-slot:userCount="{ row }">
        <div>{{row.userCount}}人</div>
      </template>
      <template v-slot:status="{ row }">
        <dict-tag
          :options="options"
          :value="row.status"
        />
      </template>
      <template v-slot:operate="{ row }">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(row)"
          >查看</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(row)"
          >编辑</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(row)"
          >删除</el-button>
      </template>
    </CustomTable>
</template>

import { columns }  from './config.js' 
export default {
    // ........
    data () {
        // 表头
        columns,
        total: 0,
        page: {
            pageIndex: 1,
            pageSize: 10,
        }
       dataSource: [],
       tableLoading: true,
      },
    },
    mothods: {
        handleSizeChange (pageSize) {
            this.page.pageSize = pageSize
            // 请求列表
            this.xxx()
        },
        handleCurrentChange (pageIndex) {
            this.page.pageIndex = pageIndex
             // 请求列表
            this.xxx()
        },
    // 省略............................
    }
}


// config.js    配置文件
export const columns = [
  {
    prop: 'userName',
    label: '用户名称',
  },
  {
    prop: 'userCount',
    label: '人员数量',
    slot: 'userCount',
  },
  {
    prop: 'status',
    label: '状态',
    slot: 'status',
  },
  {
    prop: 'createTime',
    label: '创建时间'
  },
  {
    prop: 'operate',
    label: '操作',
    slot: 'operate'
  }
]

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容