当我删除一个项目时使用 'DataView' Wicket 排除整个列表

Using the 'DataView' Wicket when I delete one item the entire list is excluded

我在 'DataView' 做 'Wicket' 时遇到问题...我向此 DataView 添加了一些文件,到目前为止一切顺利,当我删除文件时就是这个问题,如果我删除任何不是第一条规则的文件...但是如果我尝试删除第一个项目,它会依次排除所有其他低...有人看到了什么吗?

遵循我的代码:

  //Principal panel

   private class PanelPrincipalAnexo extends WebMarkupContainer
   {
    public PanelPrincipalAnexo(String id)
    {
        super(id);

        formUpload = getFormUpload();
        add(formUpload);
        formUpload.add(getDataViewAnexos("anexos"));
    }
  }




  //creating the FormUpload
  private FileUploadForm getFormUpload() {
    return new FileUploadForm("formUpload", new PropertyModel<List<FileUpload>>(this, "uploads"));
  }





  private DataView<ProgramaAnexo> getDataViewAnexos(String id) {

    return new DataView<ProgramaAnexo>(id, new AnexoProvider()) {
        private static final long serialVersionUID = 1L;

        @Override
        protected void populateItem(Item<ProgramaAnexo> item) {

            item.add(new Label("tipoArquivo"));
            item.add(new Label("nomeAnexo"));
            item.add(new Label("tamanhoArquivoEmMB")); 
            item.add(getButtonRemove(item));

        }
    };
}







 public AjaxSubmitLink getButtonRemove(Item<ProgramaAnexo> item) {
    AjaxSubmitLink button = new AjaxSubmitLink("btnRemoverBem", form) {
        private static final long serialVersionUID = 1L;
        @Override
        public void onSubmit(AjaxRequestTarget target, Form form) {
            actionRemoverKit(target,item);
        }
    };
    return button;
}





 private void actionRemoverKit(AjaxRequestTarget  target,Item<ProgramaAnexo> item) {

    ProgramaAnexo bemRemove=item.getModelObject();
    int cont=0;
    for(ProgramaAnexo bem:list)
    {            
        if(bemRemove.getBem().getId().intValue()==bem.getBem().getId().intValue())
        {  
            listaBensSelecionadosDireito.remove(cont);
            break;
        }
    }        
    target.add(panelPrincipalAnexo);
}




   THE HTML

<table width="98%" class="table table-hover"
                                style="table-layout: fixed;">
                                <thead>
                                    <tr>
                                        <th width="16%"></th>
                                        <th width="16%">Tipo</th>
                                        <th width="29%">Arquivo</th>
                                        <th width="10%">Tamanho</th>
                                        <th width="25%" class="text-left">Ações</th>
                                        <th width="4%"></th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <tr  wicket:id="anexos">
                                        <td width="16%"></td>
                                        <td width="16%"><span wicket:id="tipoArquivo"></span></td>
                                        <td width="29%"><span wicket:id="nomeAnexo"></span></td>
                                        <td width="10%"><span wicket:id="tamanhoArquivoEmMB"></span></td>
                                        <td width="25%" class="text-left">

                                            <button wicket:id="btnExcluirAnexo" 
                                                    title="Excluir" 
                                                    class="btn btn-danger btn-sm"
                                                    id="btnRemoverBem"">
                                                            <i class="fa fa-minus"></i>
                                            </button>
                                        </td>
                                        <td width="4%"></td>
                                    </tr>
                                </tbody>
                            </table>

您的代码有点混乱。这是您要找的吗?

页数:

public class TestPage4 extends WebPage {

    FeedbackPanel feedbackPanel;
    List<Customer> customers = new ArrayList<Customer>();
    boolean listAlreadySet = false;
    CustomerProvider customerProvider = new CustomerProvider();

    public TestPage4(final PageParameters parameters) {

        feedbackPanel = new FeedbackPanel("feedback");
        feedbackPanel.setOutputMarkupId(true);
        add(feedbackPanel);

        Form<Void> form = new Form<Void>("form");
        add(form);

        addFormComponents(form);
    }

    private void addFormComponents(final Form<Void> form) {


        if (!listAlreadySet){
            customerProvider = new CustomerProvider();
        } else {
            customerProvider = new CustomerProvider(customers);
        }
        DataView<Customer> dataView = new DataView<Customer>("table", customerProvider) {

            @Override
            protected void populateItem(final Item<Customer> item) {
                final Customer customer = item.getModelObject();
                item.add(new Label("id", customer.getId()));
                item.add(new Label("lastname", customer.getLastName()));
                item.add(new Label("firstname", customer.getFirstName()));
                item.add(new Label("age", customer.getAge()));
                AjaxFallbackLink<String> removeLink = new AjaxFallbackLink<String>("remove", Model.of(customer.getRemove())) {
                    @Override
                    public void onClick(AjaxRequestTarget target) {
                        customers = customerProvider.getCustomerList();
                        Iterator<Customer> iterator = customers.iterator();
                        while (iterator.hasNext()) {
                            Customer aCustomer = iterator.next();
                            if (customer.getId() == aCustomer.getId()) {
                                iterator.remove();
                                break;
                            }
                        }

                        target.add(form);
                        target.add(getParent());
                        target.add(findPage());
                    }
                };
                removeLink.setOutputMarkupId(true);
                item.add(removeLink);

                item.setOutputMarkupId(true);
            }
        };

        dataView.setOutputMarkupId(true);
        form.add(dataView);
    }
}

