如上图所示,点击新增添加多行,每一行都需要上传一个附件,怎么操作才能让上传的附件属于自己那一行,直接上代码
<el-row>
<el-col :span="6" style="margin-left: 20px">归属部门</el-col>
<el-col :span="6">金额</el-col>
<el-col :span="4">附件</el-col>
<el-col :span="6">备注</el-col>
</el-row>
<el-row style="margin: 20px 0" v-for="(item,key) in allocateDetail" :key="key">
<el-col :span="6" style="margin-left: 20px">
<i
class="el-icon-remove-outline delete f18"
@click="delAllocateDetail(key)"
></i>
<el-input v-model="item.deptName" @click.native="feeDeptInfo(key)" style="width: 300px"></el-input>
</el-col>
<el-col :span="6">
<el-input v-model="item.fee" style="width: 300px" @blur="waitAllocateFee"></el-input>
</el-col>
<el-col :span="4">
<el-upload
:action="uploadUrl"
ref="upload"
:before-upload="beforeAvatarUpload"
:on-success="(res,file) =>{return handleSuccess(res,file,key)}"
:on-change="handleChange"
:on-remove="(file) =>{return handleRemove(file,key)}"
:limit="1"
>
<el-button slot="trigger" size="small" type="text">上传附件</el-button>
</el-upload>
</el-col>
<el-col :span="6">
<el-input v-model="item.remark" style="width: 300px"></el-input>
</el-col>
<el-col>
<div style="height: 5px"></div>
</el-col>
</el-row>
添加的数据定义如下,这是data中的数据。allocateDetail是一个数组,里面存放的是一个个item json对象:
allocateDetail: [],
item:{
deptId:'',
deptName:'',
fee:0,
attachementPath:'',
remark:''
},
点击新增按钮,添加一行的代码如下:
<el-button type="primary" @click="addAllocateDetail()" style="margin-bottom: 15px" size="mini">新增</el-button>
addAllocateDetail() {
this.allocateDetail.push(
JSON.parse(JSON.stringify(this.item))
);
}
移除一行的代码如下:
delAllocateDetail(key) {
this.allocateDetail.splice(key, 1);
this.waitAllocateFee()
},
element-ui的on-success函数定义如下,key就是使用v-for添加行时,绑定的key:
:on-success="(res,file) =>{return handleSuccess(res,file,key)}"
//监听图片上传成功的事件
handleSuccess(response,file,key){
console.log(response)
console.log("add当前行数:"+key)
const path = response.result.url
this.allocateDetail[key].attachementPath = path
}
element-ui的on-remove函数,删除文件的代码如下:
:on-remove="(file) =>{return handleRemove(file,key)}"
handleRemove(file,key){
console.log("remove当前行数" +key)
//console.log(file)
this.allocateDetail[key].attachementPath = ''
},