我必須在單個頁面上顯示具有動態列的多個動態數量的primefaces數據表。如果每個動態數據表中的列數相同,則一切正常。具有動態列的動態數量的primefaces數據表 - 錯誤列數
錯誤的行爲發生在不同表中的列數不同時。所有表格顯示列表中第一個表格的列數。
測試於:
- Primefaces:3.4和4.0
- JSF 2.0
這裏是說明該問題的代碼:
編輯:下面的代碼將不同數量的列分配給不同的表。第一張表應該有8列,第二張應該有7列,依此類推。但是所有的桌子都佔據了8列;即它們從第一個表中獲取列的數量。
預期輸出: 5 datatables。第一個表格有8列,第二個表格7列,第三個表格6列,等等。 Expected Output Screenshot -- This is actually the output on pf 3.3
實際產出: 5個表,全部有8列。顯示的數據是正確的。但空的額外列也正在顯示,不應該顯示。 Actual Output Screenshot
test2.xhtml:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui">
<f:view contentType="text/html">
<h:head>
</h:head>
<h:body>
<h:form>
<ui:repeat value="#{tableBean.tables}" var="table">
<p:dataTable id="cars" var="car" value="#{table.carsSmall}"
style="font-size: 12px;'width: 70%;">
<p:columns value="#{table.columns}" var="column"
columnIndexVar="colIndex">
<f:facet name="header">
#{column.header}
</f:facet>
<h:outputText value="#{car[column.property]}"></h:outputText>
</p:columns>
</p:dataTable>
<br />
</ui:repeat>
</h:form>
</h:body>
</f:view>
</html>
TableBean.java:
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
@ManagedBean(name = "tableBean")
@ViewScoped
public class TableBean implements Serializable
{
private static final long serialVersionUID = 1L;
private final static List<String> VALID_COLUMN_KEYS = Arrays.asList("model", "manufacturer", "year", "color");
private final static String[] colors;
private final static String[] manufacturers;
private String columnTemplate = "model manufacturer year";
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> carsSmall;
private List<ColumnModel> columns = new ArrayList<ColumnModel>();;
private List<TableBean> tables;
public List<TableBean> getTables()
{
if (tables == null)
{
tables = new ArrayList<TableBean>();
for (int i = 0; i < 5; i++)
{
TableBean t = new TableBean();
for (int j = 5; j > i; j--)
{
t.columnTemplate += " year";
t.createDynamicColumns();
}
tables.add(t);
}
}
return tables;
}
public TableBean()
{
carsSmall = new ArrayList<Car>();
populateRandomCars(carsSmall, 9);
createDynamicColumns();
}
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 List<ColumnModel> getColumns()
{
return columns;
}
public String[] getManufacturers()
{
return manufacturers;
}
public String[] getColors()
{
return colors;
}
static public class ColumnModel implements Serializable
{
private static final long serialVersionUID = 1L;
private String header;
private String property;
public ColumnModel(String header, String property)
{
this.header = header;
this.property = property;
}
public String getHeader()
{
return header;
}
public String getProperty()
{
return property;
}
@Override
public String toString()
{
return "[header=" + header + ", property=" + property + "]";
}
}
public void createDynamicColumns()
{
String[] columnKeys = columnTemplate.split(" ");
columns.clear();
for (String columnKey : columnKeys)
{
String key = columnKey.trim();
if (VALID_COLUMN_KEYS.contains(key))
{
columns.add(new ColumnModel(columnKey.toUpperCase(), columnKey));
}
}
}
}
Car.java:
import java.io.Serializable;
public class Car implements Serializable
{
private static final long serialVersionUID = 1L;
private String model;
private int year;
private String manufacturer;
private String color;
public Car(String model, int year, String manufacturer, String color)
{
super();
this.model = model;
this.year = year;
this.manufacturer = manufacturer;
this.color = color;
}
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;
}
}
我無法理解您的問題。你的列模板包含'模型製造商年份',這非常明顯,如果你使用Table類對象't.columnTemplate + =「year」來做到這一點,'它會將列年添加到它。這就是它應該如何表現的方式,而這正是它表現的方式。請讓我們明白你想要做什麼? –
@VinayakPingale其實't.columnTemplate + =「year」'在循環中。它在第一個表中追加5個額外的列,在第二個表中追加4個額外的列,在第三個表中追加3個列等等。因此,第一張表應該有8列,第二張應該有7列,依此類推。但是所有的表格都佔用了8列。 –
PS:這是測試代碼,而不是實際的代碼。這只是在這裏,以便任何人都可以立即複製並粘貼並運行此代碼以查看輸出。 –