Editing cell in a DataTable #2449
Replies: 4 comments 4 replies
-
It would certainly be useful. |
Beta Was this translation helpful? Give feedback.
-
How would you manage the type of the cell value if updated from input? Here's a quick example using the swimmers data from the docs. Let's say you are editing a cell in the 'time' column. Should this only allow input that can be cast as the same type of the original cell value (i.e. float)? Or just allow any input and trust the programmer to manage any errors later? from typing import Any
from textual.app import App, ComposeResult
from textual.screen import ModalScreen, Screen
from textual.widgets import DataTable, Input, TextLog
ROWS = [
("lane", "swimmer", "country", "time"),
(4, "Joseph Schooling", "Singapore", 50.39),
(2, "Michael Phelps", "United States", 51.14),
(5, "Chad le Clos", "South Africa", 51.14),
(6, "László Cseh", "Hungary", 51.14),
(3, "Li Zhuhao", "China", 51.26),
(8, "Mehdy Metella", "France", 51.58),
(7, "Tom Shields", "United States", 51.73),
(1, "Aleksandr Sadovnikov", "Russia", 51.84),
(10, "Darren Burns", "Scotland", 51.84),
]
class MainScreen(Screen):
def compose(self) -> ComposeResult:
yield DataTable()
yield TextLog()
def on_mount(self) -> None:
table = self.query_one(DataTable)
table.styles.margin = (3, 0)
table.add_columns(*ROWS[0]) # type: ignore[arg-type]
for number, row in enumerate(ROWS[1:], start=1):
table.add_row(*row, label=str(number))
table.focus()
def on_data_table_cell_selected(
self,
event: DataTable.CellSelected,
) -> None:
cell_value: Any = event.value
message = (
f"Original value of cell at {event.coordinate}"
f" is {event.value} and type {type(event.value)}"
)
self.query_one(TextLog).write(message)
self.app.push_screen(EditCellScreen(cell_value))
class EditCellScreen(ModalScreen):
def __init__(
self,
cell_value: Any,
name: str | None = None,
id: str | None = None,
classes: str | None = None,
) -> None:
super().__init__(name=name, id=id, classes=classes)
self.cell_value = cell_value
def compose(self) -> ComposeResult:
yield Input()
def on_mount(self) -> None:
cell_input = self.query_one(Input)
cell_input.value = str(self.cell_value)
cell_input.focus()
def on_input_submitted(self, event: Input.Submitted) -> None:
main_screen = self.app.get_screen("main")
table = main_screen.query_one(DataTable)
table.update_cell_at(
table.cursor_coordinate,
event.value,
update_width=True,
)
message = (
f"New value of cell at {table.cursor_coordinate}"
f" is {event.value} and type {type(event.value)}"
)
main_screen.query_one(TextLog).write(message)
self.app.pop_screen()
class ExampleApp(App):
SCREENS = {"main": MainScreen()}
def on_mount(self) -> None:
self.push_screen("main")
if __name__ == "__main__":
app = ExampleApp()
app.run() |
Beta Was this translation helpful? Give feedback.
-
Is there any other way to editing cell by clickable event in the datatable cell? |
Beta Was this translation helpful? Give feedback.
-
It would be great to have in place editing of table cells! |
Beta Was this translation helpful? Give feedback.
-
Is there much appetite for in place editing DataTable cells?
I'm thinking double click a cell and it 'becomes' an input, Enter to 'save' the new value and emit a
changed
event.Beta Was this translation helpful? Give feedback.
All reactions