Thursday 2 October 2014

resolve first and last row in the af:table using varstatus

During my current project i've been asked about rendering a button for first and last row in a table. There is very simple way to achieve that without a single line of Java code. The keyword to the success is varStatus. I've prepared simple application where on the testPage i've drag&dropped sortable and filterable read only table. Also i've added few columns where varStatus properties are displayed.



What can be noticed here:
  • begin always returns 0, it's the index of first element in the collection
  • count returns the number of objects in the collection for the current step
  • current returns the reference to the current row (in the iterators loop)
  • end is supposed to return index of the last element - unfortunately it returns null
  • last is supposed to return true for the last element and false otherwise, it's null (see bug: varStatus.last working?)
  • index returns row index, could be used to display order number value for each row as #{vs.index+1} because it starts from 0
  • step is always 1 - each row from the collection should be displayed
OK, so what needs to be done to properly point the last row? Just small and simple trick:



You need to compare current row index with row count in the collection #{bindings.TheNameOfTheIterator.estimatedRowCount==vs.index+1}. The screenshots above presents filtered collection. Filtering and sorting don't corrupt the solution.

Please notice that varStatus is not only related with <af:table> but also with other components like <af:iterator> or <af:forEach> and its attribute set (and returned values) may differ there.

3 comments:

  1. Another great post! Just a small note - is the blog title OK? I have such feeling there is a word missing after "in the"...

    ReplyDelete
  2. Hi Nice Post.. but how can we access the selected rows varStatus value from a bean. Pls help me on this.

    ReplyDelete
  3. You can resolve EL expression in the code. But the same as for the JSF, you must be in the "context" of table (means that i.e. there is a button in each row and the code is under the button).

    Could you describe the use case?

    ReplyDelete