JSF2.0,Mojarra2.1.19,PrimeFaces 3.4.1
问题的摘要__:有一个由p:remoteCommand触发的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>上面的代码说,当按enter键时,触发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中显示了三个状态,在第二个状态(索引为1)中单击p:inputText,输入"relax“并按enter键。
在调试控制台中,它正确地显示了"relax",但是它找到了错误的状态,因为indexStatusList的值为2,它属于-- p:statusList中的最后一个状态。它必须是1,它是单击p:inputText行的dataTable的索引。
我认为问题在于p:remoteCommand,它使用屏幕上的最后一个索引。
它是如何工作的?
让我们想象一下,这里有一个p:commandLink而不是p:remoteCommand和p:inputText
<p:commandLink action=#{statusBean.insertComment>
<f:setPropertyActionListener target="#{statusBean.indexStatusList}"
value="#{indexStatusList}"></f:setPropertyActionListener>此组件成功地传递当前单击的indexStatusList。
发布于 2013-02-20 22:28:02
这个解决方案的概念问题在于p:remoteCommand是如何工作的。它创建JavaScript函数,其名称在p:remoteCommand的name属性中定义。当您在dataTable中插入这个函数时,它将迭代和创建名为test的JavaScript函数,就像这个表中有行一样,最后一个只有一个。因此,解决方案可以是在remoteCommand名称下附加索引,但这是错误的,因为您将有许多不必要的JavaScript函数。更好的方法是创建一个函数,作为它的pass参数。因此,在datatable之外定义remoteCommand:
<p:remoteCommand name="test" action="#{statusBean.insertComment}" update="statusRepeatPanel">并在您的test事件中像这样调用onkeypress函数:
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());注意:在更新每行中的面板时,也许可以将remoteCommand的remoteCommand属性更改为@parent,这样所有行都可以这样做。
编辑:您可以使用Java方法中的以下代码更新特定行中的特定面板:
RequestContext.getCurrentinstance().update("form:dataTable:" + rowNumber + ":statusRepeatPanel")https://stackoverflow.com/questions/14990692
复制相似问题