New Spec: Column Resizing
I've been spending some time reworking how ember-table handles column resizing. Though the code isn't ready for PR yet, here's our ideas of the new behavior - please discuss if you have problems with how it works!
Problems with current behavior
- With
forceFillColumnson, resizing one column changes all the other columns. This isn't what the user expects minWidthandmaxWidthare buggy. The table can break when forceFill gives a column a width less than 0.- Some behavior isn't possible. For our app, we'd like to make it so that the table behaves like excel in most cases, but expands on initial load to fill the available width as well as possible.
Guiding principles of the new spec
- Consistent, predictable UI
- Consistent API (for example, a column's
canAutoResizeproperty should be the single way to specify automatic column resizing, regardless of the table mode). - Column widths should be persistable (there should be an easy way to save column widths, even if the table automatically resizes).
Spec: Default Mode
- By default, table columns have
canAutoResize = false. The table behaves exactly like excel. - Any number of columns can be set so that
canAutoResize = true. This will cause them to expand/contract (respecting min/max width) to fill the width of the table. Resizing the table will trigger this behavior, but resizing columns will not. A similar behavior is the new Mac OS Finder - it's a table where the leftmost column hascanAutoResize = true. - If the
canAutoResizecolumns cannot expand/contract to fill the space, due to min/max width constraints, either white space or a horizontal scroll bar will appear. - Once the table is "unlocked" - meaning it has white space or a horizontal scroll bar - resizing the table will not cause auto-resize behavior. Auto-resize behavior will resume once the table is resized to get rid of the white space/horizontal scroll, at which point the table will again be considered "locked" and changes to table width will again affect the
canAutoResizecolumns.
Spec: ForceFit Mode
- This mode should be used when the table absolutely must fit within the given space, and should never break into horizontal scroll.
- Manual column resizing is done via "stealing" width, ie adjusting the boundary between two columns. The table behaves like http://quocity.com/colresizable/.
- When the width of the table changes, all
canAutoResizecolumns will proportionally scale to consume the change.
Persisting Widths
- All manual changes to column width will be exposed via a
savedWidthattribute on the columns. This will replacedefaultColumnWidth. Changes caused by auto-resizing will not affectsavedWidth. - The application can choose to persist (or not persist)
savedWidth.
Final Notes
- This is still in development and open to suggestions.
- I'll continue to post more explanations (and hopefully, demos) to make it more clear what's going on here.
- Current development is happening on the branch https://github.com/Addepar/ember-table/tree/azirbel/new-column-resizing
It will probably be really useful to take a look at Apple Cocoa's TableView if you haven't already... seeing as Ember uses similar patterns all over the place to Cocoa (for various obvious reasons if one knows the history). TableView is one of the best implementations of a table I've ever encountered.
While you're at it, perhaps you could end up doing some base work for an implementation of CollectionView, (this enables grid style views, but also has a flow layout which allows for other, not-so-linear layouts). That'd be absolutely amazing to have/use.
Cappuccinos implementation is pretty interesting too as it copies Cocoas but is adapted a little to the web.
@JulianLeviston Ember already has a CollectionView, perhaps it can be leveraged. http://emberjs.com/api/classes/Ember.CollectionView.html
This has been finished and released as v0.4.0!
The old forceFillColumns has been removed, since it's unnecessary. Instead, configure each column's resize behavior using isResizable and canAutoResize (to allow resizing, and automatic resizing, respectively). Automatic resizing refers to scaling the width of columns when the width of the table changes.
"Fluid" mode (once a separate demo) is now available in Ember Table out of the box, configurable via columnMode='standard/fluid'.
It's easier to get a feel for the new configuration via the configurable column demo: http://addepar.github.io/ember-table/#/ember-table/configurable-columns.
This is really great @azirbel.
Thank you for those new features. It would be also really cool if there is a auto size option, I mean, the size of the column is based on it content.
The size of each column is automatically calculated from the contents of the column.
BR,
Marius Edelsbrunner
@azirbel erm... Does the table actually not scroll? But rather the data is displaced within the table? That seems a little... off, to me.
@mariuserik that's a good idea and one we've toyed around with in the past. The main reasons we decided not to make it were (1) it's expensive to do "properly", and (2) it's less simple than modes that rely on manual resizing.
By (1) I mean that the "right" way to do it would be to calculate a width for each column such that all content will fit within that width. But to do that, you need to know how much space every row needs in the column - even the rows that aren't rendered. So this goes against the idea of lazy rendering that makes ember-table fast.
Of course, in a lot of cases you don't need to be so thorough in that computation, since data (dates, numbers) are the same width throughout the column. I think there's a great extension to be made that would look at the visible rows/headers, estimate best widths, and set the table to those widths. If you or anyone else implements it, we should add it to http://addepar.github.io/ember-table/#/ember-table/community-examples!
@JulianLeviston I'm not sure what you mean?
@azirbel It doesn't scroll, it simply displaces the data from position to position, cell to cell, vertically.
For example, say you have a table with the numbers 1 to 100 in a single cell, one cell per row. Say you're looking at the top. When you "scroll" down, it moves the data from 2 to 1, 3 to 2, 4 to 3, etc., but it doesn't move the cells themselves.
Contast this to the way any other scrolling works. For example, on an actual HTML table. When you scroll down, the content smoothly moves, pixel by pixel. The difference is vast. The latter gives you context for your scrolling, lets you see the speed, and gives you PLACE within the scrolling (lets you see where you're located at withought actively looking for that).
Oh, I see. That behavior isn't related to the column resizing changes, so I was confused. I'll go ahead and answer here; if you want to propose a new behavior or get into more detail, open a new issue and we'll move the discussion to there.
Ember Table's principle of "lazy rendering" comes from only rendering data that's visible. In your example of numbers from 1 to 100 (say 10 rows are visible), the DOM element used to display row 1 will be recycled once row 1 is off the screen. Using absolute positioning, it will become the bottom row and repopulated with row 11's data.
Scrolling is then made smoother by rending a couple extra rows on the top and bottom as well - even though they're not visible, they will be as soon as the user scrolls.
This means that scrolling does work normally, and I'm able to scroll ember-tables pixel by pixel with no problem.
Sorry for introducing a totally different topic. I loaded the page again and today it's working fine. Also the weird pulsing headings that were bothering me are now not exhibiting that behaviour either so all good. Again, sorry!
No worries! I'm glad it's resolved. =)