Selaa lähdekoodia

Added functions to get client data to AgGrid element

Brian Landry 1 vuosi sitten
vanhempi
säilyke
1ca5bb82e2
1 muutettua tiedostoa jossa 28 lisäystä ja 0 poistoa
  1. 28 0
      nicegui/elements/aggrid.py

+ 28 - 0
nicegui/elements/aggrid.py

@@ -79,3 +79,31 @@ class AgGrid(Element):
         """
         rows = await self.get_selected_rows()
         return rows[0] if rows else None
+
+
+    async def get_client_data(self) -> List[Dict]:
+        """Gets the data from the client including any edits made by the client.
+
+        This method is especially useful when the grid is configured with ``'editable': True``.
+
+        See `AG Grid API <https://www.ag-grid.com/javascript-data-grid/accessing-data/>`_ for more information.
+
+        :return: list of row data
+        """
+        result = await run_javascript(
+            f"""
+            let rowData = [];
+            getElement({self.id}).gridOptions.api.forEachNode(node => rowData.push(node.data));
+            return rowData;
+            """
+        )
+        return cast(List[Dict], result)
+
+    async def load_client_data(self) -> None:
+        """Obtains client data and updates the element's row data with it.
+
+        This syncs edits made by the client in editable cells to the server.
+        """
+        client_row_data = await self.get_client_data()
+        self.options["rowData"] = client_row_data
+        self.update()