aggrid_documentation.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 AGgrid Rows', '''
  23. Selection checkboxes can be added to rows, allowing for retrieval of user selection.\n
  24. Note that the on click for the buttons receive the callable function (no parentheses) and that they are not lambda functions.\n
  25. See the [AGgrid documentation](https://www.ag-grid.com/javascript-data-grid/row-selection/#example-single-row-selection) for more information.
  26. ''')
  27. def aggrid_with_selectable_rows():
  28. grid = ui.aggrid({
  29. 'columnDefs': [
  30. {'headerName': 'Name', 'field': 'name', 'checkboxSelection': True},
  31. {'headerName': 'Age', 'field': 'age'},
  32. ],
  33. 'rowData': [
  34. {'name': 'Alice', 'age': 18},
  35. {'name': 'Bob', 'age': 21},
  36. {'name': 'Carol', 'age': 42},
  37. ],
  38. 'rowSelection': 'multiple',
  39. }).classes('max-h-40')
  40. async def get_rows():
  41. # This function grabs all selected rows (use when rowSelection is multiple)
  42. # Obtain rows
  43. rows = await grid.get_selected_rows()
  44. # Check if empty
  45. if rows == []:
  46. ui.notify("None selected!")
  47. return
  48. # Process as desired
  49. for row in rows:
  50. row_name = row["name"]
  51. row_age = row["age"]
  52. ui.notify(f"{row_name}, {row_age}")
  53. async def get_row():
  54. # This function grabs the row you selected (use when rowSelection is single)
  55. # Obtain row
  56. row = await grid.get_selected_row()
  57. # Check if None
  58. if row is None:
  59. ui.notify("None selected!")
  60. return
  61. # Process as desired
  62. row_name = row["name"]
  63. row_age = row["age"]
  64. ui.notify(f"{row_name}, {row_age}")
  65. ui.button('Get Rows', on_click=get_rows)
  66. ui.button('Get Row', on_click=get_row)
  67. ui.run()