Posted 24 March 2023, 10:04 am EST - Updated 24 March 2023, 10:09 am EST
Hi there,
I am attempting to create a Wijmo FlexGrid in React with a default extra row at the bottom for adding new items to the grid. The grid has three columns, with one of the columns being a dropdown that uses a data map, while the other two are simple numbers.
I have attached an image of the grid.
The first issue I am encountering is that when I edit one column on the new row and move to the next, the previous data is getting removed.
The second issue is that the selected data from the dropdown is not being retained.
For reference below is my code.
[code]
import { Modal } from “react-bootstrap”
import { FlexGrid, FlexGridColumn } from ‘@grapecity/wijmo.react.grid’;
import { DataMap } from ‘@grapecity/wijmo.grid’;
import { useRef, useState } from “react”;
import { Transform, Type } from “class-transformer”;
import { Money } from “…/…/models/money”;
export class QuoteCostGrid {
costId: number = 0;
description: string = “”;
itemId: string = “”;
@Type(() => Money) @Transform(({ value }) => new Money(value), { toClassOnly: true }) price: Money | null = null; @Type(() => Money) @Transform(({ value }) => new Money(value), { toClassOnly: true }) retail: Money | null = null; markup: number = 65;
}
export function QuoteCosts({ showCostpopup, closeCostpopup, selectedOption }: { showCostpopup: boolean, closeCostpopup: () => void, selectedOption:number|null }) {
const [editingRowData, setEditingRowData] = useState({});
const [costGridData, setCostGridData] = useState<QuoteCostGrid>();
const gridRowHeight = 28;
const gridRef = useRef<any | null>(null);
const costItems = [{ “itemId”: “LABRUGREPAIRC”, “description”: “RUG REPAIR | AREA RUG REPAIR COMMERCIAL” },
{ “itemId”: “LABHOMESERVICEC”, “description”: “IN HOME SERVICE | INSTALLATION COMMERCIAL” },
{ “itemId”: “LABINSTLABORC”, “description”: “INSTALLATION: LABOR | INSTALLATION COMMERCIAL” },
{ “itemId”: “LABINSTMISCC”, “description”: “INSTALLATION: MISC CHARGE | INSTALLATION COMMERCIAL” }]
const costItemsMap = new DataMap(costItems, ‘itemId’, ‘description’);
const initializeGrid = (flex: any) => { gridRef.current = flex; flex.select(-1, -1); flex.rows.defaultSize = gridRowHeight; flex.bottomLeftCells.setCellData(0, 0, '?'); flex.columnHeaders.rows.defaultSize = gridRowHeight; flex.columnFooters.rows.defaultSize = gridRowHeight; flex.beginningEdit.addHandler((s: any, args: any) => { let selectedRowIndex = s.selection.row; if (selectedRowIndex != -1) { const currentRow = s.rows[selectedRowIndex].dataItem; console.log(s.rows); if (currentRow) { console.log(currentRow); const otherValues = { itemId: currentRow.itemId, price: currentRow.price, retail: currentRow.retail, markup: currentRow.markup }; setEditingRowData(otherValues); } } }); flex.cellEditEnded.addHandler((s: any, args: any) => { let selectedRowIndex = s.selection.row; if (selectedRowIndex != -1) { const currentRow = s.rows[selectedRowIndex].dataItem; console.log(currentRow); const newData = [...costGridData]; newData[selectedRowIndex] = { ...currentRow }; setCostGridData(newData); setEditingRowData({}); } }); } const handleRowAdded = (e: any) => { const newItem = new QuoteCostGrid(); setCostGridData(costGridData.concat(newItem)); //setCostGridData([...costGridData, newItem]); }; return ( <Modal show={showCostpopup} fullscreen={"xl-down"} size={ "lg"} className={"quote-cost-modal"} onHide={closeCostpopup} > <Modal.Header closeButton> <Modal.Title>Quote - Costs {selectedOption}</Modal.Title> </Modal.Header> <Modal.Body className={"quote-cost-body"} > <FlexGrid initialized={initializeGrid} headersVisibility={'Column'} headerHeight={50} itemsSource={costGridData} allowAddNew={true} newRowAtTop={false} onRowAdded={handleRowAdded} > <FlexGridColumn header="Item" binding="itemId" name="itemId" dataMap={costItemsMap} width="2*" align="left" /> <FlexGridColumn header="Cost" binding="price" name="price" width="*" align="right" /> <FlexGridColumn header="Net" binding="retail" name="retail" width="*" align="right" /> <FlexGridColumn header="M/U%" binding="markup" name="markup" width="*" align="center" /> <FlexGridColumn header=" " binding="option" name="option" width="*" align="center" /> </FlexGrid> </Modal.Body> </Modal> )
}
[/code]