2 Replies Latest reply on Feb 5, 2011 5:57 PM by niknik77771

    How to delete and add rows of rich:extendedDataTable by ajax.

    niknik77771

      This topic is obtained from this discussion.

      I found a solution how to add and delete rows of rich:extendedDataTable. Please, see an example:

       

      <h:form>

          Create item:

          <h:inputText value="#{updateExtDataTableBean.item}"/>

          <a4j:commandButton value="Create"

                             actionListener="#{updateExtDataTableBean.addItem}"

                             render="table1"

                             execute="@form"/>

          <a4j:commandButton value="Delete selection"

                             actionListener="#{updateExtDataTableBean.deleteItem}"

                             render="table1"

                             execute="@form"

                  />

       

       

          <p/>

          <a4j:outputPanel id="table1" layout="block">

              <rich:extendedDataTable binding="#{updateExtDataTableBean.uitable}"

                                      value="#{updateExtDataTableBean.items}" var="item"

                                      id="table"

                                      styleClass="stable"

                                      selection="#{updateExtDataTableBean.selection}"

                                      style="width:200px;"

                                      selectionMode="multi"

                      >

                  <rich:column width="199px">

                      <f:facet name="header">

                          <h:outputText value="Items"/>

                      </f:facet>

                      <h:outputText value="#{item}"/>

                  </rich:column>

              </rich:extendedDataTable>

          </a4j:outputPanel>

      </h:form>

       

      Corresponding bean is:

      package ru.agost.jsf.beans.tests;

       

       

      import com.google.common.collect.Lists;

      import com.google.common.collect.Ordering;

      import com.google.common.collect.Sets;

      import org.richfaces.component.UIExtendedDataTable;

       

       

      import javax.annotation.PostConstruct;

      import javax.faces.bean.ManagedBean;

      import javax.faces.bean.ViewScoped;

      import java.io.Serializable;

      import java.util.Collection;

      import java.util.List;

      import java.util.Set;

       

       

      @ManagedBean

      @ViewScoped

      public class UpdateExtDataTableBean implements Serializable {

          private static final long serialVersionUID = 7908746683927598277L;

          private static Set<String> dataBase = Sets.newTreeSet(Ordering.<Comparable>natural());

          private String item;

          private List<String> items;

          private Collection<Object> selection;

          private transient UIExtendedDataTable uitable;

          @PostConstruct

          public void init() {

              items = Lists.newArrayList(dataBase);

          }

       

       

          public void updateTable() {

              items = Lists.newArrayList(dataBase);

          }

       

       

          public void addItem() {

              if (item != null || "".equals(item)) {

                  dataBase.add(item);

                  updateTable();

              }

          }

       

       

          public void deleteItem() {

              if (!selection.isEmpty()) {

                  Object originalKey = uitable.getRowKey();

                  for (Object selectionKey : selection) {

                      uitable.setRowKey(selectionKey);

                      if (uitable.isRowAvailable()) {

                          String searchedItem = (String) uitable.getRowData();

                          dataBase.remove(searchedItem);

                      }

                  }

                  uitable.setRowKey(originalKey);

                  uitable.getSelection().clear();

              }

              updateTable();

          }

       

       

          public String getItem() {

              return item;

          }

       

       

          public void setItem(String item) {

              this.item = item;

          }

       

       

          public List<String> getItems() {

              return items;

          }

       

       

          public void setItems(List<String> items) {

              this.items = items;

          }

       

       

          public Collection<Object> getSelection() {

              return selection;

          }

       

       

          public void setSelection(Collection<Object> selection) {

              this.selection = selection;

          }

       

       

          public UIExtendedDataTable getUitable() {

              return uitable;

          }

       

       

          public void setUitable(UIExtendedDataTable uitable) {

              this.uitable = uitable;

          }

      }