4 Replies Latest reply on Sep 21, 2011 5:25 AM by Derk Dukker

    Set rows in extendedDataTable as selected from backing bean

    Derk Dukker Novice

      Hi all,

       

      I wonder how I can programmaticly set rows in an ExtendedDataTable as selected from my backing bean. I need to edit a user in my web app. The user has some roles so what I want is that when the page is loaded the groups which the user has are selected in the extendedDataTable.

       

      I'm using Spring3 with JSF 2 and richfaces 4.

       

      I think I need to bind the table to a backing bean which is in request scope. Can I use the Spring request scope for that? After that I need to implement the walk() on the datatable I guess. I have no idea where to go from there, can somebody point me in the right direction or give me an example?

       

      Regards,

       

      Derk

        • 1. Re: Set rows in extendedDataTable as selected from backing bean
          Derk Dukker Novice

          Here a piece of my code. This works, I see the "rowdata equals object" log statement  but now I need to say to the row "selected" but there isn't a method for that as far as I know...

           

          public void selectRows(){

                              Collection<Object> s = new ArrayList<Object>(getGroups());

            log.debug("set the selection to the table");

           

                              table.getTable().walk(FacesContext.getCurrentInstance(), new DataVisitor() {

           

            @Override

                                        public DataVisitResult process(FacesContext context, Object rowKey,

                                                            Object argument) {

            log.debug("entered walk");

                                                  Collection<Object> selection = (Collection<Object>) argument;

                                                  for(Object o : selection){

                                                            table.getTable().setRowKey(rowKey);

                                                            if(table.getTable().getRowData().equals(o)){

            log.debug("rowdata equals object");

                                                                       table.getTable().getSelection().add(o);

                                                                       log.debug("size of selection is: " + table.getTable().getSelection().size());

                                                            }

                                                  }

                                                  table.getTable().setRowKey(rowKey);

            return null;

                                        }

                              }, s );

                    }


          • 2. Re: Set rows in extendedDataTable as selected from backing bean
            Ilya Shaikovsky Master

            check the example there. http://richfaces-showcase.appspot.com/richfaces/component-sample.jsf?demo=extendedDataTable&sample=exTableSelection&skin=blueSky

             

            As you could see selection attribute bound to list of rowKeys, So just add the rowKeys of rows which should be selected initially to that collection. 

            1 of 1 people found this helpful
            • 3. Re: Set rows in extendedDataTable as selected from backing bean
              Derk Dukker Novice

              Ilya,

               

              Thank you for your response. I copied the example and changed it a bit. Now the example works. Next I need to implement this in my app. The problem is as follows.

               

              I have a list with groups which is loaded in the datatable. Right before the page is displayed I fire a page action (with pretty faces) which loads the user info. In this user info I get a list with groups. The groups in that list needs to compared with the list in the datatable. If a group in the user list matches against the list in the table I add the rowkey to the selection list. See below:

               

              public void selectRows(){

                log.debug("entered selectRows");

                                  user = findById(WnrExtranetUser.class, user.getWnrExtranetUserId());

                                  Collection<Object> userGroups = new ArrayList<Object>(user.getWnrExtranetGroups());

                log.debug("set the selection to the table");

                                  selection = new ArrayList<Object>();

               

                                  if(table != null) {

                log.debug("table is not null");

                                            if(table.getTable() != null) {

                log.debug("table.getTable is not null");

               

                                                      table.getTable().walk(FacesContext.getCurrentInstance(), new DataVisitor() {

               

                @Override

                                                                public DataVisitResult process(FacesContext context, Object rowKey,

                                                                                    Object argument) {

                                                                          log.debug("entered walk");

                                                                          Collection<Object> userGroups = (Collection<Object>) argument;

                                                                          for(Object userGroup : userGroups){

                                                                                    table.getTable().setRowKey(rowKey);

                                                                                    if(table.getTable().getRowData().equals(userGroup)){

                log.debug("rowdata equals object");

                //                                                            table.getTable().getSelection().add(userGroup);

                //                                                            log.debug("size of selection is: " + table.getTable().getSelection().size());

                                                                                              selection.add(rowKey);

                                                                                              selectionItems.add((WnrExtranetGroup)userGroup);

                                                                                    }

                                                                          }

                                                                          table.getTable().setRowKey(rowKey);

                return null;

                                                                }

                                                      }, userGroups );

                                            }

                                  }

                        }

               

              When I fire this method in a page action with pretty faces the table.getTable() == null. If a fire this method after the page is shown on screen with a commandButton the row in the table is selected.

              How can I fix this so that the method is fired on page load and the table.getTable isn't null anymore???

               

              btw I went wrong with the selection on the server by calling  table.getTable().getSelection().add(userGroup); instead of adding the rowKey to the selection attribute in the class which it was bind to... Why use Object and not Integer for the rowKey? Eventually it is cast to an Integer...

              • 4. Re: Set rows in extendedDataTable as selected from backing bean
                Derk Dukker Novice

                I solved it by calling the selectRows method in a

                 

                <f:event type="preRenderView" listener="#{newExtranetUserAction.selectRows}"  /> 

                 

                that works!

                 

                But I'm still curious why the table.getTable() resolves to null when using the page action with pretty faces.