最近剛上手vue,使用vue和Element-UI做了一個簡單的多條件篩選列表,功能如下圖:
“篩選條件1”和“篩選條件2”都是組件SelectionConditionList生成的,點擊“新建任務”會增加一個“篩選條件n”。
當我增加多條“篩選條件”后,每條篩選條件都選中一個條件類型,如下圖:
此時,當我刪除“篩選條件2”時,詭異的事情發(fā)生了:
可以看到,序號確實是“篩選條件2”沒有了,但是內容“篩選條件2”的“注冊時間”仍然存在,而消失的是最后一條篩選條件“消費點位”。
從console.log的打印信息也能看出,實際是序號4的內容被刪除了。我的代碼自己查了好多遍了,看不出問題出在什么地方,我把代碼貼出來,求大神幫忙理理是哪里出錯了?
1,主文件 UserSelectionTask.vue
<template>
<div class="content-body">
<div class="line-title">優(yōu)惠券系統(tǒng)/添加用戶篩選任務</div>
<el-form ref="form" :model="form" label-width="100px">
<el-row :gutter="10">
<el-col :span="12">
<el-form-item label="任務名稱">
<el-input v-model="form.inputTaskName"></el-input>
</el-form-item>
</el-col>
<el-col :span="12">
<el-form-item label="參考任務">
<el-select v-model="form.selectRefTask" placeholder="請選擇" @change="selectRefTaskSC">
<el-option
v-for="item in refTaskOptions"
:label="item.label"
:value="item.value"
:key="item.value">
</el-option>
</el-select>
</el-form-item>
</el-col>
</el-row>
<el-row :gutter="10">
<el-col :span="12">
<el-form-item label="驗證手機號碼"><el-input v-model="form.inputVerifyPhoneNumber"></el-input></el-form-item>
</el-col>
<el-col :span="12">
<el-button plain icon="el-icon-circle-plus-outline" @click="addTaskVerifyPhone">加入</el-button><el-button plain icon="el-icon-close">刪除</el-button><el-button plain icon="el-icon-delete">清空</el-button>
</el-col>
</el-row>
<el-row :gutter="10">
<el-col :span="20">
<el-form-item label=" ">
<el-input type="textarea" :rows="6" readonly v-model="form.phoneNumbersTextArea"></el-input>
</el-form-item>
</el-col>
</el-row>
<div>
<v-selection-condition-list v-for="item in selectionConditionList" :key="item.id" :condition-index="item.index" :dom-show="item.show" @delete="deleteSelectionCondition"></v-selection-condition-list>
</div>
<div class="div_center">
<el-button type="warning" plain>取消任務</el-button> <el-button type="primary" plain @click="addNewSelectionTask">新建任務</el-button> <el-button type="primary" plain>提交任務</el-button>
</div>
</el-form>
</div>
</template>
<script>
/* eslint-disable */
import vSelectionConditionList from './SelectionConditionList'
export default
{
data()
{
return {
selectionConditionList:[ {index:1,show:false}],
form: {
inputTaskName: '',
selectRefTask: '',
inputVerifyPhoneNumber: '',
phoneNumbersTextArea: ''
},
refTaskOptions: [
{
value: '17年10月W1任務',
label: '17年10月W1任務'
},
{
value: '18年1月W2任務',
label: '18年1月W2任務'
},
{
value: '18年2月W3任務',
label: '18年2月W3任務'
}]
}
},
methods: {
// 添加驗證手機號碼
addTaskVerifyPhone(){
},
// 參考條件選中值發(fā)生變化時觸發(fā)
selectRefTaskSC(re)
{
// re: 目前的選中值
this.form.inputTaskName = re;
},
// 添加新的用戶篩選條件
addNewSelectionTask(){
var len = this.selectionConditionList.length;
this.selectionConditionList.push({index:len+1});
// console.log(this.selectionConditionList)
},
// 刪除用戶篩選條件
deleteSelectionCondition(index){
console.log("delete..."+index);
this.selectionConditionList.splice(index-1,1);
// console.log(this.selectionConditionList);
var len = this.selectionConditionList.length;
/* for(var i=0;i<len;i++){
this.selectionConditionList[i].index = i+1;
}*/
}
},
components:{
vSelectionConditionList
}
}
</script>
<style>
.div_center {
text-align: center;;
width:100%;
margin:0 auto;
}
</style>
2,篩選條件組件文件 SelectionConditionList.vue
<template>
<div class="content-body">
<el-row :gutter="10">
<el-col :span="4">
<div class="text select-txt">篩選條件{{ myConditionIndex }} <i class="el-icon-close" @click="deleteCondition" v-show="myDomShow"></i></div>
</el-col>
<el-col :span="8">
<el-form-item label="邏輯關系" v-show="myDomShow">
<el-select v-model="form.logicType" placeholder="請選擇" @change="logicSC" class="logic-select">
<el-option
v-for="item in logicTypeOptions"
:label="item.label"
:value="item.value"
:key="item.value">
</el-option>
</el-select>
</el-form-item>
</el-col>
<el-col :span="12">
<el-form-item label="條件類型">
<el-select v-model="form.conditionType" placeholder="請選擇" @change="conditionTypeSC">
<el-option
v-for="item in conditionTypeOptions"
:label="item.label"
:value="item.value"
:key="item.value">
</el-option>
</el-select>
</el-form-item>
</el-col>
</el-row>
<el-row :gutter="10">
<el-col :span="12">
<el-form-item label="條件類型">
<el-input v-model="form.inputConditionName"></el-input>
</el-form-item>
</el-col>
<el-col :span="12">
</el-col>
</el-row>
</div>
</template>
<script>
/* eslint-disable */
export default
{
props:{
conditionIndex:{type:Number}, domShow:{type:Boolean,default:true},inputName:{type:String}
},
data()
{
return {
myConditionIndex:this.conditionIndex,
myDomShow:this.domShow,
form: {
conditionType:'',
logicType:'',
inputConditionName:''
},
logicTypeOptions: [{
value: '且',
label: '且'
}, {
value: '和',
label: '和'
}, {
value: '排除',
label: '排除'
}],
conditionTypeOptions: [{
value: '樓宇點位',
label: '樓宇點位'
}, {
value: '注冊時間',
label: '注冊時間'
}, {
value: '最近消費商品',
label: '最近消費商品'
}, {
value: '消費點位',
label: '消費點位'
}, {
value: '付費時間',
label: '付費時間'
}, {
value: '消費城市',
label: '消費城市'
}, {
value: '企業(yè)導入',
label: '企業(yè)導入'
}, {
value: '已有任務',
label: '已有任務'
}, {
value: '杯均價',
label: '杯均價'
}, {
value: '消費頻次',
label: '消費頻次'
}, {
value: '導入用戶',
label: '導入用戶'
}]
}
},
destroyed()
{
console.log("name.."+this.form.inputConditionName+"..id.."+this.conditionIndex);
},
methods: {
// 選擇篩選條件
conditionTypeSC(re)
{
// re: 目前的選中值
this.form.inputConditionName = re;
console.log("name.."+this.form.inputConditionName+"..id.."+this.conditionIndex);
},
// 選擇邏輯關系
logicSC(re){
},
// 刪除此篩選條件
deleteCondition(){
this.$emit('delete',this.myConditionIndex);
}
},
watch:{
conditionIndex(val){
this.myConditionIndex = val;
},
domShow(val){
this.myDomShow = val;
}
}
}
</script>
<style>
.logic-select {
width:100px;
}
</style>
全部文件網(wǎng)盤下載鏈接: https://pan.baidu.com/s/1mjM7Jjq 密碼: yspj
你每次刪除操作改變的只是 vfor list的數(shù)據(jù),并不是子組件里面form的數(shù)據(jù),每次刪除list是按所需邏輯刪除 長度減1。form隨組件渲染,只是渲染的數(shù)量減1 所以只會去處原form的最后一個。不知道樓主能不能理解。
r-
不太明確需求,不知道行不行:
可將form數(shù)據(jù)放放在selectionConditionList里 :
selectionConditionList:{
form:{...}
}
VselectionConditionList組件里:
{
...
props:{form:{type:Object}},
...
watch:{
'form' : function(n){
// 將form 數(shù)據(jù) 和 conditionIndex(傳進來的)傳給父組件
this.$emit('update',{form,index:this.index});
}
}
};
北大青鳥APTECH成立于1999年。依托北京大學優(yōu)質雄厚的教育資源和背景,秉承“教育改變生活”的發(fā)展理念,致力于培養(yǎng)中國IT技能型緊缺人才,是大數(shù)據(jù)專業(yè)的國家
北大青鳥中博軟件學院創(chuàng)立于2003年,作為華東區(qū)著名互聯(lián)網(wǎng)學院和江蘇省首批服務外包人才培訓基地,中博成功培育了近30000名軟件工程師走向高薪崗位,合作企業(yè)超4
中公教育集團創(chuàng)建于1999年,經(jīng)過二十年潛心發(fā)展,已由一家北大畢業(yè)生自主創(chuàng)業(yè)的信息技術與教育服務機構,發(fā)展為教育服務業(yè)的綜合性企業(yè)集團,成為集合面授教學培訓、網(wǎng)
達內教育集團成立于2002年,是一家由留學海歸創(chuàng)辦的高端職業(yè)教育培訓機構,是中國一站式人才培養(yǎng)平臺、一站式人才輸送平臺。2014年4月3日在美國成功上市,融資1
曾工作于聯(lián)想擔任系統(tǒng)開發(fā)工程師,曾在博彥科技股份有限公司擔任項目經(jīng)理從事移動互聯(lián)網(wǎng)管理及研發(fā)工作,曾創(chuàng)辦藍懿科技有限責任公司從事總經(jīng)理職務負責iOS教學及管理工作。
浪潮集團項目經(jīng)理。精通Java與.NET 技術, 熟練的跨平臺面向對象開發(fā)經(jīng)驗,技術功底深厚。 授課風格 授課風格清新自然、條理清晰、主次分明、重點難點突出、引人入勝。
精通HTML5和CSS3;Javascript及主流js庫,具有快速界面開發(fā)的能力,對瀏覽器兼容性、前端性能優(yōu)化等有深入理解。精通網(wǎng)頁制作和網(wǎng)頁游戲開發(fā)。
具有10 年的Java 企業(yè)應用開發(fā)經(jīng)驗。曾經(jīng)歷任德國Software AG 技術顧問,美國Dachieve 系統(tǒng)架構師,美國AngelEngineers Inc. 系統(tǒng)架構師。