DataTable - Multiple Row Selection
DataTable also supports multiple row selection using metaKey and shiftKey.
Source
<h:form id="form">
<p:dataTable id="cars" var="car" value="#{tableBean.smallCarsModel}" rowKey="#{car.model}"
selection="#{tableBean.selectedCars}" selectionMode="multiple">
<f:facet name="header">
Click "View" button after selecting multiple to see details
</f:facet>
<p:column headerText="Model">
#{car.model}
</p:column>
<p:column headerText="Year">
#{car.year}
</p:column>
<p:column headerText="Manufacturer" >
#{car.manufacturer}
</p:column>
<p:column headerText="Color">
#{car.color}
</p:column>
<f:facet name="footer">
<p:commandButton value="View" icon="ui-icon-search"
update=":form:display" oncomplete="carDialog.show()"/>
</f:facet>
</p:dataTable>
<p:dialog header="Car Detail" widgetVar="carDialog"
width="250" showEffect="explode" hideEffect="explode">
<p:dataList id="display"
value="#{tableBean.selectedCars}"
var="selectedCar" type="definition">
Model: #{selectedCar.model}, Year: #{selectedCar.year}
<f:facet name="description">
<p:graphicImage value="/images/cars/#{selectedCar.manufacturer}.jpg"/>
</f:facet>
</p:dataList>
</p:dialog>
</h:form>
package org.primefaces.examples.view;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import org.primefaces.examples.domain.Car;
public class TableBean {
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";
manufacturers = new String[10];
manufacturers[0] = "Mercedes";
manufacturers[1] = "BMW";
manufacturers[2] = "Volvo";
manufacturers[3] = "Audi";
manufacturers[4] = "Renault";
manufacturers[5] = "Opel";
manufacturers[6] = "Volkswagen";
manufacturers[7] = "Chrysler";
manufacturers[8] = "Ferrari";
manufacturers[9] = "Ford";
}
private final static String[] colors;
private final static String[] manufacturers;
private List<Car> carsSmall;
private Car[] selectedCars;
public TableBean() {
carsSmall = new ArrayList<Car>();
populateRandomCars(carsSmall, 9);
}
private void populateRandomCars(List<Car> list, int size) {
for(int i = 0 ; i < size ; i++)
list.add(new Car(getRandomModel(), getRandomYear(), getRandomManufacturer(), getRandomColor()));
}
public Car[] getSelectedCars() {
return selectedCars;
}
public void setSelectedCar(Car[] selectedCars) {
this.selectedCar = selectedCars;
}
public List<Car> getCarsSmall() {
return this.carsSmall;
}
}
