How to select a given row in a DataTable #5089
-
Assume I load 10 items in a data table, I have set up row selection mode, and I want programmatically to set row 5 as selected. How do I do that? I tried: select_msg = DataTable.RowSelected(table, row_index, RowKey(str(row_index)))
table.post_message(select_msg)
table.loading = False but that does not work: [18:44:51] EVENT message_pump.py:763
Mount() >>> LoadingIndicator() method=<LoadingIndicator.on_mount>
[18:44:51] EVENT message_pump.py:763
Mount() >>> LoadingIndicator() method=<Widget.on_mount>
[18:44:51] EVENT message_pump.py:763
Unmount() >>> LoadingIndicator() method=<Widget.on_unmount>
[18:44:51] EVENT message_pump.py:772
RowHighlighted(cursor_row=0, row_key=<textual.widgets._data_table.RowKey object at 0x1103c4470>) >>> DataTable() method=None
[18:44:51] EVENT message_pump.py:772
RowHighlighted(cursor_row=0, row_key=<textual.widgets._data_table.RowKey object at 0x1103c4470>) >>> Screen(id='_default') method=None
[18:44:51] EVENT message_pump.py:763
RowHighlighted(cursor_row=0, row_key=<textual.widgets._data_table.RowKey object at 0x1103c4470>) >>> JaasTop(title='JaasTop', classes={'-dark-mode'}, pseudo_classes={'dark', 'focus'})
method=<JaasTop.on_data_table_row_highlighted>
[18:44:51] EVENT message_pump.py:772
RowSelected(cursor_row=3, row_key=<textual.widgets._data_table.RowKey object at 0x110335130>) >>> DataTable() method=None
[18:44:51] EVENT message_pump.py:772
RowSelected(cursor_row=3, row_key=<textual.widgets._data_table.RowKey object at 0x110335130>) >>> Screen(id='_default') method=None
[18:44:51] EVENT message_pump.py:772
RowSelected(cursor_row=3, row_key=<textual.widgets._data_table.RowKey object at 0x110335130>) >>> JaasTop(title='JaasTop', classes={'-dark-mode'}, pseudo_classes={'dark', 'focus'}) method=None It seems a cursor row 0 gets triggered by default and my row 3 is ignored. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Here's how you might programmatically select a row in the the table: # First move the cursor to the given row
# either using the `move_cursor` method:
table.move_cursor(row=index)
# Or by setting the `cursor_coordinate` reactive attribute:
# table.cursor_coordinate = Coordinate(index, 0)
# Then select the row under the cursor
table.action_select_cursor() The reason your code does not work is that messages are intended to notify parent widgets of state changes or input, not modify the states of the widgets themselves. I'd recommend working through the Tutorial if you haven't already, and refer to the Guide for a deeper dive into events and messages. |
Beta Was this translation helpful? Give feedback.
-
Messages are generally sent to inform you when things happen. To change state, you generally either modify reactives or call methods. In this case, I think you need Tom, beat me to it! |
Beta Was this translation helpful? Give feedback.
Here's how you might programmatically select a row in the the table:
The reason your code does not work is that messages are intended to notify parent widgets of state changes or input, not modify the states of the widgets themselves. I'd recommend working through the Tutorial if you haven't already, and refer to the Guide for a deeper dive into events and messages.