2 Replies Latest reply on Nov 17, 2010 9:43 AM by ibstmt

    ExtendedDataTable select

    ibstmt Newbie

      I am using an ExtendedDataTableBean class to manage my table. After loading the table, how can I programatically select the first row by default?

       

      Here is my class.

       

      package com.ibi.widgets;

       

      import java.util.ArrayList;
      import java.util.Iterator;
      import java.util.List;

       

      import org.richfaces.model.DataProvider;
      import org.richfaces.model.ExtendedTableDataModel;
      import org.richfaces.model.selection.SimpleSelection;

       

      public class ExtendedDataTableBean {
          private String sortMode = "single";
          private String selectionMode = "multi";
          private SimpleSelection selection;
          private List selectedItems = new ArrayList();
          private ExtendedTableDataModel dataModel;
          private List items = new ArrayList();

       

          public SimpleSelection getSelection() {
              return selection;
          }

       

          public List getSelectedItems() {
              return selectedItems;
          }

       

          public void setSelectedItems(List selectedItems) {
              this.selectedItems = selectedItems;
          }

       

          public void setSelection(SimpleSelection selection) {
              this.selection = selection;
          }

       

          public Object getSelectedObject() {
              Iterator<Object> iterator = getSelection().getKeys();
              while (iterator.hasNext()) {
                  Object key = iterator.next();
                  dataModel.setRowKey(key);
                  return dataModel.getRowData();
              }
              return null;
          }

       

          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 ExtendedDataTableBean() {
          }

       

          public ExtendedTableDataModel getDataModel() {
                  dataModel = new ExtendedTableDataModel(new DataProvider() {

       

                      private static final long serialVersionUID = 5054087821033164847L;

       

                      public Object getItemByKey(Object key) {
                          for (Object c : items) {
                              if (key.equals(getKey(c))) {
                                  return c;
                              }
                          }
                          return null;
                      }

       

                      public List getItemsByRange(int firstRow, int endRow) {
                          return items.subList(firstRow, endRow);
                      }

       

                      public Object getKey(Object item) {
                          return item.hashCode();
                      }

       

                      public int getRowCount() {
                          return items.size();
                      }

       

                  });

       

              return dataModel;
          }

       

          public void takeSelection() {
              clearSelection();
              Iterator<Object> iterator = getSelection().getKeys();
              while (iterator.hasNext()) {
                  Object key = iterator.next();
                  getDataModel().setRowKey(key);
                  getSelectedItems().add(getDataModel().getRowData());
              }
          }

       

          public void clearSelection() {
              getSelectedItems().clear();   
          }
         
          public void setItems(List items) {
              this.items = items;
          }

       

      }

        • 1. Re: ExtendedDataTable select
          Ilya Shaikovsky Master

          for richfaces-demo code I made it like:

          /**
          *
          */
          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.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 Object tableState;
              private Selection selection;
              private List<Capital> capitals = new ArrayList<Capital>();
              private ExtendedTableDataModel<Capital> dataModel;
              private List<Capital> selectedCapitals = 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 void takeSelection() {
                  selectedCapitals.clear();
                  Iterator<Object> iterator = getSelection().getKeys();
                  while (iterator.hasNext()) {
                      Object key = iterator.next();
                      selectedCapitals.add(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;
              }
              public Object getTableState() {
                  return tableState;
              }
              public void setTableState(Object tableState) {
                  this.tableState = tableState;
              }
              public Selection getSelection() {
                  if (selection==null){
                      selection = new SimpleSelection();
                      ((SimpleSelection)selection).addKey(capitals.get(0).getName());
                  }
                  return selection;
              }
              public void setSelection(Selection selection) {
                  this.selection = selection;
              }
              public List<Capital> getSelectedCapitals() {
                  return selectedCapitals;
              }
              public void setSelectedCapitals(List<Capital> selectedCapitals) {
                  this.selectedCapitals = selectedCapitals;
              }
          }

          /**

          *

          */

          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.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 Object tableState;

              private Selection selection;

              private List<Capital> capitals = new ArrayList<Capital>();

              private ExtendedTableDataModel<Capital> dataModel;

              private List<Capital> selectedCapitals = 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 void takeSelection() {

                  selectedCapitals.clear();

                  Iterator<Object> iterator = getSelection().getKeys();

                  while (iterator.hasNext()) {

                      Object key = iterator.next();

                      selectedCapitals.add(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;

              }

           

              public Object getTableState() {

                  return tableState;

              }

           

              public void setTableState(Object tableState) {

                  this.tableState = tableState;

              }

           

              public Selection getSelection() {

                  if (selection==null){

                      selection = new SimpleSelection();

                      ((SimpleSelection)selection).addKey(capitals.get(0).getName());

                  }

                  return selection;

              }

           

              public void setSelection(Selection selection) {

                  this.selection = selection;

              }

           

              public List<Capital> getSelectedCapitals() {

                  return selectedCapitals;

              }

           

              public void setSelectedCapitals(List<Capital> selectedCapitals) {

                  this.selectedCapitals = selectedCapitals;

              }

           

          }

           

          pay attention to getSelection method.

          • 2. Re: ExtendedDataTable select
            ibstmt Newbie

            Ah, I see it. I've got it working. Thanks!