8 Replies Latest reply on Oct 8, 2010 6:23 AM by Ilya Shaikovsky

    ExtendedDataTable: getting a selection without using session scope

    Baraa Basata Newbie

      Hi,

       

      I'm trying to keep to request scope for a managed bean that backs an ExtendedDataTable. I'm fine with using <a4:keepAlive> to avoid session scope. What I'm finding is that, on clicking a table row, the setter for the selection model does get called, but it gets called with an empty selection (size() == 0).

       

      Configuring the bean to be session-scoped does get me the correct result of selection.size() == 1, but it's unexpected to me that setSelection would get called with an empty selection when using request scope. I've read other discussions in which session scope is suggested. Is this behavior expected, and it it possible to avoid the use of session scope here?

       

      Thanks!

       

       

      {code}
      <rich:extendedDataTable

          selection="#{myTableDataModel.selection}">

          ...

       

      --

       

      public class MyTableDataModel implements Serializable {

          private Selection selection = new SimpleSelection();

       

          public void setSelection(Selection selection) {

              this.selection = selection;

              System.out.println("Selection  size is: " + selection.size());

          }

          ...

      }

      {code}


        • 1. Re: ExtendedDataTable: getting a selection without using session scope
          Ilya Shaikovsky Master

          please post more complete code for both page and bean.

          • 2. Re: ExtendedDataTable: getting a selection without using session scope
            Baraa Basata Newbie

            Thanks for your reply, Ilya. I'm including code below, which I have isolated down to the smallest example. This has a form and a table. The form adds simple items to the list backing the table.

             

            I actually get a different behavior from this reduced example, which is that the table does not get rerendered upon clicking the command button. I've tried both reRender="myForm" and reRender="myTable", but the table continues to only show the two items that were added upon construction. Any ideas for this non-rerendering behavior? If this example will work, then my selection issue may be resolved with it.

             

            {code}

            <!doctype html public "-//w3c//dtd html 4.0 transitional//en">

            <%@ taglib prefix="f" uri="http://java.sun.com/jsf/core" %>

            <%@ taglib prefix="rich" uri="http://richfaces.org/rich" %>

            <%@ taglib prefix="h" uri="http://java.sun.com/jsf/html" %>

            <%@ taglib prefix="a4j" uri="http://richfaces.org/a4j" %>

            <html>

            <head>

            <title>Hello</title>

            </head>

            <body>

            <f:view>

              <a4j:keepAlive beanName="myTableDataModel" />

              <a4j:form id="inputForm">

              <h:inputText id="inputText" value="#{myTableDataModel.input}" />

              <a4j:commandButton id="submitButton" value="Add"

                 actionListener="#{myTableDataModel.addItem}" reRender=":myTable" />

              </a4j:form>

             

              <h:form id="myForm">

                <rich:extendedDataTable

                  id="myTable"

                  value="#{myTableDataModel.rows}"

                  var="row"

                  rows="5"

                  height="200"

                  selection="#{myTableDataModel.selection}">

             

                  <a4j:support event="onselectionchange" actionListener="#{myTableDataModel.onSelectionChange}"

                               reRender="selectedItemName" />

                  <rich:column>

                    <h:outputText value="#{row.name}" />

                  </rich:column>

                </rich:extendedDataTable>

              </h:form>

             

              <h:outputText id="selectedItemName" value="#{myTableDataModel.selectedItem.name}" />

             

            </f:view>

            </body>

            </html>

             

             

            package v4.uimodel;

            import org.richfaces.model.selection.Selection;
            import org.richfaces.model.selection.SimpleSelection;

            import javax.faces.event.ActionEvent;
            import java.io.Serializable;
            import java.util.ArrayList;
            import java.util.List;

            public class MyTableDataModel implements Serializable {
                public static class Item implements Serializable {
                    private String name;
                    public String getName() { return name; }
                    public void setName(String name) { this.name = name; }

                    public Item(String name) { this.name = name; }
                }

                private String input;
                private List<Item> items;
                private Item selectedItem;
                private Selection selection = new SimpleSelection();

                public MyTableDataModel() {
                    items = new ArrayList<Item>();
                    items.add(new Item("One"));
                    items.add(new Item("Two"));
                }

                public void addItem(ActionEvent event) {
                    items.add(new Item(input));
                }
               
                public String getInput() {
                    return input;
                }

                public void setInput(String input) {
                    this.input = input;
                }

                public List<Item> getRows() {
                    return items;
                }

                public Item getSelectedItem() {
                    return selectedItem;
                }

                public Selection getSelection() {
                    return selection;
                }

                public void setSelection(Selection selection) {
                    this.selection = selection;
                    if (selection.size() > 0) {
                        Integer selectedIndex = (Integer)selection.getKeys().next();
                        selectedItem = items.get(selectedIndex);
                    }
                   
                    System.out.println("Selection size is: " + selection.size());
                    System.out.println("Row count is: " + items.size());
                }

                public void onSelectionChange(ActionEvent event) {
                    System.out.println("Selection size is: " + selection.size());
                    System.out.println("Row count is: " + items.size());
                }
            }

            {code}

            • 3. Re: ExtendedDataTable: getting a selection without using session scope
              Baraa Basata Newbie

              To clarify, I think my original issue was that both the form and the table were contained in the same <a4j:form>, which seems to have affected selection behavior.

               

              Here, they are placed into two separate forms. The reRender is the only piece remaining to solve in this example, I think.

              • 4. Re: ExtendedDataTable: getting a selection without using session scope
                Baraa Basata Newbie

                More detail: if I use a DataTable instead of ExtendedDataTable, the table is rerendered perfectly. The unique piece seems to be the use of ExtendedDataTable inside a Form.

                 

                With an ExtendedDataTable inside a Form, using reRender="myTable" has no effect. Using reRender="myForm" results in a neverending  "loading" spinner/indicator.

                 

                I'm assuming the Form is necessary for the use of <a4j:support> here.

                • 5. Re: ExtendedDataTable: getting a selection without using session scope
                  Ilya Shaikovsky Master

                  in your original snippet the problem was in

                   

                  :myTable

                  Such usage means that the component will be searched form outside of any forms. 
                  should be just
                  
                  myTable
                  
                  Your current problem of neverending loadind - seems just old issue of table - 
                  which version you using? Try the 3.3.3 final if using older.
                  • 6. Re: ExtendedDataTable: getting a selection without using session scope
                    Baraa Basata Newbie

                    Thanks for your reply, Ilya.  I'm using 3.3.3 Final. I've played with setting reRender in different ways: "myTable", ":myTable", and "myForm". With reRender="myTable" and reRender=":myTable", the table does not change at all. With reRender="myForm", I get the neverending loading indicator.

                     

                    If I remove the <h:form> that's wrapping the table and then use reRender="myTable", the table gets refreshed perfectly. It's when the ExtendedDataTable is inside a Form that I'm experiencing this.

                    • 7. Re: ExtendedDataTable: getting a selection without using session scope
                      Alex He Newbie

                      I am running into the same issue. Is this bug has been reported?

                      • 8. Re: ExtendedDataTable: getting a selection without using session scope
                        Ilya Shaikovsky Master

                        No I can't reproduce that at test pages where EDT being reRender'ed. it's successful for me.