问题
What is the difference between the Grid and Table components in Vaadin 7?
Which should I use, and when?
回答1:
Grid is a new more powerful component which is supposed to be the successor of Table (see The Table is dead, long live the Grid). So there should not be any need to favor Table over Grid.
Here is a first in the series of articles by Vaadin aboout migrating from Table to Grid: https://vaadin.com/blog/-/blogs/mission-rip-table-migrate-to-grid-basic
回答2:
Summary
Grid → New & Amazing
Table → Venerable & Reliable
Table is a very good data-grid display widget built into the earliest versions of Vaadin.
Grid is grand rewrite from scratch, designed to supplant Table. The Vaadin team is leveraging their wisdom gained from experience, “if we knew then what we know now”, to make the very best data-grid possible given today’s Web technology. Grid is such a big deal that it gets its own vanity page. See this company blog post for a quick overview.
So, generally speaking, I suggest you focus on Grid. Try it out, learn it first, and see if it meets your needs. If you run into bugs or problems, or you have need features lacking in Grid, then fallback to Table. You can mix-and-match both in a project, with the caveat that the different appearance and behavior may confuse your users.
Think of Grid as the precocious adolescent full of promise and eager to make the leap into adulthood, and Table as the mature grownup working hard in its prime years of middle-age while dreaming of a well-earned future retirement sailing into the sunset.
Details
If using Vaadin 6, on a continuing project or you need to support very old browsers, then Table
is your only choice. Grid
requires Vaadin 7 or later.
Here are some major Table features currently lacking in Grid.
- Drag-and-drop features (to be added later).
- Resize column by user dragging edge of column header.
Both share many features. They practice lazy-loading to the browser, automatically loading data only as needed from the server-side so as to not overload the web browser. Both allow the user to drag columns to re-order. Both let the user show/hide columns.
Row Selection
Both allow selecting single rows or multiple rows.
Grid also has an automatic feature where it adds a column of checkboxes. The user can select multiple rows by clicking those checkboxes rather than using a mouse or mouse+keyboard. Many, if not most, users are clumsy with mouse-driven multiple row selection. See this screenshot, and notice the very first column.
The programming support for selection is different. Grid does not extend AbstractSelect, instead defines its own selection API. Call addSelectionListener() and define a SelectionListener. See The Book Of Vaadin.
Headers & Footers
Both have headers and footers, but Grid has more options. Grid can place widgets instead of text. Grid can have multiple rows of headers. Grid can join header cells, like spanning in an HTML table.
In-Place Editing
Both provide in-place editing of data, but in different ways. Table allows editing of data in the cell. Grid took a different approach, for editing the entire row by displaying a mini-window, a little data-entry form. This form includes a pair of confirmation & cancellation buttons. This form is much more flexible than Table’s cell-editing.
Filtering
Grid offers user-controlled filtering, where a row of enterable cells appears below the headers. As users type a filter is applied to show only matching rows. See this screenshot. With Table, you need to create some kind of user-interface and apply the filtering.
Backed By Data Container
UPDATE: Vaadin 8 brings a new version of Grid that leverages a newly improved and greatly simplified data model. This is a major reason to use Grid instead of Table. Note that both the original Grid as well as Table are still available in Vaadin 8 via the Vaadin 7 compatibility layer.
The following old info left intact…
Both Table and Grid are a presentation-only widget, backed by a separate data object implementing the Container interface according to the Vaadin Data Model.
The Table class also acts as a Container which always confused me. I’m glad to see Grid maintain a more clear distinct separation.
Like Table, Grid does offer some convenience methods for quick-and-dirty situations where you want to throw some data at the Grid itself without formally producing a Container. But Grid’s convenience methods use row and column terms in contrast to the Container’s item and property terms. These terms make it more clear that your are talking to the Grid but the Grid is acting on its default attached IndexedContainer instance on your behalf.
Cell Content
UPDATE: In Vaadin 8.1, Grid gains the ability to display a Component in a cell. See a live demo of the Component Renderer.
Cell content handling is different. Grid cannot directly display column icons, nor can it place components (widgets) in a cell. Instead used the new Renderer features.
Doc & Demo
Both have a chapter in The Book Of Vaadin, one for Table and one for Grid.
Both have a live demos. One for Table (and TreeTable). And a couple for Grid, one full-window and one with various aspects.
See this brochure page for Grid, including an embedded live demo, with a link to further demos.
Miscellaneous Differences
Grid has a built-in widget for displaying a number as a small thermometer widget. See this screenshot, in the last column.
For more specific differences, see section 5.24.1 Overview – Differences To Table in The Book Of Vaadin.
Esoterica… Grid is the first component in Vaadin Components, a high quality set of Web Components built on Google Polymer that is ready to be used with any framework that supports Web Components. While the Vaadin team has promised to support Table for years in the future, don’t expect it to receive such special attention.
Vaadin 8
In Vaadin 8.0 and 8.1, Grid is getting even better. Major enhancements include:
- Works with the simpler sleeker data model new in Vaadin 8
- Pass a collection of entities for display
- Easily define columns with type-safe lambda syntax
grid.addColumn( Person::getFirstName ).setCaption( "First Name" );
- Easier lazy-loading of data now that
Container
is gone:grid.setDataProvider( ( sortorder , offset , limit) -> service.findAll( offset , limit) , () -> service.count() );
- The ability to display Vaadin components rather than just renderers
- Drag-and-drop via the drag-and-drop support defined by HTML5.
- Even more speed
The Table component is still available via the Compatibility layer in Vaadin 8 for continuing the use of Vaadin 7 classes.
Future
The Vaadin team has great plans for Grid, so much of what you read on StackOverflow page will change. The team will be eagerly adding features, enhancements, and bug fixes in the coming months and years. Many enhancements have already been made to Grid in its short history, so beware when reading older documents about limitations or lacking features – that may not be so anymore.
回答3:
Actually it's possible to implement everything you want with both of them. But my experience is, that the Grid is more comfortable to use.
The Table is easy to understand and easy to use for simple tables (as you might guess). So if you just want to show a few lines of data visualized nicely - use the Table. It is stable and works well on that.
The Grid looks like a Table but it has some features towards the Table. If you have a whole lot of data to render the Grid might handle it better. Also there is a good practice for "inline editing" your data. There is a way to customise the headers of a grid extensively. If you want to do a lot of customisation and interaction inside a table-like component - use the Grid.
See the features here:
https://vaadin.com/grid
http://demo.vaadin.com/sampler/#ui/grids-and-trees/grid
There is a difference how you can select rows/cells in these two components. For example the EventListeners for selection are used slightly different concerning the value they return. Also there's a difference in how you add columns and rows to them, but thats just an implementation thing, so it shouldn't really matter.
回答4:
In filter table ,if we clicking on the table header,first row will be defaultly highlighted by a method called setSelectable(true); but in grid table there is no such type of action,
I want to apply the same action in grid table. Is there any possibilty for having that nature? Is there any method or code available?
Below is the code I used in my grid table:
private void buildPagedGrid(Class<T> clazz) {
setWidth("100%");
setSelectionMode(SelectionMode.SINGLE);
setImmediate(true);
setSizeFull();
setContainerDataSource(dataSource);
setFooterVisible(true);
}
来源:https://stackoverflow.com/questions/30134479/vaadin-grid-vs-table