Vue?刪除和增加自定義組件實(shí)戰(zhàn)教程

RecommendFunction.vue
<template>
<div>
<div v-for="(item, index) in dataList" :key="index" class="data-item">
<div class="group-row">
<el-form ref="item" :model="item" label-width="100px" :rules="rules" label-position="top"
style="width: 90%">
<el-form-item label="介紹" prop="title">
<el-input v-model="item.title" placeholder="請(qǐng)輸入介紹" />
</el-form-item>
<el-form-item label="品類" prop="fruitCatagory" style="margin-top: 10px;">
<div style="display: flex; align-items: center;">
<el-select v-model="item.fruitCatagory" placeholder="請(qǐng)選擇" clearable
style="width: 200px; margin-right: 10px;" @change="handleNoRepeat">
<el-option v-for="option in getAvailableFruitCatagoryOptions(index)" :key="option.value"
:label="option.label" :value="option.value" />
</el-select>
<el-input v-model="item.fruitDescription" placeholder="請(qǐng)輸入詳情" />
</div>
</el-form-item>
<el-form-item label="價(jià)格" prop="price" style="margin-top: 10px;">
<el-input v-model="item.price" placeholder="請(qǐng)輸入價(jià)格" type="number" min="0" max="100" />
</el-form-item>
</el-form>
<el-button icon="el-icon-delete" type="text" style="margin-left: 10px;" @click="deleteItem(index)" />
</div>
</div>
<div v-if="haveAbleChoice" class="data-btn" @click="addItem">
<i class="el-icon-plus" style="font-weight: bold; margin-right:5px;" />增加組件
</div>
</div>
</template>
<script>
export default {
name: 'RecommendationConfig',
props: {
fruitOptions: {
type: Array,
required: true,
default: () => []
},
dataList: {
type: Array,
required: true
},
},
data() {
return {
itemStruct: {
title: '',
fruitCatagory: '',
fruitDescription: '',
price: 50
},
rules: {
title: [{ required: true, message: '此項(xiàng)為必填', trigger: 'blur' }],
fruitCatagory: [{ required: true, message: '此項(xiàng)為必填', trigger: 'blur' }],
price: [{ required: true, message: '此項(xiàng)為必填', trigger: 'blur' }]
}
}
},
computed: {
haveAbleChoice() {
const selectedTypes = this.dataList.map(item => item.fruitCatagory);
return this.fruitOptions.some(option => !selectedTypes.includes(option.value));
}
},
methods: {
/* eslint-disable */
getAvailableFruitCatagoryOptions(index) {
const selectedTypes = this.dataList
.map((item, idx) => (idx !== index ? item.fruitCatagory : null))
.filter(type => type !== null);
return this.fruitOptions.filter(option => !selectedTypes.includes(option.value));
},
handleNoRepeat() {
this.$forceUpdate();
},
addItem() {
const selectedTypes = this.dataList.map(item => item.fruitCatagory);
const firstAvailableType = this.fruitOptions.find(option => !selectedTypes.includes(option.value));
if (firstAvailableType) {
const itemData = JSON.parse(JSON.stringify(this.itemStruct));
itemData.fruitCatagory = firstAvailableType.value;
this.dataList.push(itemData);
}
},
deleteItem(index) {
this.dataList.splice(index, 1);
}
}
}
</script>
<style scoped>
.data-btn {
width: 140px;
height: 32px;
display: flex;
justify-content: center;
align-items: center;
border: 1px dashed #d9d9d9;
background: #ffffff;
padding: 4px 15px;
}
.data-btn:hover {
cursor: pointer;
color: #409EFF;
border: 1px dashed #409EFF;
}
.data-btn:active {
/* background: #f2f0f7; */
background: #e3f5f2;
}
.data-item {
margin: 0 0 24px;
background-color: #e3f5f2;
padding: 16px;
border-radius: 8px;
}
.group-row {
display: flex;
flex-direction: row;
align-items: end;
}
</style>父組件引用:
<RecommendFunction :data-list="form.dataList" :fruit-options="form.fruitOptions" />
export default {
components: { RecommendFunction },
data() {
return {
form: {
fruitOptions: [
{ label: "蘋果", value: "0" },
{ label: "香蕉", value: "1" }
],
dataList: []
},
}
}
}到此這篇關(guān)于Vue 刪除和增加自定義組件實(shí)戰(zhàn)教程的文章就介紹到這了,更多相關(guān)vue刪除和增加組件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue-cli 2.*中導(dǎo)入公共less文件的方法步驟
這篇文章主要介紹了vue-cli 2.*中導(dǎo)入公共less文件的方法步驟,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-11-11
如何用vue-pdf包實(shí)現(xiàn)pdf文件預(yù)覽,支持分頁(yè)
這篇文章主要介紹了如何用vue-pdf包實(shí)現(xiàn)pdf文件預(yù)覽,支持分頁(yè)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03
使用Vue實(shí)現(xiàn)點(diǎn)擊按鈕小球加入購(gòu)物車動(dòng)畫
這篇文章主要為大家詳細(xì)介紹了如何使用Vue實(shí)現(xiàn)點(diǎn)擊按鈕小球加入購(gòu)物車動(dòng)畫,文中的示例代碼講解詳細(xì),有需要的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-03-03
Vue3前端生成隨機(jī)id(生成?UUID)實(shí)際運(yùn)用
前端在做增刪改查時(shí)通常會(huì)使??個(gè)唯?數(shù)值做為數(shù)據(jù)的key值,下面這篇文章主要給大家介紹了關(guān)于Vue3前端生成隨機(jī)id(生成?UUID)的相關(guān)資料,文中給出了詳細(xì)的代碼示例,需要的朋友可以參考下2024-04-04
使用provide/inject實(shí)現(xiàn)跨組件通信的方法
在 Vue 應(yīng)用中,組件間通信是構(gòu)建復(fù)雜應(yīng)用時(shí)的一個(gè)常見需求,Vue3.x 提供了provide和inject API,讓跨組件通信變得更加簡(jiǎn)潔和高效,本文將深入探討如何使用provide和inject在Vue3.x中實(shí)現(xiàn)跨組件通信,并通過(guò)示例代碼一步步進(jìn)行說(shuō)明,需要的朋友可以參考下2024-03-03

