Skip to content

Commit e2ffcac

Browse files
masenfTom Gotsman
authored andcommitted
Optimize AG Grid state (#1082)
1 parent 7c853ad commit e2ffcac

File tree

1 file changed

+9
-15
lines changed

1 file changed

+9
-15
lines changed

docs/library/tables-and-data-grids/ag_grid.md

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -247,25 +247,21 @@ There are 7 provided cell editors in AG Grid:
247247

248248
In this example, we enable editing for the second and third columns. The second column uses the `number` cell editor, and the third column uses the `select` cell editor.
249249

250-
The `on_cell_value_changed` event trigger is linked to the `cell_value_changed` event handler in the state. This event handler is called whenever a cell value is changed and changes the value of the state var `data_df`.
251-
252-
`data` is a [computed var]({vars.computed_vars.path}), which means it has the `@rx.var` decorator and has its value derived from `data_df`.
250+
The `on_cell_value_changed` event trigger is linked to the `cell_value_changed` event handler in the state. This event handler is called whenever a cell value is changed and changes the value of the backend var `_data_df` and the state var `data`.
253251

254252
```python demo exec
255253
import reflex as rx
256254
from reflex_ag_grid import ag_grid
257255
import pandas as pd
258256

259257
class AGGridEditingState(rx.State):
260-
data_df = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/gapminder2007.csv")
261-
262-
@rx.var
263-
def data(self) -> list[dict]:
264-
return self.data_df.to_dict("records")
258+
_data_df = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/gapminder2007.csv")
259+
data: list[dict] = _data_df.to_dict("records")
265260

266261
@rx.event
267262
def cell_value_changed(self, row, col_field, new_value):
268-
self.data_df.at[row, col_field] = new_value
263+
self._data_df.at[row, col_field] = new_value
264+
self.data = self._data_df.to_dict("records")
269265
yield rx.toast(f"Cell value changed, Row: {row}, Column: {col_field}, New Value: {new_value}")
270266

271267

@@ -371,19 +367,17 @@ def ag_grid_simple_themes():
371367

372368
### Putting Data in State
373369

374-
Assuming you want to make any edit to your data, you can put the data in State. This allows you to update the grid based on user input. It is recommended to make `data` a [computed var]({vars.computed_vars.path}), which means it has the `@rx.var` decorator. The state var `data` has its value derived from `data_df`.
370+
Assuming you want to make any edit to your data, you can put the data in State. This allows you to update the grid based on user input. Whenever the `data` var is updated, the grid will be re-rendered with the new data.
375371

376372
```python demo exec
373+
from typing import Any
377374
import reflex as rx
378375
from reflex_ag_grid import ag_grid
379376
import pandas as pd
380377

381378
class AGGridState2(rx.State):
382-
data_df = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/gapminder2007.csv")
383-
384-
@rx.var
385-
def data(self) -> list[dict]:
386-
return self.data_df.to_dict("records")
379+
_data_df = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/gapminder2007.csv")
380+
data: list[dict] = _data_df.to_dict("records")
387381

388382
column_defs = [
389383
ag_grid.column_def(field="country"),

0 commit comments

Comments
 (0)