6 Replies Latest reply on Jun 10, 2009 10:03 AM by Serhiy Zastavnyy

    Wrong selection when filter using

    Serhiy Zastavnyy Novice

      Hi
      I created modalPanel, created HtmlExtendedDataTable on panel.
      When I make double click on some row - action is fireing.
      When I do not use filter - everything works fine, but when I type something in filter field - SimpleSelection object has wrong value.
      My code:

       HtmlExtendedDataTable l = new HtmlExtendedDataTable();
       l.setId(listId);
       l.setValueExpression("value",
       eF.createValueExpression(eC, "#{bean.selectionData}", List.class));
       l.setWidth("100%");
       l.setHeight("250px");
       l.setSelectedClass("dataTableSelectedRow");
       l.setValueExpression("selection",
       eF.createValueExpression(eC, "#{bean.selectionCurrentItem}", SimpleSelection.class));
       l.setRowKeyVar("rkvar");
       l.setVar("item");
       l.getChildren().add(createListNameColumn(listId, "310px"));
      

       public static HtmlColumn createListNameColumn(String lId, String width){
       HtmlColumn c = new HtmlColumn();
       c.setId(LIST_COLUMN_NAME_ID);
       ValueExpression nameExpr = eF.createValueExpression(eC, "#{item.name}", String.class);
       c.setHeaderClass("dataTableHeader");
       c.setSelfSorted(true);
       c.setValueExpression("filterBy", nameExpr);
       c.setValueExpression("sortBy", nameExpr);
       c.setFilterEvent("onkeyup");
       HtmlOutputText cc = new HtmlOutputText();
       cc.setValueExpression("value", nameExpr);
       c.getChildren().add(cc);
       HtmlOutputText ch = new HtmlOutputText();
       ch.setValue(getResLabels().getString("column_name"));
       c.getFacets().put("header", ch);
       c.setWidth(width);
      }
      

       public Object getCurrentSelectionObject(){
       Iterator<Object> iterator = getSelectionCurrentItem().getKeys();
       while (iterator.hasNext()){
       Object key = iterator.next();
       return getSelectionData().get(Integer.parseInt(key.toString()));
       }
       return null;
       }
      

      When filter is useing , getCurrentSelectionObject returns wrong value.
      Please help me, what I am doing wrong. I use RF 3.3.1

        • 1. Re: Wrong selection when filter using
          Ilya Shaikovsky Master

          rowKey which stored in simpleSelection isn't the same as index in the collection because data is filtered. check our sample for scrollableDataTable. It contains the code which will works with filtering.

          • 2. Re: Wrong selection when filter using
            Serhiy Zastavnyy Novice

            Ilya, looks like example works correctly only with ScrollableTable.
            I tried to use code from demo with ExtendedDataTable and I have exception when I rerendered table.
            My scenario:
            1. Started action. Tried to get current row data of the extended table. Method bindingTable.setRowKey( some Key ) must be called. It is the cause.
            2. Tried to rerendered table after action.
            I had exception in ajax log:

            error[my_time]: New node for ID MY_TABLE_ID :j_id303_menu_script is not present in response.

            Could you please cjeck it with ExtendedDataTabele ? Looks like method table.setRowKey(key) do not want work correctly with ExtendedDataTable.

            • 3. Re: Wrong selection when filter using
              Serhiy Zastavnyy Novice

              Ilya, could you please help me ?

              • 4. Re: Wrong selection when filter using
                Serhiy Zastavnyy Novice

                Could someone please help me on this ?

                • 5. Re: Wrong selection when filter using
                  Ilya Shaikovsky Master

                  sorry for the delay.

                  this works fine for me for filtered table.(just changed demo code)

                  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
                  <html xmlns="http://www.w3.org/1999/xhtml"
                   xmlns:ui="http://java.sun.com/jsf/facelets"
                   xmlns:h="http://java.sun.com/jsf/html"
                   xmlns:f="http://java.sun.com/jsf/core"
                   xmlns:a4j="http://richfaces.org/a4j"
                   xmlns:rich="http://richfaces.org/rich">
                  
                  <ui:composition>
                   <h:form>
                   <h:panelGrid columns="2" columnClasses="top,top">
                   <rich:extendedDataTable
                   value="#{extendedTableBean.capitalsDataModel}" var="cap" id="tablesource"
                   width="580px" height="400px"
                   sortMode="#{extendedTableBean.sortMode}"
                   selectionMode="#{extendedTableBean.selectionMode}"
                   selection="#{extendedTableBean.selection}">
                   <rich:column sortable="false" label="Flag">
                   <f:facet name="header">
                   <h:outputText value="Flag" />
                   </f:facet>
                   <h:graphicImage value="#{cap.stateFlag}" />
                   </rich:column>
                   <rich:column sortable="true" sortBy="#{cap.state}"
                   filterBy="#{cap.state}" filterEvent="onkeyup" width="170px"
                   label="State Name">
                   <f:facet name="header">
                   <h:outputText value="State Name" />
                   </f:facet>
                   <h:outputText value="#{cap.state}" />
                   </rich:column>
                   <rich:column sortable="true" sortBy="#{cap.name}"
                   filterBy="#{cap.name}" filterEvent="onkeyup" width="170px"
                   label="State Capital">
                   <f:facet name="header">
                   <h:outputText value="State Capital" />
                   </f:facet>
                   <h:outputText value="#{cap.name}" />
                   </rich:column>
                   <rich:column sortable="false" label="Time Zone">
                   <f:facet name="header">
                   <h:outputText value="Time Zone" />
                   </f:facet>
                   <h:outputText value="#{cap.timeZone}" />
                   </rich:column>
                   </rich:extendedDataTable>
                   <rich:panel>
                   <f:facet name="header">
                   <h:outputText value="Sort/Selection modes changing" />
                   </f:facet>
                   <h:panelGrid columns="2">
                   <h:outputText value="Sort Mode:" />
                   <h:selectOneMenu value="#{extendedTableBean.sortMode}">
                   <f:selectItem itemLabel="Single" itemValue="single" />
                   <f:selectItem itemLabel="Multi" itemValue="multi" />
                   <a4j:support event="onchange" ajaxSingle="true" reRender="table" />
                   </h:selectOneMenu>
                   <h:outputText value="Selection Mode:" />
                   <h:selectOneMenu value="#{extendedTableBean.selectionMode}">
                   <a4j:support ajaxSingle="true" event="onchange" reRender="table" />
                   <f:selectItem itemLabel="Single" itemValue="single" />
                   <f:selectItem itemLabel="Multi" itemValue="multi" />
                   <f:selectItem itemLabel="None" itemValue="none" />
                   </h:selectOneMenu>
                   </h:panelGrid>
                   </rich:panel>
                   </h:panelGrid>
                   <a4j:commandButton value="Show Current Selection" reRender="table"
                   action="#{extendedTableBean.takeSelection}"
                   oncomplete="javascript:Richfaces.showModalPanel('panel');" />
                   </h:form>
                   <rich:modalPanel id="panel" autosized="false" keepVisualState="false"
                   width="315" height="230">
                   <f:facet name="header">
                   <h:outputText value="Selected Rows" />
                   </f:facet>
                   <f:facet name="controls">
                   <span style="cursor: pointer"
                   onclick="javascript:Richfaces.hideModalPanel('panel')">X</span>
                   </f:facet>
                   <h:panelGroup layout="block" styleClass="scrolls">
                   <rich:dataTable value="#{extendedTableBean.selectedCapitals}"
                   var="sel" id="table">
                   <rich:column>
                   <f:facet name="header">
                   <h:outputText value="Make" />
                   </f:facet>
                   <h:outputText value="#{sel.name}" />
                   </rich:column>
                   </rich:dataTable>
                   </h:panelGroup>
                   </rich:modalPanel>
                  </ui:composition>
                  
                  </html>

                  /**
                   *
                   */
                  package org.richfaces.demo.extendedDataTable;
                  
                  import java.util.ArrayList;
                  import java.util.Iterator;
                  import java.util.List;
                  
                  import org.richfaces.demo.capitals.Capital;
                  import org.richfaces.demo.datafilterslider.DemoInventoryItem;
                  import org.richfaces.model.DataProvider;
                  import org.richfaces.model.ExtendedTableDataModel;
                  import org.richfaces.model.selection.Selection;
                  import org.richfaces.model.selection.SimpleSelection;
                  
                  /**
                   * @author Ilya Shaikovsky
                   *
                   */
                  public class ExtendedTableBean {
                   private String sortMode = "single";
                   private String selectionMode = "multi";
                   private List<Capital> selectedCapitals = new ArrayList<Capital>();
                  
                   public List<Capital> getSelectedCapitals() {
                   return selectedCapitals;
                   }
                  
                   public void setSelectedCapitals(List<Capital> selectedCapitals) {
                   this.selectedCapitals = selectedCapitals;
                   }
                  
                   public Selection getSelection() {
                   return selection;
                   }
                  
                   public void setSelection(Selection selection) {
                   this.selection = selection;
                   }
                  
                   private Selection selection = new SimpleSelection();
                  
                   private ExtendedTableDataModel<Capital> dataModel;
                   private List<Capital> capitals = new ArrayList<Capital>();
                  
                   public String getSortMode() {
                   return sortMode;
                   }
                  
                   public void setSortMode(String sortMode) {
                   this.sortMode = sortMode;
                   }
                  
                   public String getSelectionMode() {
                   return selectionMode;
                   }
                  
                   public void setSelectionMode(String selectionMode) {
                   this.selectionMode = selectionMode;
                   }
                  
                   public ExtendedTableBean() {
                   }
                  
                   public void takeSelection() {
                   getSelectedCapitals().clear();
                   Iterator<Object> iterator = getSelection().getKeys();
                   while (iterator.hasNext()) {
                   Object key = iterator.next();
                   getSelectedCapitals().add((Capital) getCapitalsDataModel().getObjectByKey(key));
                   }
                   }
                  
                   public ExtendedTableDataModel<Capital> getCapitalsDataModel() {
                   if (dataModel == null) {
                   dataModel = new ExtendedTableDataModel<Capital>(
                   new DataProvider<Capital>() {
                  
                   private static final long serialVersionUID = 5054087821033164847L;
                  
                   public Capital getItemByKey(Object key) {
                   for (Capital c : capitals) {
                   if (key.equals(getKey(c))) {
                   return c;
                   }
                   }
                   return null;
                   }
                  
                   public List<Capital> getItemsByRange(int firstRow,
                   int endRow) {
                   return capitals.subList(firstRow, endRow);
                   }
                  
                   public Object getKey(Capital item) {
                   return item.getName();
                   }
                  
                   public int getRowCount() {
                   return capitals.size();
                   }
                  
                   });
                   }
                   return dataModel;
                   }
                  
                   public void setCapitals(List<Capital> capitals) {
                   this.capitals = capitals;
                   }
                  
                  }
                  


                  • 6. Re: Wrong selection when filter using
                    Serhiy Zastavnyy Novice

                    Thank you,
                    using ExtendedTableDataModel it works fine for me now.