具有可变列和特定可编辑单元格的PrimeFaces dataTable [英] PrimeFaces dataTable with variable columns and specific editable cells

查看:46
本文介绍了具有可变列和特定可编辑单元格的PrimeFaces dataTable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建一个表,其中表头列表来自模型.表的内容也存储在模型中,并在数据上的p:dataTable循环中根据列名显示内容.

I need to create a table where the headers list are brought from a model. The table contents are also stored in the model and p:dataTable loop on the data to show the content based on the column name.

问题是我需要使某些特定的单元格可编辑.对于输出数据没有问题,因为我使用了同时接受实体和列名称并根据列名称从实体返回正确信息的模型方法.问题出在我不知道如何在实体中设置的可编辑单元格的输入中.

The issue is that I need to make some specific cells editable. For outputting data there is no problem since I use model method which takes both the entity and the column name and return the correct info from the entity based on the column name. The issue is with inputs of the editable cells which I don't know how to set in the entity.

<p:dataTable id="processTable"  var="entity" value="#{home.process.headerEntities}" tableStyle="width:auto" draggableColumns="true" editable="true" editMode="cell">

    <p:columns value="#{home.process.columns}" var="columnHead" >

        <f:facet name="header">
            <h:outputText value="#{columnHead}"/>
        </f:facet>

        <p:cellEditor>
            <f:facet name="output">
                <h:outputText value="#{home.process.getData(entity, columnHead)}" />
            </f:facet>
            <f:facet name="input">
                <p:inputText value="#{home.process.getData(entity, columnHead)}" rendered="#{home.process.isEditable(columnHead)}"  style="width:100%" />
            </f:facet>
        </p:cellEditor>


    </p:columns>

</p:dataTable>

根据最佳答案进行更改

After change based on BEST ANSWER

<p:dataTable id="processTable"  var="entity" value="#{home.process.headerEntities}" tableStyle="width:auto" draggableColumns="true" editable="true" editMode="cell">

                <p:columns value="#{home.process.columns}" var="columnHead" >

                    <f:facet name="header">
                        <h:outputText value="#{columnHead}"/>
                    </f:facet>

                    <p:cellEditor>
                        <f:facet name="output">
                            <h:outputText value="#{entity[home.process.columnPropertyMap[columnHead]]}" />
                        </f:facet>
                        <f:facet name="input">
                            <p:inputText value="#{entity[home.process.columnPropertyMap[columnHead]]}" rendered="#{home.process.isEditable(columnHead)}"  style="width:100%" />
                        </f:facet>
                    </p:cellEditor>


                </p:columns>

            </p:dataTable>

推荐答案

输入组件的值必须绑定到可写的值表达式.您所拥有的是直接的getter方法调用,因此本质上是只读的.这确实是行不通的.您需要指定#{entity}的属性名称.您可以使用花括号表示法将属性名称指定为变量,例如#{entity[propertyName]}.

The input component's value must be bound to a writable value expression. What you've there is a direct getter method invocation and thus essentially read-only. This is indeed not going to work. You need to specify a property name of the #{entity}. You can use the brace notation to specify the property name as a variable like so #{entity[propertyName]}.

所以,基本上:

<p:dataTable value="#{bean.entities}" var="entity" editable="true" editMode="cell">
    <p:columns value="#{bean.propertyNames}" var="propertyNames">
        <p:cellEditor>
            <f:facet name="output">
                #{entity[propertyName]}
            </f:facet>
            <f:facet name="input">
                <p:inputText value="#{entity[propertyName]}" />
            </f:facet>
        </p:cellEditor>
    </p:columns>
</p:dataTable>

关于列标题,请重构为Map<String, String>,其中键为propertyName,值为标题.

As to the column header, rather refactor out that into a Map<String, String> where the key is the propertyName and the value is the header.

        <f:facet name="header">
            #{bean.columnHeaders[propertyName]}
        </f:facet name="header">

或更妙的是,对于propertyName代表束密钥的一部分的情况,使用普通的i18n资源束.

Or better yet, use a normal i18n resource bundle for that where the propertyName represents part of the bundle key.

        <f:facet name="header">
            #{bundle['table.column.header.' += propertyName]}
        </f:facet name="header">

关于可编辑检查,请将propertyNameeditable包装在另一个bean中(如果不想使用i18n捆绑包,也可以包装columnHeader),例如Field,然后使用如下所示:

As to the editable check, rather wrap propertyName and editable in another bean (and perhaps also columnHeader if you don't want to use a i18n bundle), e.g. Field and then use like below:

    <p:columns value="#{bean.fields}" var="field">
        <p:cellEditor>
            <f:facet name="output">
                #{entity[field.propertyName]}
            </f:facet>
            <f:facet name="input">
                <p:inputText value="#{entity[field.propertyName]}" rendered="#{entity[field.editable]}" />
            </f:facet>
        </p:cellEditor>
    </p:columns>

总而言之,它归结为准备并提供视图期望的正确模型.这样就不必getData()了.

All in all, it just boils down to preparing and providing the right model the view expects. This way the getData() thing isn't necessary.

这篇关于具有可变列和特定可编辑单元格的PrimeFaces dataTable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