欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue封裝動態(tài)表格方式詳解

 更新時間:2022年08月15日 09:49:41   作者:悟空和大王  
這篇文章主要為大家介紹了vue封裝動態(tài)表格方式示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

前言

這里只是提供一種想法并提供一些快速實現(xiàn),實際這些技巧可以用在很多地方,如:動態(tài)表單

實現(xiàn)方式簡述

  • 通過json定義要顯示的列
  • 通過slot實現(xiàn)自定義列
  • 通過require.context實現(xiàn)組件的自動注冊,并通過<components is="xxx"></components>, 調(diào)用動態(tài)注冊的組件

優(yōu)點:

  • 支持通過slot自定義擴(kuò)展
  • 支持編寫vue文件擴(kuò)展列的顯示方式
  • 支持通過json來控制列順序和哪些列需要顯示哪些不需要
  • 能避免封裝的table組件越來越復(fù)雜

表格實現(xiàn):

<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 = {};
// 將當(dāng)前目錄以及子目錄中的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: {
    // 組件動態(tài)注冊
    ...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>

調(diào)用示例

<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: "上海市普陀區(qū)金沙江路 1518 弄",
          sex: 1,
        },
        {
          date: "2016-05-04",
          name: "王小虎",
          address: "上海市普陀區(qū)金沙江路 1517 弄",
          sex: 2,
        },
        {
          date: "2016-05-01",
          name: "王小虎",
          address: "上海市普陀區(qū)金沙江路 1519 弄",
          sex: 3,
        },
      ],
    };
  },
  methods: {
    onEdit(row) {
      console.log(row);
    },
  },
};
</script>
<style scoped></style>

效果

參考文章

Vue業(yè)務(wù)組件封裝Table表格示例詳解

前端框架之封裝Vue第三方組件三個技巧

以上就是vue封裝動態(tài)表格方式詳解的詳細(xì)內(nèi)容,更多關(guān)于vue封裝動態(tài)表格的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • vue router 組件的高級應(yīng)用實例代碼

    vue router 組件的高級應(yīng)用實例代碼

    這篇文章主要介紹了vue-router 組件的高級應(yīng)用,本文通過實例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-04-04
  • Vue代理請求數(shù)據(jù)出現(xiàn)404問題及解決

    Vue代理請求數(shù)據(jù)出現(xiàn)404問題及解決

    這篇文章主要介紹了Vue代理請求數(shù)據(jù)出現(xiàn)404的問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • vue-video-player 通過自定義按鈕組件實現(xiàn)全屏切換效果【推薦】

    vue-video-player 通過自定義按鈕組件實現(xiàn)全屏切換效果【推薦】

    這篇文章主要介紹了vue-video-player,通過自定義按鈕組件實現(xiàn)全屏切換效果,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
    2018-08-08
  • 使用Vue3+PDF.js實現(xiàn)PDF預(yù)覽功能

    使用Vue3+PDF.js實現(xiàn)PDF預(yù)覽功能

    項目中有一個需要預(yù)覽下載pdf的需求,網(wǎng)上找了很久,決定使用 pdf.js 完成,下面這篇文章主要給大家介紹了關(guān)于使用Vue3+PDF.js實現(xiàn)PDF預(yù)覽功能的相關(guān)資料,需要的朋友可以參考下
    2022-12-12
  • Vue.js系列之vue-router(上)(3)

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

    這篇文章主要介紹了Vue.js系列之vue-router(上)(3)的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2017-01-01
  • vue中mock數(shù)據(jù),模擬后臺接口實例

    vue中mock數(shù)據(jù),模擬后臺接口實例

    這篇文章主要介紹了vue中mock數(shù)據(jù),模擬后臺接口實例,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-04-04
  • Vue 實現(xiàn)一個命令式彈窗組件功能

    Vue 實現(xiàn)一個命令式彈窗組件功能

    這篇文章主要介紹了vue實現(xiàn)命令式彈窗組件功能,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-09-09
  • 詳解利用eventemitter2實現(xiàn)Vue組件通信

    詳解利用eventemitter2實現(xiàn)Vue組件通信

    這篇文章主要介紹了詳解利用eventemitter2實現(xiàn)Vue組件通信,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-11-11
  • Vue使用Element-UI生成并展示表頭序號的方法

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

    序號算是在展示數(shù)據(jù)的時候,一種很普遍的屬性了,我們可以自己寫生成序號的規(guī)則,也可以借助第三方,這篇文章主要介紹了Vue使用Element-UI生成并展示表頭序號的方法,需要的朋友可以參考下
    2023-01-01
  • vue項目報錯:Missing?script:"serve"的解決辦法

    vue項目報錯:Missing?script:"serve"的解決辦法

    這篇文章主要給大家介紹了關(guān)于vue項目報錯:Missing?script:"serve"的解決辦法,"missing script: serve"是一個錯誤信息,意味著在執(zhí)行啟動腳本時,找不到名為"serve"的腳本,需要的朋友可以參考下
    2023-11-11

最新評論