AgGrid 无法恢复原始网格数据

huangapple go评论263阅读模式
英文:

AgGrid Unable To Restore Original Grid Data

问题

我在初始 API 调用后填充了网格。其中一个字段是可编辑的 - 价格

如果我编辑一个价格单元格,然后点击还原按钮,原始数据集会被应用。

如果我再次编辑一个价格单元格,还原功能将不再起作用。

示例:https://stackblitz.com/edit/ag-grid-react-hello-world-agjiy2?file=index.js

代码:

英文:

I populate the grid after an intial API call. One of the fields is editiable - Price

If i edit a Price cell, and click the Restore button, the original dataset is applied.

If i edit a Price cell again, the Restore no longer works.

Example: https://stackblitz.com/edit/ag-grid-react-hello-world-agjiy2?file=index.js

Code:

  1. import React, { useState, useEffect, useRef } from 'react';
  2. import { render } from 'react-dom';
  3. import { AgGridReact } from 'ag-grid-react';
  4. import 'ag-grid-community/styles/ag-grid.css';
  5. import 'ag-grid-community/styles/ag-theme-alpine.css';
  6. const myPromise = new Promise((resolve, reject) => {
  7. setTimeout(() => {
  8. resolve(
  9. [
  10. { make: 'Toyota', model: 'Celica', price: 35000 },
  11. { make: 'Ford', model: 'Mondeo', price: 32000 },
  12. { make: 'Porsche', model: 'Boxter', price: 72000 },
  13. ]
  14. );
  15. }, 300);
  16. });
  17. const App = () => {
  18. const [rowData, setRowData] = useState([]);
  19. const initData = useRef([]);
  20. const [columnDefs] = useState([
  21. { field: 'make' },
  22. { field: 'model' },
  23. { field: 'price', editable: true },
  24. ]);
  25. useEffect(() => {
  26. let data = myPromise
  27. .then((data) => {
  28. console.log(data);
  29. setRowData(data);
  30. initData.current = JSON.parse(JSON.stringify(data));
  31. });
  32. }, []);
  33. const restore = () => {
  34. setRowData(initData.current);
  35. }
  36. return (
  37. <div>
  38. <button onClick={restore}>Back to original</button>
  39. <br></br>
  40. <div className="ag-theme-alpine" style={{ height: 400, width: 600 }}>
  41. <AgGridReact rowData={rowData} columnDefs={columnDefs}></AgGridReact>
  42. </div>
  43. </div>
  44. );
  45. };

答案1

得分: 1

  1. 更改您的恢复方法如下
  2. ```javascript
  3. const restore = () => {
  4. console.log('initData', initData)
  5. var obj = JSON.parse(JSON.stringify(initData.current))
  6. setRowData(obj);
  7. }

这是因为您在编辑操作之后修改了对象。为了防止它被修改,获取您的initData对象的深拷贝并使用它。

示例代码:

https://stackblitz.com/edit/ag-grid-react-hello-world-fnxqdy?file=index.js

  1. <details>
  2. <summary>英文:</summary>
  3. Change your restore method like that

const restore = () => {
console.log('initData', initData)
var obj = JSON.parse(JSON.stringify(initData.current))
setRowData(obj);
}

  1. It&#39;s happening because you modify your object after your edit action. To prevent it modifying, get a deep copy of your initData object and use it.
  2. Playground:
  3. [https://stackblitz.com/edit/ag-grid-react-hello-world-fnxqdy?file=index.js][1]
  4. [1]: https://stackblitz.com/edit/ag-grid-react-hello-world-fnxqdy?file=index.js
  5. </details>

huangapple
  • 本文由 发表于 2023年7月20日 22:19:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76730845.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定