3

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:dataTablep:inputText

デバッグコンソールでは、「relax」が正しく表示されますが、の最後のステータスindexStatusList属する値が2であるため、間違ったステータスが検出されます。dataTable行をクリックしたものの インデックスである1である必要があります。p:statusListp:inputText

p:remoteCommand問題は、どちらが画面の最後のインデックスを取るかということだと思います。


使い方?

andのp:commandLink代わりにあると想像してみましょう:p:remoteCommandp:inputText

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

このコンポーネントはindexStatusList、現在クリックされているコンポーネントを正常に渡します。

4

1 に答える 1

1

このソリューションの概念的な問題は、どのように機能するかにありp:remoteCommandます。nameの属性で名前が定義されているJavaScript関数を作成しますp:remoteCommand。これを入れると、このテーブルの行の数だけdataTable呼び出されるJavaScript関数が繰り返されて作成testされ、最後に1つだけになります。したがって、解決策は、名前にインデックスを追加するremoteCommandことですが、不要なJavaScript関数が多数あるため、これは悪いことです。より良いアプローチは、1つの関数にパス引数を作成することです。したがってremoteCommand、datatableの外部で定義します。

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

イベントで次testのような関数を呼び出します。onkeypress

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

rowNumberこれにより、AJAXリクエストでパラメータが渡されます。バッキング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")
于 2013-02-20T22:28:02.460 に答える