【问题标题】:Retriving the row index of edited cell in <p:dataTable> with <p:rowEditor>使用 <p:rowEditor> 检索 <p:dataTable> 中已编辑单元格的行索引
【发布时间】:2012-10-23 04:55:14
【问题描述】:

使用&lt;p:rowEditor&gt;时,如何检索已编辑单元格的行索引?

以下是相关代码:

<p:dataTable id="datasetParamDt" var="datasetParam" value="#{projectCampaignManagementMB.allParametersList}" editable="true" rowIndexVar="rowIndex">  
    <p:ajax event="rowEdit"  listener="#{projectCampaignManagementMB.onParameterValueEdit}" update=":campaignForm:growl" />  
    <p:ajax event="rowEditCancel" listener="#{projectCampaignManagementMB.onParameterValueCancel}" update=":campaignForm:growl"/>  

    <p:column headerText="Value">  
        <p:cellEditor>
            <f:facet name="output"> 
                <h:outputText value="#{projectCampaignManagementMB.paramValue}" />
            </f:facet> 
            <f:facet name="input"> 
                <p:inputText value="#{projectCampaignManagementMB.paramValue}" /> 
            </f:facet>     
        </p:cellEditor>
    </p:column> 

    <p:column headerText="Options" style="width:50px">  
        <p:rowEditor />
    </p:column>  
</p:dataTable>

在简单的数据表中,我曾经在&lt;p:commandLink&gt; 中使用&lt;f:param&gt;,如下所示:

<f:param name="index" value="#{rowIndex}" />

但是,在我使用&lt;p:rowEditor&gt; 的情况下,当用户在onParameterValueEdit() 方法中验证编辑的值时,如何获取行索引?

public void onParameterValueEdit(RowEditEvent event) {  
    int index = ... // index of the row to which the edited cell belongs    
    parametersValue.set(Integer.parseInt(index),paramValue);
}

【问题讨论】:

  • 您究竟需要了解哪些信息?
  • 等待...您的输入/输出值绑定到支持 bean,而不是数据模型的迭代行对象!这有什么意义?您应该使用value="#{datasetParamDt.paramValue}" 而不是value="#{projectCampaignManagementMB.paramValue}"。我想知道现在是否不需要了解行索引。
  • 是的,这是我的问题,paramValue 不是 DataSet 类中的属性

标签: jsf primefaces


【解决方案1】:

这种设计毫无意义。只需将组件值直接绑定到parametersValue 列表即可。

<h:outputText value="#{projectCampaignManagementMB.parametersValue[rowIndex]}" />
...
<p:inputText value="#{projectCampaignManagementMB.parametersValue[rowIndex]}" /> 

这样您也不再需要了解行索引,并且您可能也不再需要那些动作侦听器方法了。我只会将属性名称重命名为 parameterValues 以修复不正确的英文。


与具体问题无关,即使您在这里没有使用任何验证,但由于您提到了“验证”一词,我想补充一点,验证应该由普通验证器,而不是通过 action(listener) 方法。使用requiredvalidator 属性,或&lt;f:validator&gt;&lt;f:validateXxx&gt; 标签。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-03
    • 2023-03-10
    • 1970-01-01
    相关资源
    最近更新 更多