aggrid_documentation.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. from nicegui import ui
  2. from ..documentation_tools import text_demo
  3. def main_demo() -> None:
  4. grid = ui.aggrid({
  5. 'columnDefs': [
  6. {'headerName': 'Name', 'field': 'name'},
  7. {'headerName': 'Age', 'field': 'age'},
  8. ],
  9. 'rowData': [
  10. {'name': 'Alice', 'age': 18},
  11. {'name': 'Bob', 'age': 21},
  12. {'name': 'Carol', 'age': 42},
  13. ],
  14. 'rowSelection': 'multiple',
  15. }).classes('max-h-40')
  16. def update():
  17. grid.options['rowData'][0]['age'] += 1
  18. grid.update()
  19. ui.button('Update', on_click=update)
  20. ui.button('Select all', on_click=lambda: grid.call_api_method('selectAll'))
  21. def more() -> None:
  22. @text_demo('Select AG Grid Rows', '''
  23. You can add checkboxes to grid cells to allow the user to select single or multiple rows.
  24. To retrieve the currently selected rows, use the `get_selected_rows` method.
  25. This method returns a list of rows as dictionaries.
  26. If `rowSelection` is set to `'single'` or to get the first selected row,
  27. you can also use the `get_selected_row` method.
  28. This method returns a single row as a dictionary or `None` if no row is selected.
  29. See the [AG Grid documentation](https://www.ag-grid.com/javascript-data-grid/row-selection/#example-single-row-selection) for more information.
  30. ''')
  31. def aggrid_with_selectable_rows():
  32. grid = ui.aggrid({
  33. 'columnDefs': [
  34. {'headerName': 'Name', 'field': 'name', 'checkboxSelection': True},
  35. {'headerName': 'Age', 'field': 'age'},
  36. ],
  37. 'rowData': [
  38. {'name': 'Alice', 'age': 18},
  39. {'name': 'Bob', 'age': 21},
  40. {'name': 'Carol', 'age': 42},
  41. ],
  42. 'rowSelection': 'multiple',
  43. }).classes('max-h-40')
  44. async def output_selected_rows():
  45. rows = await grid.get_selected_rows()
  46. if rows:
  47. for row in rows:
  48. ui.notify(f"{row['name']}, {row['age']}")
  49. else:
  50. ui.notify('No rows selected.')
  51. async def output_selected_row():
  52. row = await grid.get_selected_row()
  53. if row:
  54. ui.notify(f"{row['name']}, {row['age']}")
  55. else:
  56. ui.notify('No row selected!')
  57. ui.button('Output selected rows', on_click=output_selected_rows)
  58. ui.button('Output selected row', on_click=output_selected_row)