簡體   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