aggrid_documentation.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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('AG Grid with Conditional Cell Formatting', '''
  78. This demo shows how to use [cellClassRules](https://www.ag-grid.com/javascript-grid-cell-styles/#cell-class-rules)
  79. to conditionally format cells based on their values.
  80. Since it is currently not possible to use the `cellClassRules` option in the `columnDefs` option,
  81. we use the `run_javascript` method to set the `cellClassRules` option after the grid has been created.
  82. The timer is used to delay the execution of the javascript code until the grid has been created.
  83. You can also use `app.on_connect` instead.
  84. ''')
  85. def aggrid_with_conditional_cell_formatting():
  86. ui.html('''
  87. <style>
  88. .cell-fail { background-color: #f6695e; }
  89. .cell-pass { background-color: #70bf73; }
  90. </style>
  91. ''')
  92. grid = ui.aggrid({
  93. 'columnDefs': [
  94. {'headerName': 'Name', 'field': 'name'},
  95. {'headerName': 'Age', 'field': 'age'},
  96. ],
  97. 'rowData': [
  98. {'name': 'Alice', 'age': 18},
  99. {'name': 'Bob', 'age': 21},
  100. {'name': 'Carol', 'age': 42},
  101. ],
  102. })
  103. async def format() -> None:
  104. await ui.run_javascript(f'''
  105. getElement({grid.id}).gridOptions.columnApi.getColumn("age").getColDef().cellClassRules = {{
  106. "cell-fail": x => x.value < 21,
  107. "cell-pass": x => x.value >= 21,
  108. }};
  109. getElement({grid.id}).gridOptions.api.refreshCells();
  110. ''', respond=False)
  111. ui.timer(0, format, once=True)