Replies: 2 comments
-
I think you would need to check the table height (or its container) following the from __future__ import annotations
from textual import events, on
from textual.app import App, ComposeResult
from textual.geometry import Size
from textual.message import Message
from textual.widgets import DataTable
class MyDataTable(DataTable):
DEFAULT_CSS = """
MyDataTable {
height: 1fr;
}
"""
class Resized(Message):
"""Posted by the `MyDataTable` widget when it is resized."""
def __init__(
self,
data_table: MyDataTable,
size: Size,
) -> None:
super().__init__()
self.data_table = data_table
self.size = size
@property
def control(self) -> MyDataTable:
return self.data_table
def on_resize(self, event: events.Resize) -> None:
self.post_message(
MyDataTable.Resized(self, event.size),
)
class ExampleApp(App):
def compose(self) -> ComposeResult:
yield MyDataTable()
def on_mount(self) -> None:
table = self.query_one(MyDataTable)
table.add_column("Column")
@on(MyDataTable.Resized)
def on_my_data_table_resized(self, event: MyDataTable.Resized) -> None:
table = event.data_table
available_rows = event.size.height - 1
if table.row_count != available_rows:
table.clear()
table.add_row("FIRST ROW")
for i in range(available_rows - 2):
table.add_row(f"Row #{i}")
table.add_row("LAST ROW")
if __name__ == "__main__":
app = ExampleApp()
app.run() |
Beta Was this translation helpful? Give feedback.
0 replies
-
Yeah, that's absolutely perfect, dude. Thanks as ever!! |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hey Guys,
For a DataTable, I would like for it vertically to fill the screen space available (but no vertical scroll bars.)
For rows without available content of my own, I would pad with 'empty rows'
Why do this? Because I want to anchor some rows to the bottom of the table, and some to the top. So I figured the best way to do this would be to turn off scrollbars, fill the table area with blank rows, then 'poke' row data in as I see fit.
If this is viable, how would I go about it? I guess the number I want to get to is "number of rows this table could create without vertical scroll bars appearing. Is this number easily available via the widget class?
widget.size looks pretty good.... at what stage is widget.size known? I see because it's a table, the "size" is number of rows and columns I populate it with. So it is "0" until I populate it.
Do I need the container size instead, something like that?
Cheers,
Beta Was this translation helpful? Give feedback.
All reactions