2015-02-10 111 views
0

我有一點問題。我想創建一個自定義的DataTable來創建一個可重用的組件,因爲我有很多頁面,其中一些包含具有相同結構的表格,但有些表格包含一些額外的項目。Primefaces自定義DataTable未呈現

讓我給你舉個例子。

這是公用表格式

<p:dataTable id="myTableId" 
     widgetVar="wMyTableId" 
     scrollable="true" 
     scrollHeight="100%" 
     paginator="true" 
     paginatorPosition="bottom" 
     paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}" 
     currentPageReportTemplate="({startRecord} - {endRecord} of {totalRecords}, page {currentPage}/{totalPages})" 
     rowsPerPageTemplate="20,50,100,200,500" 
     rows="20" 
     lazy="true" 
     value="myBean.dataModel" 
     var="item"> 

     <p:columns value="#{myBean.dataModel.columns}" var="column" columnIndexVar="colIndex"> 
      <f:facet name="header"> 
       <h:outputText value="#{column.header}" /> 
      </f:facet> 
      <myComponents:myCell type="#{item.data[column.property].type}" value="#{item.data[column.property].data}"/> 
     </p:columns> 


     <f:facet name="footer"> 
      <h:panelGroup layout="block"> 
       <div class="buttons"> 
        <p:commandButton process="@none" update=":myTableId" icon="fa fa-refresh" styleClass="ui-priority-secondary" type="button" value="Refresh" onclick="callARemoteCommand(); return false;"/> 
       </div> 
      </h:panelGroup> 
     </f:facet> 
    </p:dataTable> 

myBean.dataModel從org.primefaces.model.LazyDataModel延長。 我向你保證這個工作非常好。

現在,這是最常用的模板,我想將其提取到一個自定義組件中。 首先,我嘗試創建一個擴展javax.faces.component.UiComponentBase的自定義組件,但是當我將一個p:ajax標記放入其中時(爲了將其分配給我的表),它在渲染時崩潰,因爲它不支持這些標籤(很明顯)。

好吧,我嘗試了另一種方式,我做了一個自定義組件,它直接擴展了org.primefaces.component.datatable.DataTable組件。

我創建的java類:

@FacesComponent(tagName = "myTable", value = "myTable") 
public class MyTable extends DataTable implements NamingContainer{ 
    //...nothing fancy here, because it extends directly the DataTable 
    @Override 
    public void encodeBegin(FacesContext context) throws IOException { 
     super.encodeBegin(context);//here debug stops. It's ok 
    } 
} 

...和XHTML文件,該文件暫時不包含模板,我只是想打印表

<cc:interface componentType="myTable"> 
</cc:interface> 

<cc:implementation> 
</cc:implementation> 

...和我的頁面xhtml文件我把這個:

<myComponents:myTable id="myTableId" 
    widgetVar="wMyTableId" 
    scrollable="true" 
    scrollHeight="100%" 
    paginator="true" 
    paginatorPosition="bottom" 
    paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}" 
    currentPageReportTemplate="({startRecord} - {endRecord} of {totalRecords}, page {currentPage}/{totalPages})" 
    rowsPerPageTemplate="20,50,100,200,500" 
    rows="20" 
    lazy="true" 
    value="myBean.dataModel" 
    var="item"> 

    <p:ajax event="page" onstart="doPotatos();" oncomplete="eatPotatos();" /> 
    <p:columns value="#{myBean.dataModel.columns}" var="column" columnIndexVar="colIndex"> 
     <f:facet name="header"> 
      <h:outputText value="#{column.header}" /> 
     </f:facet> 
     <myComponents:myCell type="#{item.data[column.property].type}" value="#{item.data[column.property].data}"/> 
    </p:columns> 
</myComponents:myTable> 

現在,當頁面呈現時,從MyTable類a重新調用,但表格未呈現。它沒有顯示任何東西:|

你知道爲什麼嗎?難道我做錯了什麼?

此外,如果您對此任務有其他想法,歡迎您。 謝謝。

回答

1

好吧,

同時我發現了這個問題。這意味着我不太瞭解Primefaces的工作原理。

問題在那裏,我不應該在java類中。 因爲該類實現了NamingContainer,所以該家族必須是NamingContainer的特定族。所以,現在的課程如下:

@FacesComponent(value = "myTable") 
public class MyTable extends DataTable implements NamingContainer{ 
    //actually this is some fancy thing :| 
    @Override 
    public String getFamily() { 
     return UINamingContainer.COMPONENT_FAMILY;//javax.faces.NamingContainer 
    } 
} 

奧赫,花了我一些時間弄清楚。現在渲染了xhtml組合,但我希望也可以渲染DataTable(已擴展),但事實並非如此。也許有人有一個想法,爲什麼不呈現。

@BalusC - 我看到你是一種Primefaces'zen。你有什麼想法,爲什麼這必須是這樣的?

編輯︰
搜索別的,我發現這個職位https://stackoverflow.com/a/12531268/1589496你在哪裏給一個解釋已經。謝謝。