vue封装动态表格方式详解

 更新时间:2022年08月15日 09:49:41   作者:悟空和大王  
这篇文章主要为大家介绍了vue封装动态表格方式示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

前言

这里只是提供一种想法并提供一些快速实现,实际这些技巧可以用在很多地方,如:动态表单

实现方式简述

  • 通过json定义要显示的列
  • 通过slot实现自定义列
  • 通过require.context实现组件的自动注册,并通过<components is="xxx"></components>, 调用动态注册的组件

优点:

  • 支持通过slot自定义扩展
  • 支持编写vue文件扩展列的显示方式
  • 支持通过json来控制列顺序和哪些列需要显示哪些不需要
  • 能避免封装的table组件越来越复杂

表格实现:

<template>
  <el-table :data="tableData" style="width: 100%" height="100%">
    <el-table-column
      v-for="column in columnList"
      :key="column.prop"
      v-bind="column.columnAttrs"
    >
      <template slot-scope="scope">
        <slot
          v-if="column.type === 'slot'"
          :name="column.slotName"
          :row="scope.row"
        ></slot>
        <components
          v-else
          :row="scope.row"
          :prop="column.prop"
          :config="column"
          :is="`column-${column.type}`"
        ></components>
      </template>
    </el-table-column>
  </el-table>
</template>
<script>
const modules = {};
// 将当前目录以及子目录中的index.vue自动注册为组件(require.context是webpack提供的,如果是vite要用vite的方式)
const files = require.context("./", true, /index.vue$/);
files.keys().forEach((item) => {
  const name = item.split("/")[1];
  modules[`column-${name}`] = files(item).default;
});
console.log(modules, "modules");
export default {
  name: "CustomTable",
  components: {
    // 组件动态注册
    ...modules,
  },
  props: {
    tableData: {
      type: Array,
      default: () => [],
    },
    columnList: {
      type: Array,
      default: () => [],
    },
  },
};
</script>
<style scoped></style>

func组件

<template>
  <span>{{ config.call(row, config) }}</span>
</template>
<script>
export default {
  name: "ColumnFunc",
  props: {
    config: {
      type: Object,
    },
    row: {
      type: Object,
    },
  },
};
</script>
<style scoped></style>

text组件:

<template>
  <span>{{ row[config.prop] }}</span>
</template>
<script>
export default {
  name: "ColumnText",
  props: {
    config: {
      type: Object,
    },
    row: {
      type: Object,
    },
  },
};
</script>
<style scoped></style>

调用示例

<template>
  <CustomTable :column-list="columnList" :table-data="tableData">
    <template #operate="{ row }">
      <el-button size="small">删除</el-button>
      <el-button size="small" type="primary" @click="onEdit(row)">
        编辑
      </el-button>
    </template>
  </CustomTable>
</template>
<script>
import CustomTable from "@/components/my-table/CustomTable";
export default {
  name: "CustomTableDemo",
  components: {
    CustomTable,
  },
  data() {
    return {
      columnList: [
        {
          type: "text",
          prop: "name",
          columnAttrs: {
            label: "姓名",
            width: "180",
          },
        },
        {
          type: "text",
          prop: "date",
          columnAttrs: {
            label: "日期",
            width: "180",
          },
        },
        {
          type: "func",
          prop: "sex",
          columnAttrs: {
            label: "性别",
          },
          call: (row) => {
            switch (row.sex) {
              case 1: {
                return "男";
              }
              case 2: {
                return "女";
              }
              default: {
                return "未知";
              }
            }
          },
        },
        {
          type: "slot",
          slotName: "operate",
          prop: "operate",
          columnAttrs: {
            label: "操作",
          },
        },
      ],
      tableData: [
        {
          date: "2016-05-02",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1518 弄",
          sex: 1,
        },
        {
          date: "2016-05-04",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1517 弄",
          sex: 2,
        },
        {
          date: "2016-05-01",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1519 弄",
          sex: 3,
        },
      ],
    };
  },
  methods: {
    onEdit(row) {
      console.log(row);
    },
  },
};
</script>
<style scoped></style>

效果

参考文章

Vue业务组件封装Table表格示例详解

前端框架之封装Vue第三方组件三个技巧

以上就是vue封装动态表格方式详解的详细内容,更多关于vue封装动态表格的资料请关注脚本之家其它相关文章!

相关文章

  • vue router 组件的高级应用实例代码

    vue router 组件的高级应用实例代码

    这篇文章主要介绍了vue-router 组件的高级应用,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2019-04-04
  • Vue代理请求数据出现404问题及解决

    Vue代理请求数据出现404问题及解决

    这篇文章主要介绍了Vue代理请求数据出现404的问题及解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-07-07
  • vue-video-player 通过自定义按钮组件实现全屏切换效果【推荐】

    vue-video-player 通过自定义按钮组件实现全屏切换效果【推荐】

    这篇文章主要介绍了vue-video-player,通过自定义按钮组件实现全屏切换效果,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
    2018-08-08
  • 使用Vue3+PDF.js实现PDF预览功能

    使用Vue3+PDF.js实现PDF预览功能

    项目中有一个需要预览下载pdf的需求,网上找了很久,决定使用 pdf.js 完成,下面这篇文章主要给大家介绍了关于使用Vue3+PDF.js实现PDF预览功能的相关资料,需要的朋友可以参考下
    2022-12-12
  • Vue.js系列之vue-router(上)(3)

    Vue.js系列之vue-router(上)(3)

    这篇文章主要介绍了Vue.js系列之vue-router(上)(3)的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下
    2017-01-01
  • vue中mock数据,模拟后台接口实例

    vue中mock数据,模拟后台接口实例

    这篇文章主要介绍了vue中mock数据,模拟后台接口实例,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-04-04
  • Vue 实现一个命令式弹窗组件功能

    Vue 实现一个命令式弹窗组件功能

    这篇文章主要介绍了vue实现命令式弹窗组件功能,本文给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2019-09-09
  • 详解利用eventemitter2实现Vue组件通信

    详解利用eventemitter2实现Vue组件通信

    这篇文章主要介绍了详解利用eventemitter2实现Vue组件通信,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-11-11
  • Vue使用Element-UI生成并展示表头序号的方法

    Vue使用Element-UI生成并展示表头序号的方法

    序号算是在展示数据的时候,一种很普遍的属性了,我们可以自己写生成序号的规则,也可以借助第三方,这篇文章主要介绍了Vue使用Element-UI生成并展示表头序号的方法,需要的朋友可以参考下
    2023-01-01
  • vue项目报错:Missing script:"serve"的解决办法

    vue项目报错:Missing script:"serve"的解决办法

    这篇文章主要给大家介绍了关于vue项目报错:Missing script:"serve"的解决办法,"missing script: serve"是一个错误信息,意味着在执行启动脚本时,找不到名为"serve"的脚本,需要的朋友可以参考下
    2023-11-11

最新评论