简体   繁体   English

Primefaces自定义数据表未呈现

[英]Primefaces custom DataTable not rendered

I have a little problem. 我有一点问题。 I want to create a custom DataTable in order to do a reusable component, because I have a lot of pages, and some of them contains tables which has the same structure, but some tables contains some extra items. 我想创建一个自定义DataTable来做一个可重用的组件,因为我有很多页面,其中有些页面包含具有相同结构的表,但是有些表包含一些额外的项。

Let me give you an example. 让我给你举个例子。

This is the common table format 这是常见的表格格式

<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 is extended from org.primefaces.model.LazyDataModel. myBean.dataModel是从org.primefaces.model.LazyDataModel扩展的。 I assure you that this is working very good. 我向你保证,这很好。

Now, this being the most used template, I wanted to extract it into a custom component. 现在,这是最常用的模板,我想将其提取到自定义组件中。 First, I tried to create a custom component which extends javax.faces.component.UiComponentBase but when I put an p:ajax tag inside of it (in order to assign it to my table), it crashes on rendering because it doesn't support these kind of tags (kind of obviously). 首先,我尝试创建一个扩展javax.faces.component.UiComponentBase的自定义组件,但是当我在其中放入p:ajax标记(以便将其分配给我的表)时,它在渲染时崩溃,因为它没有支持这些标签(很明显)。

Ok, I tried another way, I did a custom component which extends directly the org.primefaces.component.datatable.DataTable component. 好的,我尝试了另一种方法,我做了一个自定义组件,该组件直接扩展了org.primefaces.component.datatable.DataTable组件。

I created the java class: 我创建了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
   }
}

...and the xhtml file, which for the moment doesn't contains the template, I just want to print a table ...和目前不包含模板的xhtml文件,我只想打印一张表格

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

<cc:implementation>
</cc:implementation>

...and in my page xhtml file I put this: ...然后在我的页面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> 

Now, when the page is rendered, the methods from MyTable class are called, but the table is not rendered. 现在,呈现页面时,将调用MyTable类中的方法,但不呈现表。 It's not showed anything :| 它什么也没显示:|

Do you have any idea why? 你知道为什么吗? Am I doing something wrong? 难道我做错了什么?

Also, if you have another idea for this task, you are welcome. 另外,如果您对此任务有其他想法,也欢迎您。 Thank you. 谢谢。

Ok, 好,

Meanwhile I discovered the problem. 同时我发现了问题。 This means that I don't know well how Primefaces work. 这意味着我不太了解Primefaces的工作原理。

The problem was there where I shouldn't expected, in the java class. 问题出在Java类中我不应该想到的地方。 Because the class implements NamingContainer, the family has to be a NamingContainer's specific family. 因为该类实现了NamingContainer,所以该家族必须是NamingContainer的特定家族。 So, the class is now the following: 因此,该类现在为:

@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
    }
}

Auch, took me some time to figure out. 好了,花了我一些时间弄清楚。 Now the xhtml composite is rendered, but I expected to render DataTable too (which is extended), but it isn't. 现在,呈现了xhtml复合,但是我也希望呈现DataTable(已扩展),但事实并非如此。 Maybe someone has an idea why isn't rendered. 也许有人知道为什么不渲染。

@BalusC - I saw that you are a kind of Primefaces' zen. @BalusC-我看到您是Primefaces的禅宗。 Do you have any idea why this have to be like that? 您知道为什么必须这样吗?

EDITED: 编辑:
Searching anything else, I found this post https://stackoverflow.com/a/12531268/1589496 where do you give an explanation already. 在搜索其他内容时,我发现了该帖子https://stackoverflow.com/a/12531268/1589496 ,您已经在其中给出了解释。 Thank you. 谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM