【问题标题】:How to implement data delete function in Angular如何在Angular中实现数据删除功能
【发布时间】:2020-07-13 22:31:12
【问题描述】:

在我们网站的编辑页面中,我有一个垫卡/工作区列表,每个工作区的顶角都有编辑图标。通过单击该编辑图标,它会将您带到该工作区的编辑页面,并且我在其中有一个删除按钮,当它单击时会显示一个对话框,并允许用户决定他们是否真的要删除该工作区。

不太确定如何处理 API 调用

 .name.length > 0 && this.description.length > 0) {
      //map
      this.workspace.name = this.name;
      this.workspace.description = this.description;
      this.workspace.type = WorkspaceType.public; //all workspaces that are created are public by default

      //create
      this.workspaceService.createWorkspace(this.workspace).subscribe(workspace => {
        this.saving = false;
        this.gotoManage();
        this.snackbar.open(this.workspace.name+ " has been created!!", "", {duration :2500});
      }, () => this.saving = false);

    }
  }

【问题讨论】:

    标签: html css angular function filter


    【解决方案1】:

    你可以这样做:

    openModalConfirmationDialog(content1: TemplateRef<any>, row, content2:TemplateRef<any>, content3: TemplateRef<any>) {
    // you can store row or data selected until page is active
      this.contetntIWantToDelete = row;
    // Make availale data in popup for confirmation
        this.modalService.open(content1).then(result => {
            if (result) {
            // load the data (row) which is to be deleted
                this.modalService.open(content2);
            // update delete
         this.yourDeleteinfoAPIcustomFunction('/name1/requestedWorkspaceDelete', row.id, content3);
            }
        });
    }
    
    yourDeleteinfoAPIcustomFunction(url, param, content1: TemplateRef<any>) { 
    // This function have success and error messages based on what is selected for deletion
     this.http.get(baseUrl + url, options)
            .subscribe(r => {
                    if (r != null) {
                       // success delete operation
                    } else {
                        // failed delete op.
                    }
                }
    

    }

    在您的 html 中删除按钮:

    (click)="openModalConfirmationDialog(modalConfirm, row, modalDeleteProgress, modalDeleteSuccess)"
    

    在上面的函数中:我有这组在函数中传递的对话框,正如你提到的,你想从对话框中得到确认:

    <ng-template #modalConfirm>
        <custom-directive>
            <p>Confirm delete operation</p>
            <p>Delete workspace Id: <B>{{workspace.id}}
                <br></B><B>{{moreDetails}}</B>
        </custom-directive>
    </ng-template>
    
    <ng-template #modalDeleteProgress>
        <custom-directive type="warning" warningTitle="Delete in progress">
        </custom-directive>
    </ng-template>
    
     <ng-template #modalDeleteSuccess>
        <custom-directive type="success" successTitle="Success" btn="Close">
            <div>workspace ID: {{workspace.id}} has been deleted</div>
        </custom-directive>
    </ng-template>
    

    【讨论】:

    • 我知道你很难理解我的项目背后的业务,但我对你的代码有点困惑。我的朋友告诉我在上面的代码中做一些逻辑,当我与你的代码进行比较时,我似乎很难理解它。我添加了我们如何创建新工作区的代码。
    • 哦,是的,实际上我没有最简单的例子,但让我进一步解释到底发生了什么,我会更新我的答案
    • 好的.. 因为我们使用来自数据库的 API 调用,而我以前从未这样做过,所以我不知道该怎么做。我从我们在对话框组件中创建新工作区的方式中复制了一些代码,但不确定如何从那里删除工作区。你还认为我需要一个新的 api 调用,或者我可以使用创建新工作区中的相同代码(第一个代码框)
    • 我更新了 ans 的更多详细信息,如果您在 anser 中有类似“getAllWorkspaces()”的内容,请确保您需要一个新的 API 来删除,否则如何为工作区更新您的记录/name1/requestedWorkspaceDelete'是删除操作API,这里是一个简单的例子,但是你应该有自己的删除操作:stackblitz.com/edit/mat-dialog-example-delete-confirm
    • 谢谢你的帮助
    猜你喜欢
    • 2019-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-22
    • 2023-03-13
    • 1970-01-01
    相关资源
    最近更新 更多