Vue基于Element-ui實現表格彈窗組件
更新時間:2022年04月12日 11:30:21 作者:清虛桂意
這篇文章主要為大家詳細介紹了Vue基于Element-ui實現表格彈窗組件,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了Vue基于Element-ui實現表格彈窗組件的具體代碼,供大家參考,具體內容如下
效果圖
使用方式
acTable1 () { ? this.$modalTable({ ? ? title: "表格一", ? ? tableData: [{ ? ? ? date: '2016-05-02', ? ? ? name: '王小虎', ? ? ? address: '上海市普陀區(qū)金沙江路 1518 弄' ? ? }, { ? ? ? date: '2016-05-04', ? ? ? name: '王小虎', ? ? ? address: '上海市普陀區(qū)金沙江路 1517 弄' ? ? }, { ? ? ? date: '2016-05-01', ? ? ? name: '王小虎', ? ? ? address: '上海市普陀區(qū)金沙江路 1519 弄' ? ? }, { ? ? ? date: '2016-05-03', ? ? ? name: '王小虎', ? ? ? address: '上海市普陀區(qū)金沙江路 1516 弄' ? ? }], ? ? tableColumn: [ ? ? ? { ? ? ? ? prop: "date", ? ? ? ? label: "日期", ? ? ? ? width: "180" ? ? ? }, ? ? ? { ? ? ? ? prop: "name", ? ? ? ? label: "姓名", ? ? ? }, ? ? ? { ? ? ? ? prop: "address", ? ? ? ? label: "地址", ? ? ? } ? ? ] ? }) }, acTable2 () { ? this.$modalTable({ ? ? title: "表格二", ? ? tableData: [], ? ? tableColumn: [ ? ? ? { ? ? ? ? prop: "date", ? ? ? ? label: "日期", ? ? ? ? width: "180" ? ? ? }, ? ? ? { ? ? ? ? prop: "name", ? ? ? ? label: "姓名", ? ? ? }, ? ? ? { ? ? ? ? prop: "address", ? ? ? ? label: "地址", ? ? ? } ? ? ] ? }) }, acTable3 () { ? this.$modalTable({ ? ? title: "表格三", ? ? tableData: [{ ? ? ? date: '2016-05-02', ? ? ? name: '王小虎', ? ? ? address: '上海市普陀區(qū)金沙江路 1518 弄' ? ? }, { ? ? ? date: '2016-05-04', ? ? ? name: '王小虎', ? ? ? address: '上海市普陀區(qū)金沙江路 1517 弄' ? ? }, { ? ? ? date: '2016-05-01', ? ? ? name: '王小虎', ? ? ? address: '上海市普陀區(qū)金沙江路 1519 弄' ? ? }, { ? ? ? date: '2016-05-03', ? ? ? name: '王小虎', ? ? ? address: '上海市普陀區(qū)金沙江路 1516 弄' ? ? }], ? ? tableColumn: [ ? ? ? { ? ? ? ? prop: "name", ? ? ? ? label: "姓名", ? ? ? }, ? ? ? { ? ? ? ? prop: "date", ? ? ? ? label: "日期", ? ? ? }, ? ? ? { ? ? ? ? prop: "address", ? ? ? ? label: "地址", ? ? ? } ? ? ] ? }) },
1、創(chuàng)建modalTable.vue文件
將變量放在data中,正常開發(fā)即可,后續(xù)會通過別的方式將數據傳入組件data中。
<template> ? <el-dialog ref="dialog" ? ? ? ? ? ? ?:title="title" ? ? ? ? ? ? ?:visible.sync="visible" ? ? ? ? ? ? ?width="30%" ? ? ? ? ? ? ?:before-close="beforeClose"> ? ? <el-table :data="tableData" ? ? ? ? ? ? ? style="width: 100%"> ? ? ? <el-table-column v-for="(item,index) in tableColumn" ? ? ? ? ? ? ? ? ? ? ? ?:key="index" ? ? ? ? ? ? ? ? ? ? ? ?:prop="item.prop" ? ? ? ? ? ? ? ? ? ? ? ?:label="item.label" ? ? ? ? ? ? ? ? ? ? ? ?:width="item.width"> ? ? ? </el-table-column> ? ? </el-table> ? ? <span slot="footer" ? ? ? ? ? class="dialog-footer"> ? ? ? <el-button @click="closeDialog">關閉</el-button> ? ? </span> ? </el-dialog> </template> <script> export default { ? data () { ? ? return { ? ? ? visible: false, ? ? ? vmId: 0, ? ? ? title: "標題", ? ? ? tableData: [], ? ? ? tableColumn: [] ? ? }; ? }, ? methods: { ? ? beforeClose (done) { ? ? ? this.visible = false ? ? ? // 從DOM里將這個組件移除 ? ? ? ? // visible只是控制了顯示與隱藏 ?但是dom結構中還是存在組件 ?為了避免消耗內存必須銷毀組件 ? ? ? // setTimeout(() => { ? ? ? // ? console.log("this.$el.parentNode", this.$el.parentNode) ? ? ? // ? console.log("this.$el", this.$el) ? ? ? // ? this.$el.parentNode.removeChild(this.$el) ? ? ? // }, 500) ? ? ? setTimeout(() => { ? ? ? ? if (typeof this.onClose === "function") { ? ? ? ? ? this.onClose(this.vmId) ? ? ? ? ? done() ? ? ? ? } ? ? ? }, 500); ? ? }, ? ? closeDialog () { ? ? ? this.$refs.dialog.handleClose() ? ? } ? } }; </script> <style lang="less" scoped> </style>
2、創(chuàng)建modalTable.js文件
在組件中沒有props
接收參數,那么如何給modalTable
組件傳參,這就需要一個modalTable.js
文件去管理modalTable.vue
組件。
import Vue from "vue"; const constructor = Vue.extend(require('./modalTable.vue').default) let nId = 1 let instances = [] const ModalTable = (options) => { ? let id = 'table-' + nId++; ? options = options || {}; ? console.log("options", options); ? // 重點:綁定關閉事件 ? options.onClose = function (vmId) { ? ? ModalTable.close(vmId) ? } ? // 實列化 ? const instance = new constructor({ ? ? //重點:在這里將你傳過來的參數匹配到modalTable.vue組件的data ? ? data: { ? ? ? ...options, ? ? ? vmId: id ? ? } ? }) ? console.log("instance", instance); ? instance.id = id; ? instance.$mount(); // 掛載但是并未插入dom,是一個完整的Vue實例 ? document.body.appendChild(instance.$el) // 將dom插入body ? instance.visible = true //這里修改modalTable.vue數據中的visible,這樣modalTable組件就顯示出來 ? instances.push(instance) ? return instance }; ModalTable.close = function (vmId) { ? console.log("vmId", vmId) ? instances.forEach((instance, index) => { ? ? if (instance.id == vmId) { ? ? ? document.body.removeChild(instances[index].$el) ? ? ? instances.splice(index, 1) ? ? } ? }) } ModalTable.closeAll = function () { ? for (let i = instances.length - 1; i >= 0; i--) { ? ? instances[i].close() ? } } export default ModalTable;
3、在main.js文件中掛載vue原型鏈
import ModalTable from './components/modalTable/modalTable.js' Vue.prototype.$modalTable = ModalTable;
4、使用
最后就可以如上文的使用方法,通過原型鏈調用ModalTable
組件了。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Vite?vue3多頁面入口打包以及部署踩坑實戰(zhàn)
因為我們公司的項目是多頁面應用,不同于傳統(tǒng)單頁面應用,一個包就可以了,下面這篇文章主要給大家介紹了關于Vite?vue3多頁面入口打包以及部署踩坑的相關資料,需要的朋友可以參考下2022-05-05