封装el-table 基于element封装可配置JSON表格组件

基于element封装可配置JSON表格组件

话不多说直接贴代码,复制运行即可查看效果

子组件全部代码

html 复制代码
<template>
  <div class="custom-table">
    <el-table
      :data="tableData"
      border
      style="width: 100%"
      size="mini"
      max-height="800"
      v-bind="$attrs"
      v-on="$listeners"
      highlight-current-row
    >
      <template v-for="item in config.column">
        <!-- 操作 -->
        <template v-if="item.type === 'handler'">
          <el-table-column
            :key="item.prop"
            v-bind="item"
            align="center"
            fixed="right"
          >
            <template slot-scope="{ row }">
              <el-button
                v-for="btn in item.btns"
                :key="btn.label"
                type="text"
                @click="handelClick($event, btn.click, row)"
                :icon="btn.icon"
              >
                <!-- {{ btn.label }} -->
              </el-button>
            </template>
          </el-table-column>
        </template>
        <!-- 自定义slot -->
        <template v-else-if="item.type === 'custom'">
          <el-table-column :key="item.prop" v-bind="item" align="center">
            <template slot-scope="scope">
              <slot :name="item.slotName" v-bind="scope" :prop="item.prop" />
            </template>
          </el-table-column>
        </template>
        <!-- 默认 -->
        <template v-else>
          <el-table-column
            :key="item.prop"
            v-bind="item"
            align="center"
            :show-overflow-tooltip="true"
          />
        </template>
      </template>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      v-if="config.usePagination"
      background
      small
      layout="prev, pager, next"
      style="text-align: center; margin-top: 10px"
      :current-page="config.paginationData.currentPage"
      :page-size="config.paginationData.pageSize"
      :total="config.paginationData.total"
      @current-change="handleCurrentChange"
    >
    </el-pagination>
  </div>
</template>

<script>
export default {
  props: {
    config: {
      type: Object,
      default() {
        return {
          column: [],
          paginationData: {
            pageSize: 20,
            currentPage: 1,
            total: 0,
          },
        };
      },
      required: true,
    },
    tableData: {
      type: Array,
      default() {
        return [];
      },
    },
  },
  mounted() {
    console.log(this.config, "config");
  },
  methods: {
    handelClick(e, click, row) {
      e.stopPropagation();
      this.$emit(click, row);
    },
    handleCurrentChange(val) {
      this.$emit("currentChange", val);
    },
  },
};
</script>

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

父组件使用

html 复制代码
<template>
  <div>
    <el-button @click="getData">data</el-button>
    <CustomTable
      size="mini"
      v-loading="loading"
      :config="tableConfig"
      :table-data="tableData"
      @row-click="handleRowClick"
      @selection-change="selectionChange"
      @handelEditClick="handelEditClick"
      @handelTeamEditClick="handelTeamEditClick"
      @deleteItemClick="deleteItemClick"
      @currentChange="handleCurrentChange"
    >
      <template #inputEdit="{ row }">
        <el-input
          v-model="row.inputValue"
          placeholder="please input"
          size="small"
        ></el-input>
      </template>
      <template #artcleName="{ row }">
        <el-tag v-for="item in row.artcleName" :key="item.id" size="small">
          {{ item.name }}
        </el-tag>
      </template>
    </CustomTable>
  </div>
</template>

<script>
import tableConfig from "./config";
import CustomTable from "@/components/CustomTable/index.vue";

