jsf - 具有动态列的 primefaces 数据表的动态数量 - 列数错误

标签 jsf jsf-2 primefaces datatable

我必须在单个页面上显示多个动态数量的 primefaces 数据表和动态列。如果每个动态数据表中的列数相同,则一切正常。

当不同表中的列数不同时会出现错误行为。所有表格显示的列数与列表中第一个表格中的列数一样多。

测试于:

  • Primefaces:3.4 和 4.0
  • JSF 2.0

这是演示问题的代码:

编辑: 以下代码将不同数量的列分配给不同的表。第一个表应该有 8 列,第二个应该有 7 列,依此类推。但是所有的表都占用了 8 列;即他们从第一个表中获取列数。

预期输出:5 个数据表。第一个表有 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));
            }
        }
    }
}

汽车.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;
    }

}

最佳答案

我认为您应该使用 Primefaces 3.3,因为它的显示与我的情况一样。 现在这会起作用,但我也会尝试在 Primefaces 4.0 上修复它。

请找到相同的附加图像。 Image

关于jsf - 具有动态列的 primefaces 数据表的动态数量 - 列数错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21329734/

相关文章:

Java JSF 数据表定义

css - 对话框显示在页面底部

java - 如何在 JSF 应用程序中注销

java - 找不到方法 getComponent()

validation - 如何禁用一个输入字段的默认 JSF 验证和转换

mysql - Spring JDBC 模板 - 如何通过单个查询检索具有多个参数的多个结果

file-upload - 即使在正确上传后,Primefaces FileUpload 错误仍然存​​在

java - 在按下特定按钮时禁用 ICEFaces 表单上的验证

java - 在 XHTML 表单/Java PrimeFaces 中的多个按钮上使用 primefaces 对话框

jquery - 查找具有动态 ID 的元素