Bladeren bron

Merge pull request #1079 from BrianLandry/aggrid-editable

Added functions to get client data to AgGrid element
Falko Schindler 1 jaar geleden
bovenliggende
commit
48c964f7ed
1 gewijzigde bestanden met toevoegingen van 31 en 0 verwijderingen
  1. 31 0
      nicegui/elements/aggrid.py

+ 31 - 0
nicegui/elements/aggrid.py

@@ -76,3 +76,34 @@ class AgGrid(Element, component='aggrid.js', libraries=['lib/aggrid/ag-grid-comm
         """
         rows = await self.get_selected_rows()
         return rows[0] if rows else None
+
+    async def get_client_data(self) -> List[Dict]:
+        """Get 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.
+
+        Note that when editing a cell, the row data is not updated until the cell exits the edit mode.
+        This does not happen when the cell loses focus, unless ``stopEditingWhenCellsLoseFocus: True`` is set.
+
+        :return: list of row data
+        """
+        result = await run_javascript(f'''
+            const 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:
+        """Obtain client data and update the element's row data with it.
+
+        This syncs edits made by the client in editable cells to the server.
+
+        Note that when editing a cell, the row data is not updated until the cell exits the edit mode.
+        This does not happen when the cell loses focus, unless ``stopEditingWhenCellsLoseFocus: True`` is set.
+        """
+        client_row_data = await self.get_client_data()
+        self.options['rowData'] = client_row_data
+        self.update()