-
-
Notifications
You must be signed in to change notification settings - Fork 798
Added table widget to web backend #3425
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
caydnn
wants to merge
8
commits into
beeware:main
Choose a base branch
from
caydnn:3334-web-table-widget
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 all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
1e345ac
add changelog file
caydnn 979333b
add and update table documentation files
caydnn 1e515a6
update factory file to add table
caydnn aaac0a7
add table widget to web backend
caydnn 0ca9ef0
Merge branch 'beeware:main' into 3334-web-table-widget
caydnn eceef8f
Merge branch 'beeware:main' into 3334-web-table-widget
caydnn 15c47a7
Code cleanup
caydnn 2f784a5
Merge branch 'main' into 3334-web-table-widget
caydnn 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 |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| The Web backend now supports Table widgets. |
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
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
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,156 @@ | ||
| import warnings | ||
|
|
||
| import toga | ||
| from toga_web.libs import create_proxy | ||
|
|
||
| from .base import Widget | ||
|
|
||
|
|
||
| class TogaRow: | ||
| def __init__(self, value): | ||
| self.value = value | ||
|
|
||
| # All paths return none as Icon is not implemented in web. | ||
| def icon(self, attr): | ||
| data = getattr(self.value, attr, None) | ||
| if isinstance(data, tuple): | ||
| if data[0] is not None: | ||
| return None | ||
| return None | ||
| else: | ||
| try: | ||
| return None | ||
| except AttributeError: | ||
| return None | ||
|
|
||
| def text(self, attr, missing_value): | ||
| data = getattr(self.value, attr, None) | ||
|
|
||
| if isinstance(data, toga.Widget): | ||
| warnings.warn("Web does not support the use of widgets in cells") | ||
| text = None | ||
| elif isinstance(data, tuple): | ||
| text = data[1] | ||
| else: | ||
| text = data | ||
|
|
||
| if text is None: | ||
| return missing_value | ||
|
|
||
| return str(text) | ||
|
|
||
|
|
||
| class Table(Widget): | ||
| def create(self): | ||
|
|
||
| self.native = self._create_native_widget("table") | ||
|
|
||
| self.table_header = self._create_native_widget("thead") | ||
| self.native.appendChild(self.table_header) | ||
|
|
||
| self.table_body = self._create_native_widget("tbody") | ||
| self.native.appendChild(self.table_body) | ||
|
|
||
| def change_source(self, source): | ||
| self.selection = {} | ||
|
|
||
| # remove old table data | ||
| for row_child in list(self.table_body.children): | ||
| for td_child in list(row_child.children): | ||
| row_child.removeChild(td_child) | ||
| self.table_body.removeChild(row_child) | ||
|
|
||
| for row_child in list(self.table_header.children): | ||
| for td_child in list(row_child.children): | ||
| row_child.removeChild(td_child) | ||
| self.table_header.removeChild(row_child) | ||
|
|
||
| if source is not None: | ||
| self._create_table_headers() | ||
|
|
||
| for i, row in enumerate(source): | ||
| self._create_table_row(row, i) | ||
|
|
||
| self.refresh() | ||
|
|
||
| def get_selection(self): | ||
| selection = sorted(self.selection) | ||
| if self.interface.multiple_select: | ||
| return selection | ||
| elif len(selection) == 0: | ||
| return None | ||
| else: | ||
| return selection[0] | ||
|
|
||
| def add_selection(self, index, table_row): | ||
| self.selection[index] = table_row | ||
| table_row.classList.add("selected") | ||
|
|
||
| def remove_selection(self, index): | ||
| table_row = self.selection.pop(index) | ||
| table_row.classList.remove("selected") | ||
|
|
||
| def clear_selection(self): | ||
| for index in list(self.selection): | ||
| self.remove_selection(index) | ||
|
|
||
| def _create_table_headers(self): | ||
| if self.interface.headings: | ||
| headings = self.interface.headings | ||
| else: | ||
| headings = self.interface.accessors | ||
| self.table_header_row = self._create_native_widget("tr") | ||
|
|
||
| for heading in headings: | ||
| th = self._create_native_widget("th", content=heading) | ||
| self.table_header_row.appendChild(th) | ||
|
|
||
| self.table_header.appendChild(self.table_header_row) | ||
|
|
||
| def _create_table_row(self, item, index): | ||
| row = TogaRow(item) | ||
| values = [] | ||
| for accessor in self.interface.accessors: | ||
| values.extend( | ||
| [ | ||
| # Removed icon accessor for now as not sure how to handle icon | ||
| # row.icon(accessor), | ||
| row.text(accessor, self.interface.missing_value), | ||
| ] | ||
| ) | ||
| tr = self._create_native_widget("tr") | ||
|
|
||
| tr.addEventListener( | ||
| "click", create_proxy(lambda event: self.dom_row_click(event, index, tr)) | ||
| ) | ||
|
|
||
| for value in values: | ||
| td = self._create_native_widget("td", content=value) | ||
| tr.appendChild(td) | ||
| self.table_body.appendChild(tr) | ||
|
|
||
| def dom_row_click(self, event, index, table_row): | ||
| if index in self.selection: | ||
| self.remove_selection(index) | ||
| else: | ||
| if not self.interface.multiple_select: | ||
| self.clear_selection() | ||
| self.add_selection(index, table_row) | ||
|
|
||
| def insert(self, index, item): | ||
| self.change_source(self.interface.data) | ||
|
|
||
| def clear(self): | ||
| self.change_source(self.interface.data) | ||
|
|
||
| def change(self, item): | ||
| self.change_source(self.interface.data) | ||
|
|
||
| def remove(self, index, item): | ||
| self.change_source(self.interface.data) | ||
|
|
||
| def insert_column(self, index, heading, accessor): | ||
| self.change_source(self.interface.data) | ||
|
|
||
| def remove_column(self, accessor): | ||
| self.change_source(self.interface.data) | ||
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.
This clearly works; but is it not possible to do a more selective index-based child insert?
self.table_body.insertBefore(...new row..., self.table_body.childNodes[index]);Similarly for remove and change.