-
Notifications
You must be signed in to change notification settings - Fork 818
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
DataTable refactor - Adds cell updating and sorting functionality #1638
Conversation
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.
Just some comments to consider.
Pretty sure this will be good to merge early next week.
I think there is a bug where if a cell's initial value is an empty string then the cell can no longer be updated after row creation: import asyncio
from textual.app import App
from textual.widgets import DataTable, Footer, Header, Switch
BACKGROUND_TASKS = []
class Listing(DataTable):
def on_mount(self):
super().on_mount()
self.add_column('', key='status')
self.add_column('', key='title')
self.show_header = False
self.cursor_type = 'row'
self.call_after_refresh(lambda: BACKGROUND_TASKS.append(asyncio.create_task(self.__on_mount())))
async def __on_mount(self):
# self.add_row('something', 'title1', key='foo')
self.add_row('', 'title1', key='foo')
class MyApp(App):
def compose(self):
yield Header()
yield Switch()
yield Listing()
yield Footer()
async def on_switch_changed(self, event: Switch.Changed):
listing = self.query_one(Listing)
listing.update_cell('foo', 'status', '✓' if event.value else '')
if __name__ == '__main__':
app = MyApp()
app.run() |
@ofek Since all the cells in that column are the empty string (including the label), the width will be calculated as 0. The width of a column will only be recomputed if you pass |
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.
Nice work. Just a few minor requests.
Rows and columns in the DataTable have been decoupled from their current visual location.
API Changes
sort
method was added to theDataTable
, allowing you to supply one or more column keys. Rows will be re-ordered using the provided keys.update_cell(row_key, column_key, value, update_width)
was added to theDataTable
, which can be used to update the value of a cell identified by(row_key, column_key)
.update_coordinate(coordinate, value, update_width)
was added which can be used to update the cell which is currently present atcoordinate
.add_row
andadd_column
have been updated to accept akey
argument. If thiskey
is not supplied, the DataTable will generate a key for you. This key will be returned from calls toadd_row
andadd_column
and can be used to reference the row and column regardless of where it’s currently located on the screen. This means that if you sort the rows in your table, you can always use the key of that row to reference it, even if its coordinate/index has changed.add_rows
andadd_columns
have been updated to returnlist[RowKey]
andlist[ColumnKey]
respectively.get_cell_value
has been renamed toget_value_at(coordinate)
- this seems more appropriate given our new distinction between spatial coordinates and cells identified by row/column keys.get_cell_value
exists which returns the value of a cell given aRowKey
andColumnKey
.cursor_cell
was renamed tocursor_coordinate
since cells and coordinates are no longer the same things. It was renamed to make that more explicit.validate_cursor_cell
was renamed tovalidate_cursor_coordinate
.watch_cursor_cell
was renamed towatch_cursor_coordinate
.hover_cell
renamed tohover_coordinate
for similar reasons.validate_hover_cell
was renamed tovalidate_hover_coordinate
.row_count
is now a property. Previously it was an attribute that was manually kept in sync.data
,columns
androws
are re-structured to all be dicts keyed by row and column keys.ordered_rows
andordered_columns
have been added, which returnlist[Row]
andlist[Column]
respectively. The ordering of these lists reflects the positioning of the rows and columns you see on screen.refresh_cell
was changed torefresh_coordinate(coordinate)
. (Although I'm not sure what the use case for this being public is).DataTable
*Selected
methods report wrong selection location when caused by mouse selection #1723Please review the following checklist.