简体   繁体   English

如何根据 javascript 或 angular 中的用户选择删除从数组上传的多个文件

[英]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}}&nbsp;ModifiedDate:{{image.lastModifiedDate}}
                <input type="checkbox" style="float:left" value="{{image.name}}" [(ngModel)]="imageChecked" 
                       [name]="image.name">&nbsp;&nbsp;
                <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}}&nbsp;ModifiedDate:{{image.lastModifiedDate}}
                <input type="checkbox" style="float:left" value="{{image.name}}" [(ngModel)]="image.imageChecked" 
                       [name]="image.name">&nbsp;&nbsp;
                <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.

相关问题 如何根据用户从下拉列表中的选择访问数组元素的其他字段,以及如何使用 javascript 删除重复项 - How to access other fields of array elements based on user selection from dropdown list and also how to remove the duplicates using javascript 如何根据用户选择使用多个广播来调用角度控制器 - how to use multiple broadcast to call angular controllers based on the user selection 如何为多个上传的文件添加/删除按钮? - How to add/remove button for multiple uploaded files? 使用javascript删除上传的文件 - Remove uploaded files using javascript 如何从特定用户删除上传的文件 - How to delete uploaded files from specific user 在 HTML/JavaScript 中根据用户选择动态删除表单元素 - Dynamically Remove a Form Element Based on User Selection in HTML/JavaScript 如何在 angular 中显示上传文件列表以及如何删除它 - How to display list of uploaded files and how to remove it in angular 如何从javascript(Angular6)中的数组中删除数组? - How to remove array from an array in javascript (Angular6)? 如何使用 Vue.Js 从数组中删除上传的多个图像,并且应该从 UI 中删除该图像? - How to remove uploaded multiple images from array using Vue.Js and it should delete that image from UI? Angular 2 如何从多个输入类型文件中删除文件? - Angular 2 How to remove file from files of input type file multiple?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM