ColumnToggler is a helper component of datatable to toggle the visibility of columns.
Documentation| Id | Year | Brand | Color | Price |
|---|---|---|---|---|
| f5a65709 | 1998 | Volkswagen | Red | $18,236.00 |
| 6c691833 | 1968 | Renault | Green | $30,576.00 |
| 472ccb41 | 1974 | Volkswagen | Black | $24,135.00 |
| f5288d44 | 1975 | Audi | Black | $79,456.00 |
| dd57ae6f | 1972 | Renault | Orange | $55,850.00 |
| 92b59273 | 1969 | Fiat | White | $51,216.00 |
| 15c83915 | 1986 | Audi | White | $73,911.00 |
| 824e598e | 1980 | Renault | Brown | $79,487.00 |
| f518bdaa | 1975 | Honda | Green | $35,431.00 |
| 6318c8cf | 1960 | Volvo | Yellow | $97,381.00 |
<style type="text/css">
.ui-datatable-header {
height: 50px
}
</style>
<p:dataTable id="cars" var="car" value="#{dtBasicView.cars}">
<f:facet name="header">
List of Cars
<p:commandButton id="toggler" type="button" value="Columns" style="float:right" icon="pi pi-align-justify" />
<p:columnToggler datasource="cars" trigger="toggler" />
</f:facet>
<p:column headerText="Id">
<h:outputText value="#{car.id}" />
</p:column>
<p:column headerText="Year">
<h:outputText value="#{car.year}" />
</p:column>
<p:column headerText="Brand">
<h:outputText value="#{car.brand}" />
</p:column>
<p:column headerText="Color">
<h:outputText value="#{car.color}" />
</p:column>
<p:column headerText="Price">
<h:outputText value="#{car.price}">
<f:convertNumber type="currency" currencySymbol="$" />
</h:outputText>
</p:column>
</p:dataTable>@Named("dtBasicView")
@ViewScoped
public class BasicView implements Serializable {
private List<Car> cars;
@Inject
private CarService service;
@PostConstruct
public void init() {
cars = service.createCars(10);
}
public List<Car> getCars() {
return cars;
}
public void setService(CarService service) {
this.service = service;
}
}@Named
@ApplicationScoped
public class CarService {
private final static String[] colors;
private final static String[] brands;
static {
colors = new String[10];
colors[0] = "Black";
colors[1] = "White";
colors[2] = "Green";
colors[3] = "Red";
colors[4] = "Blue";
colors[5] = "Orange";
colors[6] = "Silver";
colors[7] = "Yellow";
colors[8] = "Brown";
colors[9] = "Maroon";
brands = new String[10];
brands[0] = "BMW";
brands[1] = "Mercedes";
brands[2] = "Volvo";
brands[3] = "Audi";
brands[4] = "Renault";
brands[5] = "Fiat";
brands[6] = "Volkswagen";
brands[7] = "Honda";
brands[8] = "Jaguar";
brands[9] = "Ford";
}
public List<Car> createCars(int size) {
List<Car> list = new ArrayList<Car>();
for(int i = 0 ; i < size ; i++) {
list.add(new Car(getRandomId(), getRandomBrand(), getRandomYear(), getRandomColor(), getRandomPrice(), getRandomSoldState()));
}
return list;
}
private String getRandomId() {
return UUID.randomUUID().toString().substring(0, 8);
}
private int getRandomYear() {
return (int) (Math.random() * 50 + 1960);
}
private String getRandomColor() {
return colors[(int) (Math.random() * 10)];
}
private String getRandomBrand() {
return brands[(int) (Math.random() * 10)];
}
private int getRandomPrice() {
return (int) (Math.random() * 100000);
}
private boolean getRandomSoldState() {
return (Math.random() > 0.5) ? true: false;
}
public List<String> getColors() {
return Arrays.asList(colors);
}
public List<String> getBrands() {
return Arrays.asList(brands);
}
}