2015-11-24 26 views
1

我目前正在使用JSF 2和Primefaces 5.2,更確切地說使用p:dataTable。 我使用p:dataTable與分頁。從Primefaces數據表中檢索分頁對象

從我的Java bean中,我找不到檢索任何分頁組件的方法。我在一個物體特別感興趣,在那裏我能得到 選擇的元素在{RowsPerPageDropdown}

<p:dataTable 
     var="item" 
     value="#{items}" 
     rows="10" 
     id="tableItems"  
     paginator="true" paginatorPosition="top" paginatorAlwaysVisible="true" 
     paginatorTemplate="{CurrentPageReport} {PreviousPageLink} Page : {JumpToPageDropdown} {NextPageLink} NbrElements : {RowsPerPageDropdown}" 
     rowsPerPageTemplate="#{fn:length(items)},10,20,50" 
     currentPageReportTemplate="{startRecord}-{endRecord}/{totalRecords}" binding="#{tableComponent.table}"> 

通過檢查的頁面,在下拉菜單中的id是tableItems_rppDD

我試圖通過打印組件ID與 組合FacesContext.getCurrentInstance().getViewRoot()...getChildren()findComponent(),但沒有成功找到它。

有誰知道如何讓我的手在分頁上,或者如果可能的話?或者它是一個純Javascript對象?

非常感謝

編輯

我添加動態元素到我的清單(p:dataTable + h:commandLink)。

如果您看數據表聲明,{RowsPerPageDropdown}的第一個元素是新列表中的元素數(我們稱之爲n)。 第一次,如果我從{RowsPerPageDropdown}中選擇第一個元素,它將顯示n元素。

如果我在列表中添加一個新的元素,在{RowsPerPageDropdown}的第一個元素成爲n + 1 但在表中顯示的元素的總數是n(最新的元素是下頁)。

我已經有輕微的變化{CurrentPageReport}

+0

爲什麼你需要訪問它?你想解決什麼問題?也許lazyLoading對你更好。您在加載方法中收到「頁面」大小(在下拉列表中選擇),然後 – Kukeltje

+0

如果只是獲取選定值,則可以始終查看請求參數並從那裏檢索它。它的ID /名稱與由':row'附加的數據表的完整ID相同。# – Kukeltje

+0

@Kukeltje:查看更新。我會研究lazydatamodel。謝謝你們兩位 – ccheneson

回答

1

太長評論重新實現固定我的問題......

DataTable是一個UIComponent,而且至今沒有一個分頁程序「JSF組件」(UIComponent)。
因此,您需要做的每個關於分頁的操作都必須直接在DataTableDataModel(當以value屬性傳遞時)完成。

但是,這取決於你需要做什麼,你可以訪問它的屬性:

DataTable table = [find component somehow: UIComponent.getCurrentComponent(context), context.getViewRoot().findComponent(expr), ...] 

// if you need to know currently selected RowsPerPageDropdown (page size) 
int rows = table.getRows(); // table.getRowsToRender(); 

// or you can get the template 
String rowsPerPageTemplate = table.getRowsPerPageTemplate(); 

// and split options 
String[] options = rowsPerPageTemplate.split("[,\\s]+"); 

你可以找到DataTable中是如何呈現在org.primefaces.component.paginator.RowsPerPageDropdownRenderer源代碼的HTML <select>下拉。


@Kukeltje在這裏你是一個小例子:

的例子顯示瞭如何獲得服務器端所選行下拉值:選擇一個行下拉值,然後單擊命令按鈕。

<p:growl id="messages" autoUpdate="true" showDetail="true" 
    showSummary="true" /> 

<h:form> 
    <p:dataTable id="table" var="elem" value="#{testTableBean.valueList}" rows="5" 
     paginator="true" 
     paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}" 
     rowsPerPageTemplate="#{component.rowCount},5,10,15"> 
     <p:column> 
      <h:outputText value="#{elem}" /> 
     </p:column> 
    </p:dataTable> 

    <p:commandButton action="#{testTableBean.execute}" process="@form" value="execute" /> 

    <p:commandButton process="@form" update="table" 
      actionListener="#{testTableBean.valueList.add('value #' += testTableBean.valueList.size())}" 
      value="add" /> 
</h:form> 

@ManagedBean 
@ViewScoped 
public class TestTableBean implements Serializable 
{ 
    private static final long serialVersionUID = 1L; 

    private final List<String> valueList = new ArrayList<>(); 

    @PostConstruct 
    public void init() 
    { 
     // add 25 dummy values 
     for(int i = 0; i < 25; i++) 
     { 
      valueList.add("value #" + i); 
     } 
    } 

    public void execute() 
    { 
     FacesContext context = FacesContext.getCurrentInstance(); 

     // get current component: CommandButton 
     UIComponent component = UIComponent.getCurrentComponent(context); 

     // get the naming container: HtmlForm 
     UIComponent namingContainer = component.getNamingContainer(); 

     // find data table by id 
     DataTable table = (DataTable) namingContainer.findComponent("table"); 

     // get the displayed rows value: this value is updated by DataTableRenderer.decode() 
     // when user changes rows dropdown (previous ajax request) 
     int rows = table.getRows(); 

     // add as message 
     context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "rows: " + rows, null)); 
    } 

    public List<String> getValueList() 
    { 
     return valueList; 
    } 
} 

@ccheneson:你不需要依靠HTML端,因爲它是由渲染器生成的;每個信息渲染者都知道它只是在服務器端可用和可訪問的。

+0

在這種情況下,你如何獲得'selected'元素?用戶選擇的那個?渲染時間遲到了......我至少從問題 – Kukeltje

+0

中得知了我實際上已經發現了有關RowsPerPageDropdownRenderer的問題,所以我正在爲它進行調查以尋求我需要做的事情。所選元素的問題是,如果查看生成的'select'element,則沒有'name'屬性(只有一個'id'),所以該值甚至不會發送到服務器。如果是,它將在請求參數中可用。一種方法是重新實現'RowsPerPageDropdownRenderer'來爲選擇添加一個'name'。 – ccheneson

+0

@Kukeltje查看更新 –