DataTable - Resizable Columns
DataTable columns can be resized by dragging. Optional "colResize" ajax behavior can be used to get notified about new column widths.
Source
<h:form id="form">
<p:growl id="growl" showDetail="true"/>
<h3>Basic Resize</h3>
<p:dataTable var="car" value="#{tableBean.carsSmall}" resizableColumns="true" id="basicTable">
<p:column headerText="Model" style="width:125px" id="model">
#{car.model}
</p:column>
<p:column headerText="Year" style="width:125px" id="year">
<h:outputText value="#{car.year}" />
</p:column>
<p:column headerText="Manufacturer" style="width:125px" id="manufacturer">
<h:outputText value="#{car.manufacturer}" />
</p:column>
<p:column headerText="Color" style="width:125px" id="color">
<h:outputText value="#{car.color}" />
</p:column>
</p:dataTable>
<h3>Ajax Resize</h3>
<p:dataTable var="car" value="#{tableBean.carsSmall}" resizableColumns="true" id="ajaxTable">
<p:ajax event="colResize" update=":form:growl" listener="#{tableBean.onResize}" />
<p:column headerText="Model" style="width:125px" id="model">
#{car.model}
</p:column>
<p:column headerText="Year" style="width:125px" id="year">
<h:outputText value="#{car.year}" />
</p:column>
<p:column headerText="Manufacturer" style="width:125px" id="manufacturer">
<h:outputText value="#{car.manufacturer}" />
</p:column>
<p:column headerText="Color" style="width:125px" id="color">
<h:outputText value="#{car.color}" />
</p:column>
</p:dataTable>
</h:form>
package org.primefaces.examples.view;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import org.primefaces.examples.domain.Car;
import org.primefaces.event.ColumnResizeEvent;
public class TableBean implements Serializable {
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;
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 List<Car> getCarsSmall() {
return carsSmall;
}
private int getRandomYear() {
return (int) (Math.random() * 50 + 1960);
}
private String getRandomColor() {
return colors[(int) (Math.random() * 10)];
}
private String getRandomManufacturer() {
return manufacturers[(int) (Math.random() * 10)];
}
private String getRandomModel() {
return UUID.randomUUID().toString().substring(0, 8);
}
public void onResize(ColumnResizeEvent event) {
FacesMessage msg = new FacesMessage("Column " + event.getColumn().getClientId() + " resized", "W:" + event.getWidth() + ", H:" + event.getHeight());
FacesContext.getCurrentInstance().addMessage(null, msg);
}
}
