Vue動態(tài)組件表格的實(shí)現(xiàn)代碼
更新時(shí)間:2022年10月16日 11:51:47 作者:生活在北極的企鵝
這篇文章主要介紹了Vue動態(tài)組件表格的實(shí)現(xiàn)代碼,包括框架結(jié)構(gòu)組件,文中還給大家封裝了幾個(gè)組件,有按鈕組件、圖片組件、滑動開關(guān),結(jié)合示例代碼給大家詳細(xì)講解,需要的朋友可以參考下
Vue組件
數(shù)據(jù)源
//這里是HTML內(nèi)容 這里通過下面的引入框架結(jié)構(gòu)把數(shù)據(jù)源傳到框架中 還有匹配項(xiàng)
<Mytable :configList="configList" :configData="configData"></Mytable>
// 引入結(jié)構(gòu)組件
import myCard from "./components/card";
// 注冊組件
components: { myCard },
data() {
return {
// 這里定義數(shù)據(jù)列表
configList: [
// 這里是數(shù)據(jù)有源
{
text: "111",
img: "/02.jpg",
tap: "標(biāo)簽1",
switch: true,
button: "按鈕1",
},
],
// 這里定義匹配項(xiàng)
configData: [
{
table: "貨幣",
porp: "text",
name: "MyText",
},
{
table: "圖片",
porp: "img",
name: "Myimg",
},
{
table: "標(biāo)簽",
porp: "tap",
name: "tag",
},
{
table: "滑動開關(guān)",
porp: "switch",
name: "Btn",
funName: (row) => {
this.mySwitch(row);
},
},
{
table: "按鈕",
porp: "button",
name: "Mybtn",
// 如果組件中需要?jiǎng)討B(tài)綁定事件 在這里設(shè)置
funName: (row) => {
this.myBtn(row);
},
},
]
}
]框架結(jié)構(gòu)組件
<div>
// 這里接受數(shù)據(jù)組件傳遞過來的數(shù)據(jù)
<el-table :data="configList">
<!-- 文字表格區(qū)間 -->
// 這里進(jìn)行循環(huán)渲染數(shù)據(jù)
<el-table-column
align="center"
v-for="(item, index) in configData"
:key="index"
:label="item.table"
>
<!-- 組件 -->
// 動態(tài)組件 這里可以進(jìn)行標(biāo)簽 按鈕 圖片等 的別的組件進(jìn)行循環(huán)渲染到表格中
<template slot-scope="scope">
<component
:is="item.name"
:value="scope.row"
// 把每一項(xiàng)有點(diǎn)擊事件進(jìn)行傳參
@parentFun="fun(item.funName, scope.row)"
></component>
</template>
</el-table-column>
</el-table>
</div>
// 這里引用自己封裝的動態(tài)組件
import Myimg from "@/components/toConfigure/img.vue";
import tag from "@/components/toConfigure/tag.vue";
import Btn from "@/components/toConfigure/switch.vue";
import MyText from "@/components/toConfigure/text.vue";
import Mybtn from "@/components/toConfigure/button.vue";
// 進(jìn)行注冊組件
components: {
Myimg,
tag,
Btn,
MyText,
Mybtn,
},
// 這里進(jìn)行判斷每個(gè)按鈕時(shí)候有點(diǎn)擊事件 沒有為空
methods: {
fun(funName, row) {
if (funName) {
funName(row);
}
},
},
// 這里接受傳過來的數(shù)據(jù)
props: {
configData: {
type: Array,
},
configList: {
type: Array,
},
},這里我自己封裝了幾個(gè)組件
按鈕組件
<template>
// 這里是按鈕
<el-button round @click="btn">{{ value.button }}</el-button>
</template>
<script>
export default {
// 接受組件傳過來的值
props: {
value: {
type: Object,
},
},
// 這里進(jìn)行綁定動態(tài)點(diǎn)擊事件
methods: {
btn() {
// 這里接受傳參
this.$emit("parentFun");
},
},
};
</script>
<style></style>圖片組件
<template>
<div>
<el-image
style="width: 100px; height: 100px"
:src="Myimg"
// 使用時(shí)候把這條注釋刪除 這個(gè)屬性是點(diǎn)擊圖片放大 不需要可以刪除
:preview-src-list="[Myimg]"
></el-image>
</div>
</template>
<script>
export default {
props: {
value: {
type: Object,
},
},
computed: {
Myimg() {
if (this.value.img.length > 0) {
// "@/assets/images" 這個(gè)是圖片的根路徑 加上 傳遞過來的數(shù)據(jù)中圖片的名字
return require("@/assets/images" + this.value.img);
} else {
return;
}
},
},
};
</script>
<style></style>滑動開關(guān)
<template>
<div>
<el-switch
v-if="this.value.switch !== undefined"
v-model="value.switch"
active-color="#13ce66"
inactive-color="#ff4949"
@change="switchClick"
></el-switch>
</div>
</template>
<script>
export default {
props: {
value: {
type: Object,
},
},
methods: {
switchClick() {
// 事件分發(fā)
this.$emit("parentFun", this.value);
},
},
mounted() {
// console.log(this.value.button);
},
};
</script>
<style></style>tap組件
<template>
<div>
<el-tag v-if="value.tap.length > 0">{{ value.tap }}</el-tag>
</div>
</template>
<script>
export default {
props: {
value: {
type: Object,
},
},
};
</script>
<style></style>text組件
<template>
<div>
{{ value.text }}
</div>
</template>
<script>
export default {
props: {
value: {
type: Object,
},
},
};
</script>
<style></style>到此這篇關(guān)于Vue動態(tài)組件 表格的文章就介紹到這了,更多相關(guān)Vue動態(tài)組件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解vue3中setUp和reactive函數(shù)的用法
這篇文章主要介紹了vue3函數(shù)setUp和reactive函數(shù)的相關(guān)知識及setup函數(shù)和reactive函數(shù)的注意點(diǎn),通過具體代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2021-06-06
vue?element表格某一列內(nèi)容過多,超出省略號顯示的實(shí)現(xiàn)
這篇文章主要介紹了vue?element表格某一列內(nèi)容過多,超出省略號顯示的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01
Vue3?封裝一個(gè)支持輸入和單/多選InputSelect組件-Antd詳解
Antd的Select組件默認(rèn)不支持作為輸入框使用或手動添加選項(xiàng),為了實(shí)現(xiàn)這一功能,我們封裝了一個(gè)通用組件,支持單選和多選模式,并允許用戶在組件失焦時(shí)手動輸入選項(xiàng),主要通過定義searchText存儲輸入數(shù)據(jù),感興趣的朋友跟隨小編一起看看吧2024-09-09
VUE使用echarts?5.0以上版本渲染器未導(dǎo)入錯(cuò)誤問題
這篇文章主要介紹了VUE使用echarts?5.0以上版本渲染器未導(dǎo)入錯(cuò)誤問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-06-06
Vue中watch監(jiān)聽第一次不觸發(fā)、深度監(jiān)聽問題
這篇文章主要介紹了Vue中watch監(jiān)聽第一次不觸發(fā)、深度監(jiān)聽問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10
vue大屏自適應(yīng)的實(shí)現(xiàn)方法(cv就能用)
最近在用VUE寫大屏頁面,遇到屏幕自適應(yīng)問題,下面這篇文章主要給大家介紹了關(guān)于vue大屏自適應(yīng)的實(shí)現(xiàn)方法,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-06-06

