3

I am trying this example in PrimeFaces. I understand only the first few lines of the code.

<p:dataTable var="car" value="#{tableBean.carsSmall}"
    emptyMessage="No cars found with given criteria">

     <f:facet name="header">
        <p:outputPanel>
            <h:outputText value="Search all fields:" />
            <p:inputText id="globalFilter" onkeyup="carsTable.filter()" style="width:150px" />
        </p:outputPanel>
    </f:facet>

It could display a search box here. The reaming lines of code would be to add the column and populate the columns with data. I don't understand what

<p:column filterBy="#{car.model}" 
    headerText="Model" footerText="contains"
    filterMatchMode="contains">
    <h:outputText value="#{car.model}" />
</p:column>`

What is #{car.model} ? it doesn't specify anything call model in the java class. How do I alter my java class to make a column display?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Illep
  • 16,375
  • 46
  • 171
  • 302

2 Answers2

4

The expression variable car is declared to be the var attribute of the dataTable. This means that each unique row in the dataTable component can be referenced in expression language by the variable car.

The model property of car is a Bean property of the Serializable POJO Car. It is assumed that the Car class has a property model meaning a getter getModel() and a setter setModel().

The filterBy attribute of <p:column> specifies that this column header will have its own unique filter text field and that it will filter the rows on car.model property.

The attribute filterMatchMode specifies that the match criteria is contains which means any textual occurence of what is typed into the column filter field will equate as a matched record. See the Primefaces Guide for a complete list of filterMatchMode options.

maple_shaft
  • 10,435
  • 6
  • 46
  • 74
  • Well, i managed to do the same example and it doesn't filter as shown in the website. when i type the character '1' i get a totally different filter result. Has anyone come across this issue. and also i get this exception `Caused by: java.lang.InstantiationException: mk.war.Car` – Illep Aug 02 '11 at 11:42
  • Is this in the global filter or in the column filter where you have this problem? Can you edit your question and add the mk.war.Car class? – maple_shaft Aug 02 '11 at 12:12
  • It's the Car.Java class [http://code.google.com/p/primefaces/source/browse/examples/trunk/prime-showcase/src/main/java/org/primefaces/examples/domain/Car.java?r=2274&spec=svn2317] that i added. This error comes when i add ` ` code. all the other columns appear except for this. – – Illep Aug 02 '11 at 16:23
2
private List<Car> carsSmall;

carsSmall is a list that contains Car objects. Car is imported here:

import org.primefaces.examples.domain.Car; 

Car.java Source

Car is the backing bean, it has an attribute model that contains the car's model as a String.

In car #{car.model} is defined here:

<p:dataTable var="car"...>

The dataTable iterates over every element in the list carsSmall and you can access the current element using the name given in the var attribute (here: car). So #{car.model} calls the getModel() method of the current Car object.

Jacob
  • 41,721
  • 6
  • 79
  • 81
  • Well, i managed to do the same example and it doesn't filter as shown in the website. when i type the character '1' i get a totally different filter result. Has anyone come across this issue. and also i get this exception Caused by: java.lang.InstantiationException: mk.war.Car – Illep Aug 02 '11 at 11:41
  • 2
    @Illep Primefaces randomizes the car variable in the bean. So the color will be different then yours and such, the model, year whatever all will be randomized. This means you will never get the same results as the primefaces one, unless you write them all down and add the same cars. – Lyrion Aug 02 '11 at 14:44
  • well, i copied the same class and didn't alter the code at all. I undestand that the values are randomly generated. but yet if i enter '19' in the 'year column', it doesn't filter out all the 'year' starting with '19'. So that's what i need to find – Illep Aug 02 '11 at 16:25