0 Replies Latest reply on Jul 15, 2011 6:49 PM by tsunglintsai

    default selection of rich:extendedDataTable

    tsunglintsai Newbie

      I have a working datatable with selection based on example from richfaces demo.

      what i want to achieve is to have all rows selected by default when table is shown. can someone give me some exapmle to do so.

       

      thank you, following is my code.

       

      <rich:extendedDataTable  selection="#{SendEmailBean.filterOptionsUI.selection}" value="#{SendEmailBean.filterOptionsUI.dataModel}" var="item" id="userTable" height="260px" selectionMode="multi">

         <rich:column sortable="true" width="50%" sortBy="#{item.email}" style="text-align: left;border:0px">

            <f:facet name="header">

               <h:outputText value="Email" />

            </f:facet>

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

         </rich:column>

         <rich:column sortable="true" width="50%" sortBy="#{item.name}" style="text-align: left;border:0px">

            <f:facet name="header">

               <h:outputText value="Name" />

            </f:facet>

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

         </rich:column>

          <a4j:support id="extended_table_bean_take_selection"

                          action="#{SendEmailBean.takeSelection}"

                          event="onselectionchange" />

      </rich:extendedDataTable>

       

       

      -- MB --

       

      public class SendEmailBean implements Serializable{

          @PostConstruct

          public void init(){

              this.filterOptionsUI = new FilterOptionsUI();

          }

       

      public void takeSelection() {
          this.filterOptionsUI.selectedUsers.clear();
          Iterator<Object> iterator = this.filterOptionsUI.getSelection().getKeys();
          while (iterator.hasNext()) {
              Object key = iterator.next();
              this.filterOptionsUI. selectedUsers.add( this.filterOptionsUI.getDataModel().getObjectByKey(key));
          }
      }

       

      public static class FilterOptionsUI  implements Serializable

          {

              private boolean emailChecked;

              private boolean nameChecked;

              private boolean userTypeChecked;

              private boolean clientOSChecked;

              private boolean clientVersionChecked;

              private String email;

              private String name;

              private String userType;

              private String clientOS;

              private String clientVersionCompare;

              private String clientVersion;

              private SimpleSelection selection;

              private List<UserInfo> userList = new ArrayList<UserInfo>();

              private List<UserInfo> selectedUsers = new ArrayList<UserInfo>();

              private ExtendedTableDataModel<UserInfo> dataModel;

              private String okDisabled;

              private String toInputString;

              private String ccInputString;

              private String bccInputString;

              private List<SelectItem> osList;

       

              public ExtendedTableDataModel<UserInfo> getDataModel() {

                  if (dataModel == null) {

                      dataModel = new ExtendedTableDataModel<UserInfo>(new DataProvider<UserInfo>() {

       

                          private static final long serialVersionUID = 5054087821033164847L;

       

                          public UserInfo getItemByKey(Object key) {

                              for (UserInfo c : userList) {

                                  if (key.equals(getKey(c))) {

                                      return c;

                                  }

                              }

                              return null;

                          }

       

                          public List<UserInfo> getItemsByRange(int firstRow, int endRow) {

                              return userList.subList(firstRow, endRow);

                          }

       

                          public Object getKey(UserInfo item) {

                              return item.getEmail();

                          }

       

                          public int getRowCount() {

                              return userList.size();

                          }

                      });

                  }

                  return dataModel;

              }

       

              public void setDataModel(ExtendedTableDataModel<UserInfo> dataModel) {

                  this.dataModel = dataModel;

              }

       

              public FilterOptionsUI(){

                  this.okDisabled = "true";

                  this.osList = new ArrayList<SelectItem>();

              }

       

              public String getClientOS() {

                  return clientOS;

              }

       

              public void setClientOS(String clientOS) {

                  this.clientOS = clientOS;

              }

       

              public boolean isClientOSChecked() {

                  return clientOSChecked;

              }

       

              public void setClientOSChecked(boolean clientOSChecked) {

                  this.clientOSChecked = clientOSChecked;

              }

       

              public String getClientVersion() {

                  return clientVersion;

              }

       

              public void setClientVersion(String clientVersion) {

                  this.clientVersion = clientVersion;

              }

       

              public boolean isClientVersionChecked() {

                  return clientVersionChecked;

              }

       

              public void setClientVersionChecked(boolean clientVersionChecked) {

                  this.clientVersionChecked = clientVersionChecked;

              }

       

              public String getClientVersionCompare() {

                  return clientVersionCompare;

              }

       

              public void setClientVersionCompare(String clientVersionCompare) {

                  this.clientVersionCompare = clientVersionCompare;

              }

       

              public String getEmail() {

                  return email;

              }

       

              public void setEmail(String email) {

                  this.email = email;

              }

       

              public boolean isEmailChecked() {

                  return emailChecked;

              }

       

              public void setEmailChecked(boolean emailChecked) {

                  this.emailChecked = emailChecked;

              }

       

              public String getName() {

                  return name;

              }

       

              public void setName(String name) {

                  this.name = name;

              }

       

              public boolean isNameChecked() {

                  return nameChecked;

              }

       

              public void setNameChecked(boolean nameChecked) {

                  this.nameChecked = nameChecked;

              }

       

              public String getUserType() {

                  return userType;

              }

       

              public void setUserType(String userType) {

                  this.userType = userType;

              }

       

              public boolean isUserTypeChecked() {

                  return userTypeChecked;

              }

       

              public void setUserTypeChecked(boolean userTypeChecked) {

                  this.userTypeChecked = userTypeChecked;

              }

              public void resetUI(){

                  this.emailChecked = false;

                  this.nameChecked = false;

                  this.userTypeChecked = false;

                  this.clientOSChecked = false;

                  this.clientVersionChecked = false;

                  this.email = null;

                  this.name = null;

                  this.userType = null;

                  this.clientOS = null;

                  this.clientVersionCompare = null;

                  this.clientVersion = null;

                  this.toInputString = null;

                  this.ccInputString = null;

                  this.bccInputString = null;

                  this.setOkDisabled("true");

              }

       

              @Override

              public String toString() {

                  return "FilterOptionsUI{" + "emailChecked=" + emailChecked + ",nameChecked=" + nameChecked + ",userTypeChecked=" + userTypeChecked + ",clientOSChecked=" + clientOSChecked + ",clientVersionChecked=" + clientVersionChecked + ",email=" + email + ",name=" + name + ",userType=" + userType + ",clientOS=" + clientOS + ",clientVersionCompare=" + clientVersionCompare + ",clientVersion=" + clientVersion + '}';

              }

       

              public List<UserInfo> getUserList() {

                  return userList;

              }

       

              public void setUserList(List<UserInfo> userList) {

                  this.userList = userList;

              }

       

              public List<UserInfo> getSelectedUsers() {

                  return selectedUsers;

              }

       

              public void setSelectedUsers(List<UserInfo> selectedUsers) {

                  this.selectedUsers = selectedUsers;

              }

       

              public SimpleSelection getSelection() {

                  if (selection==null){

                      selection = new SimpleSelection();

                  }

                  return selection;

              }

       

              public void setSelection(SimpleSelection selection) {

                  this.selection = selection;

              }

       

              public String getOkDisabled() {

                  return okDisabled;

              }

       

              public void setOkDisabled(String okDisabled) {

                  this.okDisabled = okDisabled;

              }

       

              public String getBccInputString() {

                  return bccInputString;

              }

       

              public void setBccInputString(String bccInputString) {

                  this.bccInputString = bccInputString;

              }

       

              public String getCcInputString() {

                  return ccInputString;

              }

       

              public void setCcInputString(String ccInputString) {

                  this.ccInputString = ccInputString;

              }

       

              public String getToInputString() {

                  return toInputString;

              }

       

              public void setToInputString(String toInputString) {

                  this.toInputString = toInputString;

              }

       

              public List<SelectItem> getOsList() {

                  return osList;

              }

       

              public void setOsList(List<SelectItem> osList) {

                  this.osList = osList;

              }

             

          }