Java JSF:h:dataTable 与 h:panelGrid

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3367710/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 22:38:36  来源:igfitidea点击:

JSF: h:dataTable vs h:panelGrid

javajsf

提问by Jan

In JSF, h:dataTableand h:panelGridboth create html table-tags.

在JSF中,h:dataTable并且h:panelGrid都创建HTMLtable标签都有效。

What is the difference between both?

两者有什么区别?

When to use one or the other?

何时使用其中之一?

采纳答案by Jose Diaz

As it names indicates h:dataTableis used mostly for displaying a table containing data from some of the models in your application. Here is an usage example.

顾名思义,它h:dataTable主要用于显示包含来自应用程序中某些模型的数据的表格。这是一个使用示例。

<h:dataTable value="#{dataTableBean.items}" var="item">
    <h:column>
        <f:facet name="header" >
            <h:outputText value="Item Category"/>
        </f:facet>    
        <h:outputText value="#{item.category}"/>
    </h:column>
</h:dataTable>

The h:panelGridis used mostly for layout and placing of elements purpose. Here is an usage example.

h:panelGrid主要用于布局和元件放置目的。这是一个使用示例。

<h:panelGrid id="panel" columns="2" border="1">
    <h:outputText value="First Name:" />
    <h:inputText id="first" value="#{backingBean.user.firstName}" />
    <h:outputText value="Last Name:" />
    <h:inputText id="last" value="#{backingBean.user.lastName}" />
</h:panelGrid>

回答by Mateusz Dymczyk

You should use a panelGrid when you know the number of rows and columns that you want to display, you have to define all the child components yourself (manually, the panelGrid will not add any rows/collumns by itself) and the panelGrid will arrange them. DataGrid on the other hand is used when you have a data structure (like a collection) with an indetermined size, then you just have to specify the columns you want to print and the dataGrid will iterate over that collection creating a row for each entry.

当您知道要显示的行数和列数时,您应该使用 panelGrid,您必须自己定义所有子组件(手动,panelGrid 不会自行添加任何行/列)并且 panelGrid 将安排它们. 另一方面,当您拥有不确定大小的数据结构(如集合)时,将使用 DataGrid,然后您只需指定要打印的列,dataGrid 将遍历该集合,为每个条目创建一行。

回答by McDowell

The dataTableis a model-drivendataoutput component. For every row in your model, the component can set the state of its children for every phase of the JSF lifecycle. This behaviour is beneficial or detrimental depending on what you're trying to achieve.

所述的dataTable是一个模型驱动的数据输出部件。对于模型中的每一行,组件可以为JSF 生命周期的每个阶段设置其子组件的状态。这种行为是有益的还是有害的,取决于您要实现的目标。

By contrast, the panelGridis just a layout control.

相比之下,panelGrid只是一个布局控件

The difference is (very) roughly analogous to the difference between JTableand JPanel.

差异(非常)大致类似于JTableJPanel之间的差异。