6 Replies Latest reply on Sep 14, 2009 8:33 AM by Nick Belaevski

    Getting row number in rich:dataTable

    Zabin Begum Newbie

      Hello All

      I was trying to get row index in rich:dataTable. Can anybody help me out with this.

      Thanks
      Zabin

        • 1. Re: Getting row number in rich:dataTable
          Zabin Begum Newbie

          Hello All

          Just adding some code which I am using.

          I am using PaginatingDataModel.

          @Name("businessDataModel")
          @Scope(ScopeType.EVENT)
          public class BusinessesDataModel extends PaginatingDataModel<IcBusiness, Long> {
          ....
          }

          In the jsp I am calling the data table like this:
          <rich:dataTable id="businesseslist" value="#{businessDataModel}" rows="10" width="100%" var="business" rowKeyVar="rowkey" >
          ..........
          </rich:dataTable>


          The rowKey is giving me the object Id. But I want the row index.
          I have overrided getRowIndex and setRowIndex along with getter and setter of wrappedData in BusinessesDataModel, but of no use.

          From some forums, I got to know about binding option. I tried with that too but of no use, might be i am using it in wrong direction.

          Please help.

          • 2. Re: Getting row number in rich:dataTable
            Nick Belaevski Master

            Hi,

            Have you tried #{businessDataModel.rowIndex} ?

            • 3. Re: Getting row number in rich:dataTable
              Zabin Begum Newbie

              Yes, I tried with #{businessDataModel.rowIndex} and I have overrided getRowIndex() and setRowIndex(). but I am getting null pointer exception.
              I have overrided like this:

              @Override
              public int getRowIndex(){
              return this.rowIndex;
              }

              @Override
              public void setRowIndex(int rowIndex){
              this.rowIndex = rowIndex;
              }

              I know that this method will give nullpointer exception because nowhere we are setting the rowIndex. Can you tell me where can I call this setter method so that I can set the rowIndex.


              Thanks

              • 4. Re: Getting row number in rich:dataTable
                Nick Belaevski Master

                What is PaginatingDataModel class - I cannot find such class in RF?

                • 5. Re: Getting row number in rich:dataTable
                  Zabin Begum Newbie

                  PaginatingDataModel is an abstract class of SerializableDataModel
                  package com.sec.portal.data;

                  import java.io.IOException;
                  import java.util.ArrayList;
                  import java.util.HashMap;
                  import java.util.List;
                  import java.util.Map;

                  import javax.faces.context.FacesContext;

                  import org.ajax4jsf.model.DataVisitor;
                  import org.ajax4jsf.model.Range;
                  import org.ajax4jsf.model.SequenceRange;
                  import org.ajax4jsf.model.SerializableDataModel;

                  /**
                  * @param
                  * @param
                  */
                  public abstract class PaginatingDataModel<T, U> extends SerializableDataModel {

                  /** */
                  private static final long serialVersionUID = 2954923950179861809L;
                  /** */
                  protected U currentPk;
                  /** */
                  protected boolean descending = true;
                  /** */
                  protected String sortField = getDefaultSortField();
                  /** */
                  protected boolean detached = false;
                  /** */
                  protected List wrappedKeys = new ArrayList();
                  /** */
                  protected Integer rowCount;
                  /** */
                  protected Map<U, T> wrappedData = new HashMap<U, T>();

                  /**
                  * @see org.ajax4jsf.model.ExtendedDataModel#getRowKey()
                  */
                  @Override
                  public Object getRowKey()
                  {
                  return currentPk;
                  }

                  /**
                  * @see org.ajax4jsf.model.ExtendedDataModel#setRowKey(java.lang.Object)
                  */
                  @SuppressWarnings("unchecked")
                  @Override
                  public void setRowKey(final Object key)
                  {
                  this.currentPk = (U) key;
                  }

                  /**
                  * @see org.ajax4jsf.model.SerializableDataModel#update()
                  */
                  @Override
                  public void update()
                  {
                  if (getSortFieldObject() != null)
                  {
                  final String newSortField = getSortFieldObject().toString();
                  if (newSortField.equals(sortField))
                  {
                  descending = !descending;
                  }
                  sortField = newSortField;
                  }
                  detached = false;
                  }

                  /**
                  * @return Object
                  */
                  protected Object getSortFieldObject()
                  {
                  final FacesContext context = FacesContext.getCurrentInstance();
                  final Object sortFieldObject = context.getExternalContext().getRequestParameterMap().get("sortField");
                  return sortFieldObject;
                  }

                  /**
                  * @param sortField
                  */
                  public void setSortField(final String sortField)
                  {
                  if (this.sortField.equals(sortField))
                  {
                  descending = !descending;
                  } else
                  {
                  this.sortField = sortField;
                  }
                  }

                  /**
                  * @return String
                  */
                  public String getSortField()
                  {
                  return sortField;
                  }

                  /**
                  * @see org.ajax4jsf.model.ExtendedDataModel#getSerializableModel(org.ajax4jsf.model.Range)
                  */
                  @Override
                  public SerializableDataModel getSerializableModel(final Range range)
                  {
                  if (wrappedKeys != null)
                  {
                  detached = true;
                  return this;
                  }
                  return null;
                  }

                  /**
                  * @see javax.faces.model.DataModel#setRowIndex(int)
                  */
                  @Override
                  public void setRowIndex(final int rowIndex)
                  {
                  throw new UnsupportedOperationException();

                  }

                  /**
                  * @see javax.faces.model.DataModel#setWrappedData(java.lang.Object)
                  */
                  @Override
                  public void setWrappedData(final Object data)
                  {
                  throw new UnsupportedOperationException();

                  }

                  /**
                  * @see javax.faces.model.DataModel#getRowIndex()
                  */
                  @Override
                  public int getRowIndex()
                  {
                  throw new UnsupportedOperationException();
                  }

                  /**
                  * @see javax.faces.model.DataModel#getWrappedData()
                  */
                  @Override
                  public Object getWrappedData()
                  {
                  throw new UnsupportedOperationException();
                  }

                  /**
                  * @see org.ajax4jsf.model.ExtendedDataModel#walk(javax.faces.context.FacesContext,
                  * org.ajax4jsf.model.DataVisitor, org.ajax4jsf.model.Range,
                  * java.lang.Object)
                  */
                  @Override
                  public void walk(final FacesContext context, final DataVisitor visitor, final Range range, final Object argument)
                  throws IOException
                  {
                  final int firstRow = ((SequenceRange) range).getFirstRow();
                  final int numberOfRows = ((SequenceRange) range).getRows();
                  if (detached && getSortFieldObject() != null)
                  {
                  for (final U key : wrappedKeys)
                  {
                  setRowKey(key);
                  visitor.process(context, key, argument);
                  }
                  } else
                  { // if not serialized, than we request data from data
                  // provider
                  wrappedKeys = new ArrayList();
                  for (final T object : findObjects(firstRow, numberOfRows, sortField, descending))
                  {
                  wrappedKeys.add(getId(object));
                  wrappedData.put(getId(object), object);
                  visitor.process(context, getId(object), argument);
                  }
                  }
                  }

                  /**
                  * @see javax.faces.model.DataModel#isRowAvailable()
                  */
                  @Override
                  public boolean isRowAvailable()
                  {
                  if (currentPk == null)
                  {
                  return false;
                  }
                  if (wrappedKeys.contains(currentPk))
                  {
                  return true;
                  }
                  if (wrappedData.entrySet().contains(currentPk))
                  {
                  return true;
                  }
                  try
                  {
                  if (getObjectById(currentPk) != null)
                  {
                  return true;
                  }
                  } catch (final Exception e)
                  {

                  }
                  return false;
                  }

                  /**
                  * @see javax.faces.model.DataModel#getRowData()
                  */
                  @Override
                  public Object getRowData()
                  {
                  if (currentPk == null)
                  {
                  return null;
                  }

                  T object = wrappedData.get(currentPk);
                  if (object == null)
                  {
                  object = getObjectById(currentPk);
                  wrappedData.put(currentPk, object);
                  }
                  return object;
                  }

                  /**
                  * @see javax.faces.model.DataModel#getRowCount()
                  */
                  @Override
                  public int getRowCount()
                  {
                  if (rowCount == null)
                  {
                  rowCount = getNumRecords();
                  }
                  return rowCount;
                  }

                  /**
                  *
                  * @param object
                  * @return U
                  */
                  public abstract U getId(T object);

                  /**
                  * @param firstRow
                  * @param numberOfRows
                  * @param sortField
                  * @param descending
                  * @return List
                  */
                  public abstract List findObjects(int firstRow, int numberOfRows, String sortField, boolean descending);

                  /**
                  * @param id
                  * @return T
                  */
                  public abstract T getObjectById(U id);

                  /**
                  * @return String
                  */
                  public abstract String getDefaultSortField();

                  /**
                  * @return int
                  */
                  public abstract int getNumRecords();

                  }

                  So in my class I am using it like this:
                  package com.sec.ezeestay.datamodels;

                  import java.util.List;

                  import javax.persistence.EntityManager;

                  import org.jboss.seam.ScopeType;
                  import org.jboss.seam.annotations.In;
                  import org.jboss.seam.annotations.Name;
                  import org.jboss.seam.annotations.Scope;

                  import com.sec.portal.data.PaginatingDataModel;
                  import com.sec.portal.persistence.instanceconfig.IcBusiness;
                  import com.sec.portal.services.reservation.BusinessSearchService;
                  import com.sec.portal.structures.search.BusinessSearch;

                  @Name("businessDataModel")
                  @Scope(ScopeType.EVENT)
                  public class BusinessesDataModel extends PaginatingDataModel<IcBusiness, Long> {

                  private static final long serialVersionUID = 561019436789160412L;

                  @In
                  EntityManager em;

                  @In(required=false)
                  BusinessSearch businessSearchData;

                  private Integer cachedResultCount;
                  private Integer rowIndex;

                  @Override
                  public List findObjects(int firstRow, int numberOfRows, String sortField, boolean descending) {
                  BusinessSearchService bss = new BusinessSearchService(em);
                  businessSearchData.setProduct("ezeeStay");

                  List result = bss.list(businessSearchData, firstRow, numberOfRows);

                  return result;
                  }

                  @Override
                  public String getDefaultSortField() {
                  return "name";
                  }

                  @Override
                  public Long getId(IcBusiness object) {
                  return object.getId();
                  }

                  @Override
                  public int getNumRecords() {
                  if(cachedResultCount == null) {
                  BusinessSearchService bss = new BusinessSearchService(em);
                  businessSearchData.setProduct("ezeeStay");
                  cachedResultCount = bss.count(businessSearchData);
                  }
                  return cachedResultCount;
                  }

                  @Override
                  public IcBusiness getObjectById(Long id) {
                  BusinessSearchService bss = new BusinessSearchService(em);
                  return bss.getBusiness(id);
                  }

                  @Override
                  public int getRowIndex(){
                  return this.rowIndex;
                  }

                  @Override
                  public void setRowIndex(int rowIndex){
                  this.rowIndex = rowIndex;
                  }

                  }

                  • 6. Re: Getting row number in rich:dataTable
                    Nick Belaevski Master

                    Method walk() generates keys, so you'll have to put there something that will allow to link row index with the row key, e.g. Map.