注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

流星永恒的博客

JSF,Facelets,Rich(Prime)Faces,和java的笔记

 
 
 

日志

 
 

Primefaces dynamic columns and row editing (Can be partly resolved by page author)  

2013-12-25 16:27:37|  分类: jsf2.0 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

Tested with version 3.4.2.

One limitation: you should predefine all editing component with rendered attribute, it will render match editing component by your column.type.


Following page code is not real page code, so edit it when you tested it.


<p:datatable value="#{dynamicColumnsBean.carsSmall}"

filteredValue="#{dynamicColumnsBean.filteredCars}" editable="true">
<p:ajax event="rowEdit" listener="#{dynamicColumnsBean.onEdit}" update="cars"/>
<p:ajax event="rowEditCancel" listener="#{dynamicColumnsBean.onCancel}"/>
<p:columns value="#{dynamicColumnsBean.columns}" var="column"
columnIndexVar="colIndex" sortBy="#{column.property}"
filterBy="#{column.property}" styleClass="ui-editable-column">
<f:facet name="header">
#{column.header}
</f:facet>
<p:cellEditor rendered="#{true}"> <!-- column.canEdit -->
<f:facet name="output">
<h:outputText value="#{car[column.property]}" />
</f:facet>
<f:facet name="input">
<!-- defined all your edit type component -->
<p:inputText value="#{car[column.property]}" style="width:100%" label="model" rendered="#{column.type == 'input'}"/>
<p:selectOneMenu value="#{car[column.property]}" style="width:100px;" rendered="#{column.type=='select'}">
<f:selectItems value="#{car.propertyValues[column.property].entrySet()}" var="item"
itemLabel="#{item.key}" itemValue="#{item.value}"/>
</p:selectOneMenu>


<p:selectOneCheckbox ...rendered="#{column.type=='select'}">

<f:selectItems../>

</p:selectOnCheckbox>

</f:facet>
</p:cellEditor>
</p:columns>
<p:column style="width:6%">
<p:rowEditor />
</p:column>

</p:datatable>

Bean

/**
* @author daniely
*
*/
@Named
@ViewScoped
public class DynamicColumnsBean implements Serializable {

/**
* serial number
*/
private static final long serialVersionUID = 2310314703188061513L;

private final static List<String> VALID_COLUMN_KEYS = Arrays.asList(
"model", "manufacturer", "year", "color");

private final static String[] colors;

private final static String[] TYPES = new String[] { "input", "input",
"select", "input" };

private final static String[] manufacturers;

private String columnTemplate = "model manufacturer year";

private Map<String, Map<String, Object>> values;

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 List<Car> filteredCars;

private List<Car> carsSmall;

private List<ColumnModel> columns = new ArrayList<ColumnModel>();;

public void onCancel(RowEditEvent event) {
System.out.println("onCancel...");
Car car = (Car) event.getObject();
System.out.println(car.getModel());
Messages.addGlobalInfo("Cancel edit car :{0}", car.getModel());
}

public void onEdit(RowEditEvent event) {
System.out.println("onEdit...");
Car car = (Car) event.getObject();
System.out.println(car.getModel());
Messages.addGlobalInfo("Edit car :{0}", car.getModel());
}

public DynamicColumnsBean() {
carsSmall = new ArrayList<Car>();
Map<String, Object> years = new LinkedHashMap<String, Object>();
for (int i = 2008; i < 2015; i++) {
years.put(i + "", i);
}
values = new HashMap<String, Map<String, Object>>();
values.put("year", years);
populateRandomCars(carsSmall, 9);
createDynamicColumns();
}

private void populateRandomCars(List<Car> list, int size) {
for(int i = 0 ; i < size ; i++){
Car c = new Car(getRandomModel(), getRandomYear(), getRandomManufacturer(), getRandomColor());
c.setPropertyValues(values);
list.add(c);
}
}

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 List<ColumnModel> getColumns() {
return columns;
}

public String[] getManufacturers() {
return manufacturers;
}

public String[] getColors() {
return colors;
}

public List<Car> getFilteredCars() {
return filteredCars;
}

public void setFilteredCars(List<Car> filteredCars) {
this.filteredCars = filteredCars;
}

static public class ColumnModel implements Serializable {

private String header;
private String property;
private String type;

public String getType() {
return type;
}

public ColumnModel(String header, String property, String type) {
this.header = header;
this.property = property;
this.type = type;
}

public String getHeader() {
return header;
}

public String getProperty() {
return property;
}
}

public String getColumnTemplate() {
return columnTemplate;
}

public void setColumnTemplate(String columnTemplate) {
this.columnTemplate = columnTemplate;
}

public void updateColumns() {
// reset table state
// UIComponent table =
// FacesContext.getCurrentInstance().getViewRoot().findComponent(":form:cars");
// table.setValueExpression("sortBy", null);
// update columns
createDynamicColumns();
}

public void createDynamicColumns() {
System.out.println("createDynamicColumns...");
System.out.println(columnTemplate);
String[] columnKeys = columnTemplate.split(" ");
columns.clear();

int i = 0;
for (String columnKey : columnKeys) {
String key = columnKey.trim();
if (VALID_COLUMN_KEYS.contains(key)) {
columns.add(new ColumnModel(columnKey.toUpperCase(), columnKey,
TYPES[i]));
i++;
}
}
}

}

Car.java

public class Car {

private String model;
private int year;
private String manufacturer;
private String color;
private Map<String, Map<String, Object>> propertyValues;

public Car(String randomModel, int randomYear, String randomManufacturer,
String randomColor) {
this.model = randomModel;
this.year = randomYear;
this.manufacturer = randomManufacturer;
this.color = randomColor;
}

public String getModel() {
return model;
}

public void setModel(String model) {
this.model = model;
}

public int getYear() {
return year;
}

public void setYear(int year) {
this.year = year;
}

public String getManufacturer() {
return manufacturer;
}

public void setManufacturer(String manufacturer) {
this.manufacturer = manufacturer;
}

public String getColor() {
return color;
}

public void setColor(String color) {
this.color = color;
}

public Map<String, Map<String, Object>> getPropertyValues() {
return propertyValues;
}

public void setPropertyValues(Map<String, Map<String, Object>> propertyValues) {
this.propertyValues = propertyValues;
}
}

  评论这张
 
阅读(781)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017