How to get key of selected row without event? #3623
-
I'm subclassing Well, I think I could do the following, but it seems extremely wasteful: row_data = dt.get_row_at(dt.cursor_row)
for key in dt.rows:
if dt.get_row(key) == row_data:
row_key = key
break Isn't there a way to get the row key of the currently selected row without iterating and comparing every row? 🤔 My code: class Column(Container):
BINDINGS = [
Binding("enter", "default", "Execute default action", priority=True),
]
def compose(self):
yield DataTable()
# on_mount, update, etc.
def action_default(self) -> None:
datatable = self.query_one(DataTable)
row_index = datatable.cursor_row
row = datatable.get_row_at(row_index)
# how to get row_key? As for why I want the row key, it's because it is necessary if I want to remove the row with |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
You can get the row and column keys using the line below... row_key, col_key = dt.coordinate_to_cell_key(dt.cursor_coordinate) I remove the selected row in a project of mine using the function call below. Please note, I'm using def action_remove_row(self) -> None:
"""Delete the currently selected row."""
if not self.row_count:
return
row_key, _ = self.coordinate_to_cell_key(self.cursor_coordinate)
self.remove_row(row_key) |
Beta Was this translation helpful? Give feedback.
You can get the row and column keys using the line below...
I remove the selected row in a project of mine using the function call below. Please note, I'm using
self
as a have a custom subclassed DataTable.