浏览代码

Merge pull request #363 from Diegiwg/defing-get_selected_rows-function

(feature) Exposing the AG Grid table's getSelectedRows function.
Rodja Trappe 2 年之前
父节点
当前提交
6c7cf97c2c
共有 2 个文件被更改,包括 52 次插入1 次删除
  1. 23 1
      nicegui/elements/table.py
  2. 29 0
      tests/test_table.py

+ 23 - 1
nicegui/elements/table.py

@@ -1,7 +1,8 @@
-from typing import Dict, List
+from typing import Dict, List, Optional
 
 from ..dependencies import register_component
 from ..element import Element
+from ..functions.javascript import run_javascript
 
 register_component('table', __file__, 'table.js', ['lib/ag-grid-community.min.js'])
 
@@ -41,3 +42,24 @@ class Table(Element):
         :param args: arguments to pass to the method
         """
         self.run_method('call_api_method', name, *args)
+
+    async def get_selected_rows(self) -> List[Dict]:
+        """Get the currently selected rows.
+
+        This method is especially useful when the table is configured with ``rowSelection: 'multiple'``.
+
+        See `AG Grid API <https://www.ag-grid.com/javascript-data-grid/row-selection/#reference-selection-getSelectedRows>`_ for more information.
+
+        :return: list of selected row data
+        """
+        return await run_javascript(f'return getElement({self.id}).gridOptions.api.getSelectedRows();')
+
+    async def get_selected_row(self) -> Optional[Dict]:
+        """Get the single currently selected row.
+
+        This method is especially useful when the table is configured with ``rowSelection: 'single'``.
+
+        :return: row data of the first selection if any row is selected, otherwise `None`
+        """
+        rows = await self.get_selected_rows()
+        return rows[0] if rows else None

+ 29 - 0
tests/test_table.py

@@ -1,3 +1,6 @@
+from selenium.webdriver.common.action_chains import ActionChains
+from selenium.webdriver.common.keys import Keys
+
 from nicegui import ui
 
 from .screen import Screen
@@ -82,3 +85,29 @@ def test_call_api_method_with_argument(screen: Screen):
     screen.should_contain('Alice')
     screen.should_not_contain('Bob')
     screen.should_not_contain('Carol')
+
+
+def test_get_selected_rows(screen: Screen):
+    table = ui.table({
+        'columnDefs': [{'field': 'name'}],
+        'rowData': [{'name': 'Alice'}, {'name': 'Bob'}, {'name': 'Carol'}],
+        'rowSelection': 'multiple',
+    })
+
+    async def get_selected_rows():
+        ui.label(str(await table.get_selected_rows()))
+    ui.button('Get selected rows', on_click=get_selected_rows)
+
+    async def get_selected_row():
+        ui.label(str(await table.get_selected_row()))
+    ui.button('Get selected row', on_click=get_selected_row)
+
+    screen.open('/')
+    screen.click('Alice')
+    screen.find('Bob')
+    ActionChains(screen.selenium).key_down(Keys.SHIFT).click(screen.find('Bob')).key_up(Keys.SHIFT).perform()
+    screen.click('Get selected rows')
+    screen.should_contain("[{'name': 'Alice'}, {'name': 'Bob'}]")
+
+    screen.click('Get selected row')
+    screen.should_contain("{'name': 'Alice'}")