main.py
import flet as ft
def main(page: ft.Page):
"""縦横スクロール付きDataTable
縦スクロール(コンテナのheight必須):
ft.Container(
content=ft.ListView(
ft.DataTable()
)
height=CONTAINER_HEIGHT
width=CONTAINER_WIDTH
)
横スクロール(scroll="auto" or "always"必須):
ft.Row(scroll="auto")
ウィンドウ幅 < コンテンツ幅
"""
CONTAINER_HEIGHT = 500
CONTAINER_WIDTH = 450
page.window_width = 400
dt_cols = []
for col in ["ID", "Name", "Age"]:
dt_cols.append(ft.DataColumn(ft.Text(col)))
dt_rows = []
for row in [(x, f"Test{x}", x) for x in range(1, 101)]:
cells = []
for cell in row:
cells.append(ft.DataCell(ft.Text(cell)))
dt_rows.append(ft.DataRow(cells))
dt = ft.DataTable(
columns=dt_cols,
rows=dt_rows
)
lv = ft.ListView([dt], expand=1, spacing=10, padding=20)
page.add(
ft.Row(
[
ft.Container(
content=lv,
border=ft.border.all(1),
height=CONTAINER_HEIGHT,
width=CONTAINER_WIDTH,
),
],
scroll="auto",
)
)
ft.app(target=main)