JSF-2.0、Mojarra 2.1.19、PrimeFaces 3.4.1
問題の概要:p:inputText
内部p:dataTable
および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>
上のコードは、Enterキーを押すp:remoteCommand
と、管理対象Beanのinsertメソッドを呼び出すfireを示します。
@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)
}
一緒にデバッグしましょう。に3つのステータスが表示されていると仮定して、2番目のステータス(インデックス1)で「リラックス」と入力し、Enterキーを押しますp:dataTable
。p:inputText
デバッグコンソールでは、「relax」が正しく表示されますが、の最後のステータスにindexStatusList
属する値が2であるため、間違ったステータスが検出されます。dataTable行をクリックしたものの インデックスである1である必要があります。p:statusList
p:inputText
p:remoteCommand
問題は、どちらが画面の最後のインデックスを取るかということだと思います。
使い方?
andのp:commandLink
代わりにあると想像してみましょう:p:remoteCommand
p:inputText
<p:commandLink action=#{statusBean.insertComment>
<f:setPropertyActionListener target="#{statusBean.indexStatusList}"
value="#{indexStatusList}"></f:setPropertyActionListener>
このコンポーネントはindexStatusList
、現在クリックされているコンポーネントを正常に渡します。