简体   繁体   English

当我单击 Angular 中的删除按钮时,我的表格行和列消失了?

[英]My table rows and columns disappear when I click delete button in Angular?

When I click the delete button to delete a row using delete UserById API, all my rows and columns disappear but when I refresh the page I get them back and the deleted row remains deleted(which is good) but I don't want my other data to disappear upon clicking delete and have to refresh to get them back.当我单击删除按钮使用 delete UserById API 删除一行时,我的所有行和列都消失了,但是当我刷新页面时,我将它们取回并且删除的行仍然被删除(这很好),但我不想要我的其他行点击删除后数据消失,必须刷新才能恢复。 How do I fix this?我该如何解决?

type script键入脚本

export class SearchDeleteComponent implements OnInit {

public deleteUser(num:number){
  let resp=this.service.deleteUser(num);
  resp.subscribe((data)=>this.users=data);
}

html html

<div class="container">

    <div class="row col-md-6 col-md-offset-2 custyle">
    <table class="table table-striped custab">
    <thead>
        <tr>
            
            <th>Full Name</th>
            <th>Date Of Birth</th>
            <th>Gender</th>
           
        </tr>
    </thead>
    <tbody>
        <tr *ngFor="let user of users">
          
            <td>{{user.firstName+ '  '+ user.lastName}}</td>
            <td>{{user.dob}}</td>
            <td>{{user.gender}}</td>
         
            <td class="text-center"><button class="btn btn-danger btn-xs" (click)="deleteUser(user.num)"><span class="glyphicon glyphicon-remove"></span> Delete</button></td>
           
          </tr>
    </tbody>
    </table>
    </div>

Delete API back-end *删除 API 后端*


@Stateless
public class ManageUserBean  {
    
    
    
        
           @PersistenceContext(unitName = "users")
            private EntityManager entityManager;
           

   @TransactionAttribute(TransactionAttributeType.REQUIRED)
            public void deleteUser(int num) throws Exception {
                try {
                    System.out.println("num : " + num);
                    Query q = entityManager.createQuery("SELECT u FROM User u where u.num = :num");
                    q.setParameter("num", num);
                     
                    entityManager.remove(entityManager.merge(q.getSingleResult()));
                   entityManager.flush();
                } catch (Exception e) {
                    System.out.println(e);
                }

            }

@Stateless
@Path("/{versionID}/example/")
public class ExampleAPI {

    @EJB
    ManageUserBean manageUserBean;
    

    
         @Context
        private UriInfo context;
    
        @DELETE
        @Path("/users/delete/{num}")
        @Produces(MediaType.APPLICATION_JSON)
        public void deleteMessage(@PathParam("num") int num) throws Exception {
           manageUserBean.deleteUser(num);
        }
     

I should add a return type and return the allUsers method in the delete function.我应该添加一个返回类型并在删除函数中返回 allUsers 方法。

Edited the following code:编辑了以下代码:

@TransactionAttribute(TransactionAttributeType.REQUIRED)
            public List<User> deleteUser(int num) throws Exception {
                try {
                    System.out.println("num : " + num);
                    Query q = entityManager.createQuery("SELECT u FROM User u where u.num = :num");
                    q.setParameter("num", num);
                    
                    
                     
                    entityManager.remove(entityManager.merge(q.getSingleResult()));
                    entityManager.flush();
                    return (List<User>) q.getResultList();

                } catch (Exception e) {
                    System.out.println(e);
                    return null;
                }

  @DELETE
        @Path("/users/delete/{num}")
        @Produces(MediaType.APPLICATION_JSON) 
        public List<User> deleteMessage(@PathParam("num") int num) throws Exception {
            manageUserBean.deleteUser(num);
            return manageUserBean.allUserss();
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 单击按钮时如何删除表格中的行? 使用Javascript - How to delete a row in the table when I click on a button? Using Javascript 当我单击表格中的任何单元格时,如何强制唯一单元格保持不变而不消失? - How i can force the unique Cell to stay and not disappear when I click any cells in my table? 当我单击表单按钮时,为什么我的DOM更改消失了? - Why do my DOM changes disappear when I click a form button? 当我将鼠标悬停在我的按钮上时,我的下拉菜单消失了 - My dropdown disappear when I hover on my button 单击一次按钮后无法删除使用指令创建的表的选定行 - Unable to delete selected rows of a table created with directive on single button click Angular 8:如何在行单击时切换多行并在单击 Angular Material Table 的单个按钮上展开所有行? - Angular 8: How to toggle multiple rows on row click and to expand all rows on a single button click on Angular Material Table? 在 Angular8 中单击按钮隐藏和显示 html 表中的多列 - Hide and Show Multiple columns in html table with button click in Angular8 单击按钮时如何消失 canvas 文本 - How to disappear canvas text when click on button 为什么单击菜单时我的轮播消失了? - Why my carousel disappear when click on menu? 如何在单击按钮时让我的 HTML 元素之一消失? - How do I get one of my HTML elements to disappear at the click of a button?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM