Skip to content

Add remove_row and remove_rows to the DataTable #2189

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## Unreleased

- Add `remove_row` and `remove_rows` methods to the DataTable https://github.com/Textualize/textual/discussions/2116

### Fixed

- Fixed bindings persistance https://github.com/Textualize/textual/issues/1613
Expand Down
34 changes: 34 additions & 0 deletions src/textual/widgets/_data_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -1287,6 +1287,28 @@ def add_row(
self.check_idle()
return row_key

def remove_row(self, row_key: RowKey) -> Self:
"""Remove a row.
Args:
row_key: Key describing the specific row to remove

Returns:
The `DataTable` instance.
"""
if row_key not in self._row_locations:
raise RowDoesNotExist(f"The row key {row_key!r} already exists.")

del self._row_locations[row_key]
del self._data[row_key]
del self.rows[row_key]

# TODO should the cursor be modified?

self._new_rows.remove(row_key)
self._update_count -= 1
self.check_idle()
return self

def add_columns(self, *labels: TextType) -> list[ColumnKey]:
"""Add a number of columns.

Expand Down Expand Up @@ -1321,6 +1343,18 @@ def add_rows(self, rows: Iterable[Iterable[CellType]]) -> list[RowKey]:
row_keys.append(row_key)
return row_keys

def remove_rows(self, row_keys: Iterable[RowKey]) -> Self:
"""Remove a number of rows.
Args:
row_keys: Iterable of keys. A key describes the specific row to remove.

Returns:
The `DataTable` instance.
"""
for row_key in row_keys:
self.remove_row(row_key)
return self

def on_idle(self) -> None:
"""Runs when the message pump is empty.

Expand Down