Update the whole data / a row / a cell of the datatable. To use `datatable_update()`, you need to specify the ``instance_id`` parameter when calling :py:func:`put_datatable()`. When ``row_id`` and ``field`` is not specified (``datatable_update(instance_id, data)``), the whole data
(
instance_id: str,
data: Any,
row_id: Union[int, str] = None,
field: Union[str, List[str], Tuple[str]] = None
)
| 1693 | |
| 1694 | |
| 1695 | def datatable_update( |
| 1696 | instance_id: str, |
| 1697 | data: Any, |
| 1698 | row_id: Union[int, str] = None, |
| 1699 | field: Union[str, List[str], Tuple[str]] = None |
| 1700 | ): |
| 1701 | """ |
| 1702 | Update the whole data / a row / a cell of the datatable. |
| 1703 | |
| 1704 | To use `datatable_update()`, you need to specify the ``instance_id`` parameter when calling :py:func:`put_datatable()`. |
| 1705 | |
| 1706 | When ``row_id`` and ``field`` is not specified (``datatable_update(instance_id, data)``), |
| 1707 | the whole data of datatable will be updated, in this case, |
| 1708 | the ``data`` parameter should be a list of dict (same as ``records`` in :py:func:`put_datatable()`). |
| 1709 | |
| 1710 | To update a row, specify the ``row_id`` parameter and pass the row data in dict to ``data`` |
| 1711 | parameter (``datatable_update(instance_id, data, row_id)``). |
| 1712 | See ``id_field`` of :py:func:`put_datatable()` for more info of ``row_id``. |
| 1713 | |
| 1714 | To update a cell, specify the ``row_id`` and ``field`` parameters, in this case, the ``data`` parameter should be |
| 1715 | the cell value To update a row, specify the ``row_id`` parameter and pass the row data in dict to ``data`` |
| 1716 | parameter (``datatable_update(instance_id, data, row_id, field)``). |
| 1717 | The ``field`` can be a tuple to indicate nested key path. |
| 1718 | """ |
| 1719 | from .session import run_js |
| 1720 | |
| 1721 | instance_id = f"ag_grid_{instance_id}_promise" |
| 1722 | if row_id is None and field is None: # update whole table |
| 1723 | run_js("""window[instance_id] ? window[instance_id].then((grid) => { |
| 1724 | grid.api.setRowData(data.map((row) => grid.flatten_row(row))) |
| 1725 | }) : console.error(`Datatable instance [${instance_id}] not found`); |
| 1726 | """, instance_id=instance_id, data=data) |
| 1727 | |
| 1728 | if row_id is not None and field is None: # update whole row |
| 1729 | run_js("""window[instance_id] ? window[instance_id].then((grid) => { |
| 1730 | let row = grid.api.getRowNode(row_id); |
| 1731 | if (row) row.setData(grid.flatten_row(data)) |
| 1732 | }) : console.error(`Datatable instance [${instance_id}] not found`); |
| 1733 | """, instance_id=instance_id, row_id=row_id, data=data) |
| 1734 | |
| 1735 | if row_id is not None and field is not None: # update field |
| 1736 | if not isinstance(field, (list, tuple)): |
| 1737 | field = [field] |
| 1738 | run_js("""window[instance_id] ? window[instance_id].then((grid) => { |
| 1739 | let row = grid.api.getRowNode(row_id); |
| 1740 | if (row) |
| 1741 | row.setDataValue(grid.path2field(path), data) && |
| 1742 | grid.api.refreshClientSideRowModel(); |
| 1743 | }) : console.error(`Datatable instance [${instance_id}] not found`); |
| 1744 | """, instance_id=instance_id, row_id=row_id, data=data, path=field) |
| 1745 | |
| 1746 | if row_id is None and field is not None: |
| 1747 | raise ValueError("`row_id` is required when provide `field`") |
| 1748 | |
| 1749 | |
| 1750 | def datatable_insert(instance_id: str, records: List, row_id=None): |
nothing calls this directly
no test coverage detected
searching dependent graphs…