提供商:

public class CustomerProvider extends SortableDataProvider<Customer, String> {

    private List<Customer> customerList = new ArrayList<Customer>();
    private SortableDataProviderComparator comparator = new SortableDataProviderComparator();

    class SortableDataProviderComparator implements Comparator<Customer>, Serializable {
        public int compare(final Customer o1, final Customer o2) {
            PropertyModel<Comparable> model1 = new PropertyModel<Comparable>(o1, getSort().getProperty());
            PropertyModel<Comparable> model2 = new PropertyModel<Comparable>(o2, getSort().getProperty());

            int result = model1.getObject().compareTo(model2.getObject());

            if (!getSort().isAscending()) {
                result = -result;
            }

            return result;
        }
    }

    public CustomerProvider(List<Customer> customerList) {
        Customer latest = new Customer();
        latest.setId(2000);
        latest.setLastName("LMN");
        latest.setFirstName("OPQ");
        latest.setAge(30);
        customerList.add(latest);

        setSort("firstName", SortOrder.ASCENDING);
    }

    public CustomerProvider() {

        Customer latest = new Customer();
        latest.setId(1000);
        latest.setLastName("XYZ");
        latest.setFirstName("ABC");
        latest.setAge(20);
        customerList.add(latest);

        latest = new Customer();
        latest.setId(2000);
        latest.setLastName("LMN");
        latest.setFirstName("OPQ");
        latest.setAge(30);
        customerList.add(latest);


        setSort("firstName", SortOrder.ASCENDING);


    }

    @Override
    public Iterator<? extends Customer> iterator(final long first, final long count) {

        // Get the data
        List<Customer> newList = new ArrayList<Customer>(customerList);

        // Sort the data
        Collections.sort(newList, comparator);

        // Return the data for the current page - this can be determined only after sorting
        return newList.subList((int) first, (int) (first + count)).iterator();

    }

    @Override
    public long size() {
        return customerList.size();
    }

    @Override
    public IModel<Customer> model(final Customer customer) {

        return new IModel<Customer>() {
            @Override
            public Customer getObject() {
                return customer;
            }

            @Override
            public void setObject(Customer object) {

            }

            @Override
            public void detach() {

            }
        };
    }

    @Override
    public void detach() {

    }


    public List<Customer> getCustomerList() {
        return customerList;
    }

    public void setCustomerList(List<Customer> customerList) {
        this.customerList = customerList;
    }
}

HTML:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns:wicket>
<head>
</head>
<body>
<div wicket:id="feedback"></div>
<form wicket:id="form">
    <table class="table table-hover table-bordered">
        <thead>
        <tr>
            <th data-field="id" data-align="right" data-sortable="true">ID</th>
            <th data-field="fname">First Name</th>
            <th data-field="lname">Last Name</th>
            <th data-field="ag" data-sortable="true">Age</th>
        </tr>
        </thead>
        <tr wicket:id="table">
            <td><span wicket:id="id">[contactid]</span></td>
            <td><span wicket:id="firstname">[firstname]</span></td>
            <td><span wicket:id="lastname">[lastname]</span></td>
            <td><span wicket:id="age">[lastname]</span></td>
            <td><a href="#" wicket:id="remove">[lastname]</a></td>
        </tr>
    </table>
</form>
</body>
</html>

希望这对您有所帮助。

int cont = 0;
for(ProgramaAnexo bem:list){            
    if(bemRemove.getBem().getId().intValue()==bem.getBem().getId().intValue()){  
        listaBensSelecionadosDireito.remove(cont);
        break;
    }
}       

像这样,cont将永远是0,所以它总是会删除第一个。嗯……至少我是这么看的。如果 listaBensSelecionadosDireitoList<ProgramaAnexo>,你应该 w/o 任何问题,只是 listaBensSeelcionadosDireito.remove(bemRemove)。 像我说的那样使用,你甚至不需要使用这些 forif

大家好吗?所以我设法在这里找到了答案。事实上代码是正确的,问题出在 HTML ...我使用的代码是这样的: .

      <button wicket:id="btnExcluirAnexo" 
              title="Excluir" 
              class="btn btn-danger btn-sm"
              id="btnRemoverBem"">
                   <i class="fa fa-minus"></i>
      </button>

我刚刚删除了 'Button' 中的 id 并继续迭代......我不知道为什么会出现这个问题,但是嘿,它有效:) .

<button wicket:id="btnExcluirAnexo" 
          title="Excluir" 
          class="btn btn-danger btn-sm"
           I REMOVED THE ID HERE>
               <i class="fa fa-minus"></i>
  </button>

感谢所有回复的人。