export default {
  components: {
    CustomTable,
  },
  data() {
    return {
      loading: false,
      tableConfig,
      tableData: [
        {
          id: 1,
          text: "test",
          title: "test",
          admin: "guanliyuan ",
          category: "小白",
          inputValue: "Context",
          artcleName: [
            { id: 1, name: "Context" },
            { id: 2, name: "标签" },
          ],
        },
      ],
    };
  },
  created() {
    this.tableData = this.generateRandomData(50);
    this.tableConfig.paginationData.total = this.tableData.length;
  },
  methods: {
    getData() {
      console.log(this.tableData, "表格数据");
    },
    generateRandomData(count) {
      const categories = ["小白", "中级", "高级", "专家", "初学者"];
      const admins = ["guanliyuan", "administrator", "admin", "user", "tester"];
      const texts = ["test", "example", "sample", "demo", "testcase"];
      const titles = [
        "Test Title",
        "Example Title",
        "Sample Title",
        "Demo Title",
        "Testcase Title",
      ];

      const randomElement = (arr) =>
        arr[Math.floor(Math.random() * arr.length)];
      const randomString = (length) =>
        Math.random()
          .toString(36)
          .substring(2, length + 2);

      return Array.from({ length: count }, (_, index) => ({
        id: index + 1,
        text: randomElement(texts),
        title: randomElement(titles),
        admin: randomElement(admins),
        category: randomElement(categories),
        inputValue: randomString(8),
        artcleName: Array.from({ length: 2 }, (_, nameIndex) => ({
          id: nameIndex + 1,
          name: randomString(5),
        })),
      }));
    },
    handleRowClick(val) {
      console.log(val, "点击行");
    },
    selectionChange(val) {
      console.log(val, "勾选");
    },
    handelEditClick(row) {
      console.log(row, "编辑");
    },
    handelTeamEditClick(row) {
      console.log(row, "团队编辑");
    },
    deleteItemClick(row) {
      console.log(row, "删除");
    },
    handleCurrentChange(val) {
      this.tableConfig.paginationData.currentPage = val;
      console.log(val, "当前页");
    },
  },
};
</script>

可配置文件 js

放置同级位置

js 复制代码
const tableConfig = {
  //type 区分操作类型  normal:普通  handler:操作  custom:自定义插槽
  column: [
    { type: "selection", width: 60, align: "center" },
    {
      type: "normal",
      prop: "text",
      label: "KeyWords",
      width: 200,
    },
    { type: "normal", prop: "title", label: "Title" },
    {
      type: "normal",
      prop: "category",
      label: "Category",
      width: 150,
    },
    {
      type: "normal",
      prop: "admin",
      label: "Admin",
      width: 200,
    },
    {
      type: "handler",
      label: "Controls",
      width: 150,
      btns: [
        {
          label: "Edit",
          click: "handelEditClick",
          icon: "el-icon-edit",
        },
        {
          label: "TeamEdit",
          click: "handelTeamEditClick",
          icon: "el-icon-s-open",
        },
        {
          label: "Delete",
          click: "deleteItemClick",
          icon: "el-icon-delete",
        },
      ],
    },
    {
      type: "custom",
      label: "Input",
      prop: "inputValue",
      slotName: "inputEdit",
    },
    {
      type: "custom",
      label: "artcleName",
      prop: "artcleName",
      slotName: "artcleName",
    },
  ],
  usePagination: true,
  paginationData: {
    pageSize: 20,
    currentPage: 1,
    total: 0,
  },
};

export default tableConfig;
相关推荐
耶啵奶膘24 分钟前
uniapp-是否删除
linux·前端·uni-app
奋斗的小花生1 小时前
c++ 多态性
开发语言·c++
魔道不误砍柴功1 小时前
Java 中如何巧妙应用 Function 让方法复用性更强
java·开发语言·python
闲晨1 小时前
C++ 继承:代码传承的魔法棒,开启奇幻编程之旅
java·c语言·开发语言·c++·经验分享
老猿讲编程1 小时前
一个例子来说明Ada语言的实时性支持
开发语言·ada
王哈哈^_^2 小时前
【数据集】【YOLO】【目标检测】交通事故识别数据集 8939 张,YOLO道路事故目标检测实战训练教程!
前端·人工智能·深度学习·yolo·目标检测·计算机视觉·pyqt
Chrikk2 小时前
Go-性能调优实战案例
开发语言·后端·golang
幼儿园老大*2 小时前
Go的环境搭建以及GoLand安装教程
开发语言·经验分享·后端·golang·go
canyuemanyue2 小时前
go语言连续监控事件并回调处理
开发语言·后端·golang
杜杜的man2 小时前
【go从零单排】go语言中的指针
开发语言·后端·golang