Большая проблема здесь заключается в том, что мой гараж параметров правильно загружается в компонент, но, кажется, не доступен ни в одной из функций внутри компонента. Я не сталкивался с этим раньше, и Grid Ag - это самый новый инструмент, который я должен винить в странном поведении. Я пытаюсь преобразовать таблицу, изменяя данные его строки и столбца. Вместо этого может быть легче создать новую таблицу, но надеется не идти по этому пути. < /P>
import { agGridHelpers } from "../../../helpers";
import {AgGridReact} from 'ag-grid-react';
import { ModuleRegistry, AllCommunityModule} from "ag-grid-community";
import { useState, useRef, useEffect } from "react";
ModuleRegistry.registerModules([AllCommunityModule]);
/* Returns a table filled with data from the garage state object. if there is no data in the object only table headers appear.*/
const CustomerApplications = ({garage})=>{
const [rowData, setRowData] = useState([]);
console.log(garage); //logs the correct array.
useEffect(()=>{
console.log("last");
setRowData(garage);
}, [garage])
const [colDefs, setColDefs] = useState([
{headerName: "Nickname", field: "application_name", maxWidth: 250, minWidth: 200},
{headerName: "Application", children: [{field: "make"}, {headerName: "Model", field: "model"}, {headerName: "Year", field: "year"},
{headerName: "Engine", field: "engine"},]},
{headerName: "Batteries", maxWidth: 500, minWidth:200, valueSetter: p=> "See Batteries",
valueGetter: p=> "See Batteries", cellDataType: 'text', onCellClicked: e=>newTable(e)}
])
const newTable = (e)=>{
console.log(e);
console.log(garage); // logs an empty array
setRowData(e.data.batteries);
setColDefs([{headerName: "Back", onCellClicked: e=> resetTable(garage)},{headerName: "Battery", field: 'internal_part_number'}, {headerName: "CA", field: "attributes.1.attribute_value"},
{headerName: "CCA", field: 'attributes.0.attribute_value'}, {headerName: "Price", field: "list"}])
console.log(garage); //still logging empty array.
}
const resetTable = (garage)=>{
console.log("Reseting");
console.log(garage); //also an empty array.
setRowData(garage);
setColDefs([{headerName: "Nickname", field: "application_name", maxWidth: 250, minWidth: 200},
{headerName: "Application", children: [{field: "make"}, {headerName: "Model", field: "model"}, {headerName: "Year", field: "year"}, {headerName: "Engine", field: "engine"},]},
{headerName: "Batteries", maxWidth: 500, minWidth:200, valueGetter: p=> "See Batteries", cellDataType: 'text',
onCellClicked: e=> newTable(e)}
]);
}
return (
);
};
export default CustomerApplications;
Подробнее здесь: https://stackoverflow.com/questions/795 ... nto-a-grid