想要弹窗打开时根据返回不同个数的el-collapse-item也就是StepSelectOptions数组个数,展开不同的多选框或者单选框,选框类型由每行的下拉框进行判断展示,目前的问题是 @click="Save">确 定的时候选框的值没有动态获取,选中的时候也没有动态分离,需要改 两个v-model=""部分和Save赋值部分,源代码有错所以参考即可,可能需要很大改动
<el-dialog title="配置场景" :visible.sync="scenedialog" v-loading="dialogLoading" width="60%">
<span>{{sceneTitle}}</span>
<!-- accordion -->
<el-collapse v-model="activeNames" @change="handleCollapseChange">
<el-collapse-item v-for="(item, index) in StepSelectOptions" :key="item.id" :title="item.name"
:name="index.toString()">
<!-- 复选框列表 -->
<el-select v-model="isGameID[index]" placeholder="请选择" @change="selectOptionsChangeGame(index)">
<el-option v-for="item in GameSelectOptions" :key="item.value" :label="item.label" :value="item.value" />
</el-select>
<br />
<template v-if="isGameID[index] === '1'">
<el-checkbox v-for="subItem in checkboxItems"
v-model="selectedIds[subItem.id]" :key="subItem.id" :label="subItem.title">
{{ subItem.title }}
</el-checkbox>
</template>
<template v-if="isGameID[index] === '2'">
<el-radio v-for="(subItem, subIndex) in checkboxItemsGame"
v-model="selectedIdsGame[index]" :key="subItem.id" :label="subItem.name">
{{ subItem.name }}
</el-radio>
</template>
</el-collapse-item>
</el-collapse>
<span slot="footer" class="dialog-footer">
<el-button @click="levelClose">取 消</el-button>
<el-button type="primary" @click="Save">确 定</el-button>
</span>
</el-dialog>
//场景
scenedialog: false,
dialogLoading: false,
scenedialogForm: {},
sceneTitle: '',
StepSelectOptions: [],
checkboxItems: [],
checkboxItemsGame: [],
activeNames: [], // 活动名称,用于控制折叠面板的展开和关闭
// 存储选中的id
selectedIds: [],// 存储每个 el-collapse-item 的复选框选中状态
selectedIdsGame: [],// 存储每个 el-collapse-item 的单选框选中状态
isGameID: [], // 每个折叠面板的下拉选择框的值
collectedData: [], // 存储每个折叠面板数据的对象数组
GameSelectOptions: [{
value: '1',
label: '场景',
},
{
value: '2',
label: '游戏',
},
]
handleCollapseChange(activeNames) {
// activeNames 是一个数组,包含当前激活的 collapse-item 的 name
// 如果您的 collapse 使用了 accordion 模式,这里只有一个元素
this.activeIndex = activeNames[0];
// this.activeIndex = activeNames;
},
//场景
Save() {
// 收集选中的checkbox项的ID
let index = this.activeIndex
console.log(index,this.checkboxItemsGame)
console.log(this.selectedIdsGame[index])//name
this.selectedIds =
this.checkboxItems.filter((item, index) => this.selectedIds[index])
.map(item => item.id);
let aa = this.StepSelectOptions[this.activeNames];
this.selectedIdsGame =
this.checkboxItemsGame
.filter(item => this.selectedIdsGame.some(selectedName => item.name === selectedName))
.map(item => item.id);
console.log(this.selectedIds, this.selectedIdsGame)//id
this.StepSelectOptions.forEach((step, index) => {
// 假设每个 StepSelectOptions 的 item 都有一个 id 和 name
let stepItem = this.StepSelectOptions[index];
let resourceType = this.isGameID[index];
let selectedSceneIds;
if (resourceType === '1') {
// 如果资源类型为 '1',使用 this.selectedIds 收集选中的 scene_ids
// selectedSceneIds = this.selectedIds[index].join(',');
selectedSceneIds = this.selectedIds[index];
} else if (resourceType === '2') {
selectedSceneIds = this.selectedIdsGame[index];
}
// 创建对象
let dataObj = {
step_name: stepItem.name,
resource_type: resourceType,
scene_ids: selectedSceneIds
};
// 将对象添加到 collectedData 数组
this.collectedData.push(dataObj);
});
console.log('this.collectedData', this.collectedData)
},