-
Notifications
You must be signed in to change notification settings - Fork 34
Add usage documentation for Table cell styling and headers #53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
bobhy
wants to merge
8
commits into
fyne-io:main
Choose a base branch
from
bobhy:patch-2-table-docs
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
322adc5
Update collection/table page
bobhy 5502b35
Add collection/table-styling page
bobhy 5473986
Scrolling headers and sample outputs
bobhy e97fa2f
remove jekyll redirect
bobhy 173e285
Remove jekyll redirect;
bobhy 47dc65e
Fix hard tabs in sample
bobhy a35287e
Shorten page and simplify examples (and correct API links)
bobhy b489499
Remove API redirect that is never reached
bobhy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -71,6 +71,8 @@ | |
| docs: | ||
| - list | ||
| - table | ||
| - table-styling | ||
| - table-headers | ||
| - tree | ||
|
|
||
| - title: Data Binding | ||
|
|
||
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,215 @@ | ||
| --- | ||
| title: Table Headers | ||
|
|
||
| --- | ||
|
|
||
| The `Table` collection widget supports a header row and/or column | ||
| which can be formatted to stand out from the data | ||
|
|
||
| Although these samples don't show it, headers can be more complex objects which could trigger table-specific operations like sorting or filtering. | ||
|
|
||
| There is specific support for a "sticky" header, which remains at the top or left of the | ||
| container while the data scrolls. This support uses additional callbacks, `CreateHeader` and `UpdateHeader`. | ||
|
|
||
| ```go | ||
| package main | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| "fyne.io/fyne/v2" | ||
| "fyne.io/fyne/v2/app" | ||
| "fyne.io/fyne/v2/widget" | ||
| ) | ||
|
|
||
| var dataTemplateText = "0123456789012345" | ||
| var sampData = []string{ | ||
| "Lorem ipsum dolo", | ||
| "consectetur adip", | ||
| "sed do eiusmod t", | ||
| "Ut enim ad minim", | ||
| "quis nostrud exe", | ||
| } | ||
|
|
||
| var tableCols = 3 | ||
|
|
||
| func makeTableComponents() *widget.Table { | ||
|
|
||
| table := widget.NewTable( | ||
| // length: return #rows, #cols | ||
| func() (int, int) { | ||
| return 15, tableCols | ||
| }, | ||
|
|
||
| // create cell template | ||
| func() fyne.CanvasObject { | ||
| tpl := widget.NewLabel(dataTemplateText) | ||
| return tpl | ||
| }, | ||
|
|
||
| // update cell | ||
| func(id widget.TableCellID, cellTpl fyne.CanvasObject) { | ||
| cell := cellTpl.(*widget.Label) | ||
|
|
||
| cell.Text = sampData[(tableCols*id.Row+id.Col)%len(sampData)] | ||
|
|
||
| cell.Refresh() // refresh needed after styling changes | ||
| }, | ||
| ) | ||
|
|
||
| // Enable frozen row and/or column headers | ||
| table.ShowHeaderRow = true | ||
| table.ShowHeaderColumn = true | ||
|
|
||
| table.CreateHeader = func() fyne.CanvasObject { | ||
| return widget.NewLabelWithStyle("row xx header", | ||
| fyne.TextAlignCenter, | ||
| fyne.TextStyle{Bold: true, Underline: true}) | ||
| } | ||
|
|
||
| table.UpdateHeader = func(id widget.TableCellID, template fyne.CanvasObject) { | ||
| cell := template.(*widget.Label) | ||
|
|
||
| if id.Row < 0 && id.Col < 0 { | ||
| // even if table has both Header rows and columns, | ||
| // the top left header cell {-1, -1} is never populated | ||
| cell.SetText("This space intentionally left blank") | ||
| } else if id.Row < 0 { // {row: -1, col: x} Set column header for col x | ||
| cell.SetText(fmt.Sprintf("-- Col %d Header --", id.Col)) | ||
| } else if id.Col < 0 { // {row: x, col: -1} Set row header for row x | ||
| cell.SetText(fmt.Sprintf("Row %d Header", id.Row)) | ||
| } | ||
| } | ||
|
|
||
| return table | ||
| } | ||
| func main() { | ||
| a := app.NewWithID("com.example.sample.table_headers") | ||
| w := a.NewWindow("table_headers") | ||
|
|
||
| table := makeTableComponents() | ||
|
|
||
| w.SetContent(table) | ||
| w.Resize(fyne.NewSize(430, 300)) | ||
| w.ShowAndRun() | ||
| } | ||
| ``` | ||
|
|
||
| This renders like so: | ||
|
|
||
|  | ||
|
|
||
| ## scrolling headers | ||
andydotxyz marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| It is possible to render a table with multiple rows or columns of headers, | ||
| or whose headers scroll with the data, | ||
| or whose upper left corner contains a legend or other content. | ||
|
|
||
| To do this, the Table widget is "tricked" into invoking the standard `CreateCell` and `UpdateCell` callbacks with additional index values | ||
| which are rendered as header content rather than data. | ||
|
|
||
| ```go | ||
| package main | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| "fyne.io/fyne/v2" | ||
| "fyne.io/fyne/v2/app" | ||
| "fyne.io/fyne/v2/widget" | ||
| ) | ||
|
|
||
| var sampData = []string{ | ||
| "Lorem ipsum dolo", | ||
| "consectetur adip", | ||
| "sed do eiusmod t", | ||
| "Ut enim ad minim", | ||
| "quis nostrud exe", | ||
| } | ||
|
|
||
| var dataCols = 3 | ||
|
|
||
| // to disable row or column header, set to 0 | ||
| var numHeaderRows = 1 | ||
| var numHeaderCols = 1 | ||
|
|
||
| var headerTemplate = widget.NewLabelWithStyle("row xx header", | ||
| fyne.TextAlignCenter, | ||
| fyne.TextStyle{Bold: true, Underline: true}) | ||
|
|
||
| // example of rendering headers within standard cell callbacks | ||
| func makeTableComponents() *widget.Table { | ||
|
|
||
| table := widget.NewTable( | ||
|
|
||
| // length: return #rows, #cols | ||
| func() (int, int) { | ||
| return 15 + numHeaderRows, // extra rows and columns for headers | ||
| dataCols + numHeaderCols | ||
| }, | ||
|
|
||
| // create cell template | ||
| func() fyne.CanvasObject { | ||
| return widget.NewLabel("0123456789012345") | ||
| }, | ||
|
|
||
| // update cell | ||
| func(id widget.TableCellID, cellTpl fyne.CanvasObject) { | ||
| cell := cellTpl.(*widget.Label) | ||
| // dataIndex is the row and column in the *data array* of the referenced cell. | ||
| dataIndex := widget.TableCellID{Row: id.Row - numHeaderRows, Col: id.Col - numHeaderCols} | ||
| if id.Row >= numHeaderRows && id.Col >= numHeaderCols { | ||
|
|
||
| // index in the data range, render data cell | ||
| cell.Alignment = fyne.TextAlignLeading | ||
| cell.TextStyle = fyne.TextStyle{} | ||
| cell.Text = sampData[(dataCols*(dataIndex.Row)+(dataIndex.Col))%len(sampData)] | ||
| } else { | ||
| // index is in header range | ||
| cell.Alignment = headerTemplate.Alignment | ||
| cell.TextStyle = headerTemplate.TextStyle | ||
|
|
||
| if id.Row < numHeaderRows && id.Col < numHeaderCols { | ||
| cell.Wrapping = fyne.TextWrapWord | ||
| cell.Text = "Table discription" | ||
| // content for upper left corner of table | ||
| } else if id.Row < numHeaderRows { | ||
| // content for the header row(s) | ||
| cell.Text = fmt.Sprintf("-- Col %d Header --", dataIndex.Col) | ||
| } else { // assert id.Col < numHeaderCols | ||
| // content for header col(s) | ||
| cell.Text = fmt.Sprintf("Row %d Header", dataIndex.Row) | ||
| } | ||
| } | ||
|
|
||
| cell.Refresh() // refresh needed after styling changes | ||
| }, | ||
| ) | ||
|
|
||
| // Enable frozen row and/or column headers, set to 1 | ||
| table.StickyRowCount = 0 | ||
| table.StickyColumnCount = 0 | ||
|
|
||
| return table | ||
| } | ||
| func main() { | ||
| a := app.NewWithID("com.example.sample.table_headers") | ||
| w := a.NewWindow("table_headers") | ||
|
|
||
| table := makeTableComponents() | ||
|
|
||
| w.SetContent(table) | ||
| w.Resize(fyne.NewSize(430, 300)) | ||
| w.ShowAndRun() | ||
| } | ||
|
|
||
| ``` | ||
|
|
||
| Which renders as: | ||
|
|
||
|  | ||
|
|
||
|
|
||
|
|
||
| Refer to [`widget.Table`](/api/v2.6/widget/table.html) API for additional details on how to implement headers. | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not a great idea to use 1-dimensional data for a demonstration of a 2-dimensional output.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The sample renders different data in each column and the pattern doesn't repeat till 5 rows later. It doesn't look like 1-dimensional data.
But the point of the sample is the headers, and maybe it would be simpler and less distracting to put the same placeholder text in all the data cells, or even leave them empty?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree the data here is not the point - but docs have to consider every single line will be used as best practice - and 1D data is not best practice for a table and could lead to support requests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To clarify, are you concerned about line 25,
var dataTemplateText = "0123456789012345"? I thought the concern was about the sample data, at lines 26-31 which is indeed a 1-d vector of strings.If it's about line 25, please clarify what you'd rather have there (my idea was you can tell at a glance that this string is 16 runes long). If it's about the subsequent lines, I could have the sample show blanks in the data cells. That would focus attention on the headers, which is the point of this sample.