[英]How to remove multiple files uploaded from an array based on user selection in javascript or angular
Hi I need to remove multiple files uploaded from an array based on user selection in javascript or angular......... I have tried with below code嗨,我需要根据 javascript 或 angular 中的用户选择删除从数组中上传的多个文件......我尝试使用以下代码
First we have some files that are uploaded in an array and are displayed in checkboxes as shown below in code首先,我们有一些文件以数组的形式上传并显示在复选框中,如下面的代码所示
<div *ngFor="let image of imagefilename" style="margin-left:10%">
<label class="container" style="font-size:14px">
{{image.name}} ModifiedDate:{{image.lastModifiedDate}}
<input type="checkbox" style="float:left" value="{{image.name}}" [(ngModel)]="imageChecked"
[name]="image.name">
<span class="checkmark"></span> <br><br>
</label>
</div>
<button *ngIf="imagefilename.length" class="btn btn-danger" type="submit" (click)="resetimage(imagefilename)">Reset Selected files</button>
The user will click on the checkboxes that are to be removed and then click on the button displayed and用户将单击要删除的复选框,然后单击显示的按钮
it calls a function as displayed below它调用 function 如下所示
resetimage(imageName:any) {
for(var i = 0; i<this.imagefilename.length;i++){
if(this.imageChecked){
this.imagefilename.splice(i,1);
}
}
}
So in this function, only the first file in the array is removed although the user has selected multiple files to remove.因此,在此 function 中,尽管用户选择了多个要删除的文件,但仅删除了数组中的第一个文件。
So please help me if there is any solution.因此,如果有任何解决方案,请帮助我。
Expected result: to remove multiple files uploaded from an array based on user selection in javascript or angular预期结果:根据 javascript 或 angular 中的用户选择删除从数组上传的多个文件
Actual result: Only first file is removed even though the user has selected multiple files.(as per my code)实际结果:即使用户选择了多个文件,也只删除第一个文件。(根据我的代码)
your property imageChecked
is not binded with image.您的属性
imageChecked
未与图像绑定。 You should add property imageChecked
in image
and modify html like below.您应该在
image
中添加属性imageChecked
并修改 html,如下所示。
<div *ngFor="let image of imagefilename" style="margin-left:10%">
<label class="container" style="font-size:14px">
{{image.name}} ModifiedDate:{{image.lastModifiedDate}}
<input type="checkbox" style="float:left" value="{{image.name}}" [(ngModel)]="image.imageChecked"
[name]="image.name">
<span class="checkmark"></span> <br><br>
</label>
</div>
<button *ngIf="imagefilename.length" class="btn btn-danger" type="submit" (click)="resetimage(imagefilename)">Reset Selected files</button>
And you code should be like this你的代码应该是这样的
resetimage(imageName:any) {
for(var i =this.imagefilename.length;i--){
if(this.imagefilename[i].imageChecked){
this.imagefilename.splice(i,1);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.