0

I'm trying to build a <p:dataTable> with row count but for some reason it shows only the first index in every row. Here is my code:

index.xhtml

<p:dataTable value="#{personBean.personListAsModel}" var="person">
  <p:column headerText="No">
    <h:outputText value="#{personBean.personListAsModel.rowIndex + 1}"/>
  </p:column>

  <p:column headerText="Last name">
    <h:outputText value="#{person.lastName}"/>
  </p:column>

  <p:column headerText="First name">
    <h:outputText value="#{person.firstName}"/>
  </p:column>
</p:dataTable

PersonBean.java

@ManagedBean
@ViewScoped //The scope doesn't matter (I think)
public class PersonBean () {
    private List<Person> personList;

    public PersonBean() {
        personList = new ArrayList<>();

        // populate the list with Person objects (it's just a POJO with first and last name)
    }

    // get/set for personList

    public ListDataModel<Person> getPersonListAsModel() {
        return new ListDataModel(personList);
    }
}

But when I display the table the result is something like this:

| No | Last Name | First Name |
|----|-----------|------------|
| 1  | Smith     | John       |
| 1  | Perez     | Juan       |
| 1  | Stallman  | Richard    |
| 1  | Gosling   | James      |
| 1  | Cagatay   | Civici     |
| 1  | Doe       | Jane       |

Is there something am I missing? Any guide/help is appreciated. Thanks in advance

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Alvaro Pedraza
  • 1,176
  • 6
  • 20
  • 44
  • Other Solution [binding="#{table}" By BalusC](http://stackoverflow.com/questions/14633008/jsf-2-datatable-row-index-without-datamodel) – Ravi May 04 '16 at 02:39

3 Answers3

2

The row index is stored in DataModel and defaults to -1. You're creating a new ListDataModel instance in getter method. UIData components calls getter method on every iteration.

Just stop creating models and doing other business logic in getter methods.

As your bean is view scoped and DataModel is not serializable, best is to lazy load it.

@ViewScoped
public class PersonBean implements Serializable {

    private List<Person> personList;
    private transient DataModel<Person> personListAsModel;

    @EJB
    private PersonService personService;

    @PostConstruct    
    public void init() {
        personList = personService.list();
    }

    public ListDataModel<Person> getPersonListAsModel() {
        if (personListAsModel == null) {
            personListAsModel = new ListDataModel(personList);
        }
        return personListAsModel;
    }

}

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
1

I solved the problem (at least for now) by doing:

<p:dataTable value="#{personBean.personList}" var="person">
  <p:column headerText="No">
    <h:outputText value="#{personBean.personList.indexOf(person) + 1}"/>
  </p:column>
...

As I see, it's a clean option, but remains as a mystery why ListDataModel doesn't work.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Alvaro Pedraza
  • 1,176
  • 6
  • 20
  • 44
-1

Not getting this line :

 <h:outputText value="#{personBean.personListAsModel.rowIndex + 1}"/>

that's you are trying to access rowIndex of a datamodel but you are not iterating in this line so it is giving you index 1 always....you are iterating from datatable and you have defined var for that so use that

<h:outputText value="#{person.rowIndex + 1}"/>

it should be resolved lemme know if not.

techipank
  • 432
  • 4
  • 17