DataTable
DataTable presents a collection of data in a responsive tabular layout. Columns can be given priority to define visibility depending on screen size and optional reflow mode enables stacking on smaller screens.
Source
<h:form id="form"> <p:growl id="msgs" showDetail="true" /> <h2 class="first">Column Toggle</h2> <p:dataTable var="car" value="#{dtMobileView.cars1}" tableStyleClass="ui-table-columntoggle"> <p:column headerText="Id" priority="1"> <h:outputText value="#{car.id}" /> </p:column> <p:column headerText="Year" priority="2"> <h:outputText value="#{car.year}" /> </p:column> <p:column headerText="Brand" priority="1"> <h:outputText value="#{car.brand}" /> </p:column> <p:column headerText="Color" priority="2"> <h:outputText value="#{car.color}"/> </p:column> </p:dataTable> <h2>Reflow Mode</h2> <p:dataTable var="car" value="#{dtMobileView.cars2}" reflow="true"> <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:dataTable> <h2>Advanced</h2> <p:dataTable id="cars" var="car" value="#{dtMobileView.cars3}" selectionMode="multiple" selection="#{dtSelectionView.selectedCars}" rowKey="#{car.id}" paginator="true" rows="10" tableStyleClass="ui-table-columntoggle"> <p:ajax event="swipeleft" listener="#{dtMobileView.onRowSwipeLeft}" update="@parent:msgs" /> <p:ajax event="swiperight" listener="#{dtMobileView.onRowSwipeRight}" update="@this"/> <p:column headerText="Id" sortBy="#{car.id}" priority="1"> <h:outputText value="#{car.id}" /> </p:column> <p:column headerText="Year" sortBy="#{car.year}" priority="2"> <h:outputText value="#{car.year}" /> </p:column> <p:column headerText="Brand" sortBy="#{car.brand}" priority="1"> <h:outputText value="#{car.brand}" /> </p:column> <p:column headerText="Color" sortBy="#{car.color}" priority="2"> <h:outputText value="#{car.color}" /> </p:column> </p:dataTable> </h:form>
package org.primefaces.showcase.mobile; import java.io.Serializable; import java.util.List; import javax.annotation.PostConstruct; import javax.faces.application.FacesMessage; import javax.faces.bean.ManagedBean; import javax.faces.bean.ManagedProperty; import javax.faces.bean.ViewScoped; import javax.faces.context.FacesContext; import org.primefaces.event.SelectEvent; import org.primefaces.mobile.event.SwipeEvent; import org.primefaces.showcase.domain.Car; import org.primefaces.showcase.service.CarService; @ManagedBean(name="dtMobileView") @ViewScoped public class DatatableView implements Serializable { private List<Car> cars1; private List<Car> cars2; private List<Car> cars3; private List<Car> selectedCars; @ManagedProperty("#{carService}") private CarService service; @PostConstruct public void init() { cars1 = service.createCars(10); cars2 = service.createCars(10); cars3 = service.createCars(50); } public List<Car> getCars1() { return cars1; } public List<Car> getCars2() { return cars2; } public List<Car> getCars3() { return cars3; } public void setService(CarService service) { this.service = service; } public List<Car> getSelectedCars() { return selectedCars; } public void setSelectedCars(List<Car> selectedCars) { this.selectedCars = selectedCars; } public void onRowSwipeRight(SwipeEvent event) { Car car = ((Car) event.getData()); cars3.remove(car); if(selectedCars != null && !selectedCars.isEmpty()) { selectedCars.remove(car); } } public void onRowSwipeLeft(SwipeEvent event) { FacesMessage msg = new FacesMessage("Swiped Left", ((Car) event.getData()).getId()); FacesContext.getCurrentInstance().addMessage(null, msg); } }
package org.primefaces.showcase.service; import java.util.ArrayList; import java.util.List; import java.util.UUID; import javax.faces.bean.ApplicationScoped; import javax.faces.bean.ManagedBean; import org.primefaces.showcase.domain.Car; @ManagedBean(name = "carService") @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)]; } public int getRandomPrice() { return (int) (Math.random() * 100000); } public 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); } }