繁体   English   中英

JSF PrimeFaces在dataTable中的inputText

[英]JSF PrimeFaces inputText inside dataTable

JSF-2.0,Mojarra 2.1.19,PrimeFaces 3.4.1

问题摘要 p:inputTextp:dataTable有一个p:inputTextp:remoteCommand触发了inputText操作, p:remoteCommand dataTable行索引作为参数传递给f:setPropertyActionListener 但它总是传递dataTable的最后一行,而不是包含当前单击的p:inputText的行的索引。


从我之前的问题中可以看出,我正在尝试使用p:inputText作为Facebook等等状态的评论接受者。实现包括p:dataTable 它的行代表每个状态。 似乎:

<p:dataTable id="dataTable" value="#{statusBean.statusList}" var="status"
                     rowIndexVar="indexStatusList">
    <p:column>
        <p:panel id="statusRepeatPanel">
            <p:remoteCommand name="test" action="#{statusBean.insertComment}"
                update="statusRepeatPanel">
                <f:setPropertyActionListener 
                    target="#{statusBean.indexStatusList}"
                    value="#{indexStatusList}">
                </f:setPropertyActionListener>
            </p:remoteCommand>
            <p:inputText id="commentInput" value="#{statusBean.newComment}"
                onkeypress="if (event.keyCode == 13) { test(); return false; }">
            </p:inputText>
        </p:panel>
    </p:column>
</p:dataTable>

上面的代码表示当按下回车键时,触发p:remoteCommand调用托管bean的insert方法。

@ManagedBean
@ViewScoped
public class StatusBean {
    List<Status> statusList = new ArrayList<Status>();
    public int indexStatusList;
    public String newComment
    //getters and setters
    public void insertComment() {
        long statusID = findStatusID(statusList.get(indexStatusList));
        statusDao.insert(this.newComment,statusID)
    }

我们一起调试; 假设p:dataTable显示了三种状态,单击p:inputText ,在第二种状态(索引为1)中,键入“relax”并按Enter键。

在调试控制台中,它正确显示“放松”,但它找到错误的状态,因为indexStatusList的值为2,它属于p:statusList 的最后一个状态 它必须是1,它是点击dataTable行的p:inputText的索引。

我认为问题是关于p:remoteCommand ,它接受屏幕上的最后一个索引。


这个怎么运作?

让我们假设有一个p:commandLink而不是p:remoteCommandp:inputText

<p:commandLink action=#{statusBean.insertComment>
      <f:setPropertyActionListener target="#{statusBean.indexStatusList}"
          value="#{indexStatusList}"></f:setPropertyActionListener>

此组件成功传递indexStatusList作为当前单击的一个。

此解决方案中的概念问题在于p:remoteCommand工作方式。 它创建JavaScript函数,其名称在p:remoteCommand name属性中定义。 当你在dataTable它时,它会迭代并创建名为test JavaScript函数,因为这个表中有行,并且最后一个只有一个。 因此,解决方案可以在remoteCommand的名称附加索引,但这很糟糕,因为您将拥有许多不必要的JavaScript函数。 更好的方法是创建一个函数作为pass参数。 所以在datatable之外定义remoteCommand

<p:remoteCommand name="test" action="#{statusBean.insertComment}" update="statusRepeatPanel">

并在你的onkeypress事件中调用这样的test函数:

test([{ name: 'rowNumber', value: #{indexStatusList} }])

这将在您的AJAX请求中传递rowNumber参数。 在支持bean的insertComment()方法中,您可以读取此参数并使用它执行任何操作:

FacesContext context = FacesContext.getCurrentInstance();
Map map = context.getExternalContext().getRequestParameterMap();
Integer rowNumber = Integer.parseInt(map.get("rowNumber").toString());

注意:如您在每行中更新面板,也许你可以改变update的属性remoteCommand@parent所以这将所有行工作。

编辑:您可以使用Java方法中的以下代码更新特定行中的特定面板:

RequestContext.getCurrentinstance().update("form:dataTable:" + rowNumber + ":statusRepeatPanel")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM