aggrid_documentation.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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)
  59. @text_demo('Filter Rows using Mini Filters', '''
  60. You can add [mini filters](https://ag-grid.com/javascript-data-grid/filter-set-mini-filter/)
  61. to the header of each column to filter the rows.
  62. Note how the "agTextColumnFilter" matches individual characters, like "a" in "Alice" and "Carol",
  63. while the "agNumberColumnFilter" matches the entire number, like "18" and "21", but not "1".
  64. ''')
  65. def aggrid_with_minifilters():
  66. ui.aggrid({
  67. 'columnDefs': [
  68. {'headerName': 'Name', 'field': 'name', 'filter': 'agTextColumnFilter', 'floatingFilter': True},
  69. {'headerName': 'Age', 'field': 'age', 'filter': 'agNumberColumnFilter', 'floatingFilter': True},
  70. ],
  71. 'rowData': [
  72. {'name': 'Alice', 'age': 18},
  73. {'name': 'Bob', 'age': 21},
  74. {'name': 'Carol', 'age': 42},
  75. ],
  76. }).classes('max-h-40')
  77. @text_demo('Create Grid from Pandas Dataframe', '''
  78. You can create an AG Grid from a Pandas Dataframe using the `from_pandas` method.
  79. This method takes a Pandas Dataframe as input and returns an AG Grid.
  80. ''')
  81. def aggrid_from_pandas():
  82. import pandas as pd
  83. df = pd.DataFrame(data={'col1': [1, 2], 'col2': [3, 4]})
  84. ui.aggrid.from_pandas(df).classes('max-h-40')