需求:有个待检查项,需要上传检查后的图片,因为待检查项是不固定的,有可能就一项有可能八项,没一项都要上传图片,所以需要动态绑定数据
如何动态绑定数据,这里需要定义一个数组,然后通过v-for去循环绑定,代码如下:
data中:
form: [],
获取接口返回的数据去循环push
this.children.forEach((item) => {
const obj = {
id: item.id,
file: JSON.parse(item.photo1) || [],
sum: JSON.parse(item.photo1)
? Number(this.maxPhoto) - JSON.parse(item.photo1).length
: Number(this.maxPhoto),
radioValue: item.photo2 || 0,
}
this.form.push(obj)
})
html中:
<!-- 待检查项 -->
<view class="collapse">
<view class="collapse_title">
<view class="line"></view>
<view class="title">待检查项({
{ children.length }})</view>
</view>
<view class="collapse_content">
<view style="margin-left: 20px; margin-bottom: 10px" v-for="(item, index) in children" :key="index">
<view>{
{ Number(index) + 1 }}、{
{ item.name }}</view>
<view class="radio">
<u-radio-group v-model="form[index].radioValue" @change="radioGroupChange($event, index)">
<u-radio active-color="rgba(74, 217, 119, 1)" v-for="(item, v) in radioList" :key="v" :name="item.value">
{
{ item.name }}
</u-radio>
</u-radio-group>
</view>
<!-- 照片上传 -->
<view class="upload_box">
<view v-if="form[index].file && form[index].file.length" style="display: flex; flex-wrap: wrap">
<view v-for="(v, j) in form[index].file" :key="j" style="position: relative">
<img class="img_upload" :src="baseUrl + v.url" alt="" @click="previewImage(v.url)" />
<u-icon size="36" name="trash-fill" style="position: absolute; right: 10px"
@click="handleDelete(j, index)"></u-icon>
</view>
</view>
<view class="upload" v-for="(g, i) in form[index].sum" :key="i">
<view class="item" v-if="item.id" @click="chooseImage(index)">
<u-icon name="plus" size="36"></u-icon>
<span style="margin-left: 5px">点击上传</span>
</view>
</view>
</view>
</view>
</view>
</view>
全部代码:
<template>
<view class="">
<!-- 自定义头部状态栏 -->
<commonNavbar style="position: sticky; top: 0; z-index: 99998" :title="detailObj.taskName"&