2017-07-30 29 views
0

我使用primefaces懶數據表在JSF頁面如下圖所示:回到Bean的屬性不更新primefaces數據表排序操作

<h:form id="searchForm"> 
    <h:panelGroup id="searchFields"> 
     <h:outputText value="searchField"/> 
     <p:inputMask 
       id="txtSearchField" 
       dir="LTR" 
       mask="9?999999999999" 
       maxlength="12" 
       value="#{myBackBean.searchField}" /> 

     <h:commandButton 
       id="searchButton" 
       value="search" 
       action="{myBackBean.searchAction}" /> 
    </h:panelGroup> 
    <p:dataTable 
      id="resultTable" 
      var="res" 
      value="#{res.listOfRecords}" 
      paginator="true" paginatorPosition="bottom" rows="10" 
      rowsPerPageTemplate="5,10,15" 
      lazy="true"> 

     <p:column headerText="recordId" sortBy="#{res.recordId}"> 
      <h:outputText value="#{res.recordId}" /> 
     </p:column> 

    </p:dataTable> 
</h:form> 

正如你可以看到,有一個數據表上方的搜索面板,包含用戶可以通過填充縮小結果的參數。對於後面的bean中的每個搜索字段,存在一個相應的屬性,如下所示。

@Component 
@Scope("request") 
public class myBackBean { 

    private String searchField; 
    private LazyDataModel<RecordsDto> listOfRecords; 

    @Autowired 
    MyService myService; 

    public String getSearchField() { 
     return searchField; 
    } 

    public void setSearchField(String searchField) { 
     this.searchField = searchField; 
    } 

    public LazyDataModel<OperationsDto> getListOfRecords() { 
     return listOfRecords; 
    } 

    public void setListOfRecords(LazyDataModel<RecordsDto> listOfRecords) { 
     this.listOfRecords = listOfRecords; 
    } 

    public void searchAction() { 
     // doing the search and filling the list 
     listOfRecords = ...; 
     return listOfRecords; 
    } 
} 
當我按下「搜索按鈕」後豆

更新,並且它包含用戶已經輸入了「searchField」值。 但是,當我嘗試使用「recordId」列上的排序箭頭對網格進行排序時,我的請求轉到後臺bean的「getListOfRecords()」方法,但「searchField」值爲null。

如何指導數據表排序操作以更新後臺bean中的搜索字段?

+0

[如何選擇正確的bean作用域?](https://stackoverflow.com/questions/7031885/how-to-choose-the-right-bean-scope) – Kukeltje

回答

0

如果在您的情況下可行,請嘗試使用會話範圍,以便搜索字段值可用。

+0

可能出現重複使用會話不可行範圍,因爲JSF中的惰性數據表在排序操作中調用列表的getter方法(在我的情況下爲「getListOfRecords」方法)。根據JSF規範,getter方法會被多次調用。 所以在這種情況下,我不能把我的搜索邏輯放在我的getter方法中。 – Mostafa

+0

如何在調用搜索函數時將「searchField」的值設置爲會話範圍?所以當sort函數被調用時,「searchField」值可以從會話中獲得,並且不會再爲空。 –

+0

你的意思是把「searchField」的值放入會話對象中?這似乎是一個醜陋的解決方案,不是嗎? – Mostafa