Reputation: 1870
I am trying to use <p:columns>
but it renders nothing when I feed it with a List
that is a property of the objects the enclosing <p:dataTable>
is iterating over.
I can use that list succesfully in <ui:repeat>
or access one of it's entries inside the <p:dataTable>
using <p:column>
. <p:columns>
when a assign the list to a property of the backing bean.
But when using the property of the Car
-object nothing is rendered at that place. There is no error message etc.
The part about Dynamic Columns
in the Primefaces 4.0 User Guide confuses me as in the code example cars[colIndex]
is used but still the <p:columns>
has a value other than cars
- I don't how this is supposed to be used.
Additional Info:
I need the list of columns for <p:columns>
to be part of the class as I want to reuse the code for several classes with different properties and thus require different columns.
the Facelets code:
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui">
<h:head></h:head>
<h:messages />
<h:form>
<!-- works -->
<ui:repeat value="#{carBean.cars}" var="car">
ui:repeat: #{car.features}
</ui:repeat>
<p:dataTable value="#{carBean.cars}" var="car">
<!-- works too -->
<p:column>
p:column: #{car.features.get(0)}
</p:column>
<!-- works too -->
<p:columns value="#{carBean.beanFeatures}" var="feature" columnIndexVar="colIndex">
carBean p:columns: #{feature}
</p:columns>
<!-- renders nothing -->
<p:columns value="#{car.features}" var="feature" columnIndexVar="colIndex">
car p:columns: #{feature}
</p:columns>
</p:dataTable>
</h:form>
</html>
the backing bean:
package myPackage;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
@ManagedBean
@ViewScoped
public class CarBean implements Serializable {
List<Car> cars;
List<String> beanFeatures;
@PostConstruct
public void init(){
cars = new ArrayList<Car>();
Car car1 = new Car();
car1.features.add("windows");
car1.features.add("fridge");
Car car2 = new Car();
car2.features.add("wheels");
car2.features.add("nuclear engine");
cars.add(car1);
cars.add(car2);
beanFeatures = car1.getFeatures();
}
public List<Car> getCars() {
return cars;
}
public void setCars(List<Car> cars) {
this.cars = cars;
}
public List<String> getBeanFeatures() {
return beanFeatures;
}
public void setBeanFeatures(List<String> beanFeatures) {
this.beanFeatures = beanFeatures;
}
}
the Car
class
package myPackage;
import java.util.ArrayList;
import java.util.List;
public class Car {
List<String> features = new ArrayList<String>();
public List<String> getFeatures() {
return features;
}
public void setFeatures(List<String> features) {
this.features = features;
}
}
Upvotes: 2
Views: 1284
Reputation: 31679
That's basically not the way Primefaces dynamic columns work. Your dynamic columns must be binded against a Managed Bean's property directly. There's no choice to dinamically stablish that value for each car iteration.
So, in short words, is not possible to have different columns for different table values. Table columns can be dinamically rendered, but this must be decided before PrimeFaces starts looping over the values.
If you have a look at Primefaces showcase, you'll find that.
However, it's possible to loop over car's features in the same columns using a ui:repeat
, so you could have all its features in a single column:
<p:column>
<ui:repeat value="#{car.features}" var="feature">
#{feature}
</ui:repeat>
</p:column>
Upvotes: 1