JSF-2.0,Mojarra 2.1.19,PrimeFaces 3.4.1
Summary of the problem :在 p:remoteCommand
中触发 p:inputText
p:dataTable
和inputText操作,它将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键 .
在调试控制台中,它正确显示"relax",但它找到错误的状态,因为 indexStatusList
的值为2, p:statusList
中属于 the last status . 它必须是1,它是 p:inputText
的索引,它点击了dataTable行 .
我认为问题是关于 p:remoteCommand
,它取决于屏幕上的最后一个索引 .
How it works?
让我们假设有一个 p:commandLink
而不是 p:remoteCommand
和 p:inputText
:
<p:commandLink action=#{statusBean.insertComment>
<f:setPropertyActionListener target="#{statusBean.indexStatusList}"
value="#{indexStatusList}"></f:setPropertyActionListener>
此组件成功传递 indexStatusList
作为当前单击的组件 .
1 回答
这个解决方案中的概念问题在于如何运作.2596553_ . 它创建JavaScript函数,其名称在
p:remoteCommand
的name
属性中定义 . 正如你在dataTable
中所做的那样,它会迭代并创建名为test
的JavaScript函数,就像这个表中的行一样多次,最后一个只有一个 . 所以,解决方案可以在remoteCommand
的名称附加索引,但这很糟糕,因为你将有许多不必要的JavaScript函数 . 更好的方法是创建一个函数作为pass参数 . 因此在数据表之外定义remoteCommand
:并在
onkeypress
事件中调用test
函数:这将在您的AJAX请求中传递
rowNumber
参数 . 在支持bean的insertComment()
方法中,您可以读取此参数并使用它执行任何操作:注意:当您更新每行中的面板时,也许您可以将
remoteCommand
的update
属性更改为@parent
,这样这将适用于所有行 .编辑:您可以使用Java方法中的以下代码更新特定行中的特定面板: