-
Data Grid
- Overview
-
Data Binding
-
Paging and Scrolling
-
Editing
-
Grouping
-
Filtering and Sorting
- Focused Row
-
Row Drag & Drop
-
Selection
-
Columns
- State Persistence
-
Appearance
-
Templates
-
Data Summaries
-
Master-Detail
-
Export to PDF
-
Export to Excel
-
Adaptability
- Keyboard Navigation
-
Pivot Grid
- Overview
-
Data Binding
-
Field Chooser
-
Features
-
Export to Excel
-
Tree List
- Overview
-
Data Binding
- Sorting
- Paging
-
Editing
- Node Drag & Drop
- Focused Row
-
Selection
-
Filtering
-
Column Customization
- State Persistence
- Adaptability
- Keyboard Navigation
-
Scheduler
- Overview
-
Data Binding
-
Views
-
Features
- Virtual Scrolling
-
Grouping
-
Customization
- Adaptability
-
Html Editor
-
Chat
-
Diagram
- Overview
-
Data Binding
-
Featured Shapes
-
Custom Shapes
-
Document Capabilities
-
User Interaction
- UI Customization
- Adaptability
-
Charts
- Overview
-
Data Binding
-
Area Charts
-
Bar Charts
- Bullet Charts
-
Doughnut Charts
-
Financial Charts
-
Line Charts
-
Pie Charts
-
Point Charts
-
Polar and Radar Charts
-
Range Charts
-
Sparkline Charts
-
Tree Map
-
Funnel and Pyramid Charts
- Sankey Chart
-
Combinations
-
More Features
-
Export
-
Selection
-
Tooltips
-
Zooming
-
-
Gantt
- Overview
-
Data
-
UI Customization
- Strip Lines
- Export to PDF
- Sorting
-
Filtering
-
Gauges
- Overview
-
Data Binding
-
Bar Gauge
-
Circular Gauge
-
Linear Gauge
-
Navigation
- Overview
- Accordion
-
Menu
- Multi View
-
Drawer
-
Tab Panel
-
Tabs
-
Toolbar
- Pagination
-
Tree View
- Right-to-Left Support
-
Layout
-
Tile View
- Splitter
-
Gallery
- Scroll View
- Box
- Responsive Box
- Resizable
-
-
Editors
- Overview
- Autocomplete
-
Calendar
- Check Box
- Color Box
-
Date Box
-
Date Range Box
-
Drop Down Box
-
Number Box
-
Select Box
- Switch
-
Tag Box
- Text Area
- Text Box
- Validation
- Custom Text Editor Buttons
- Right-to-Left Support
- Editor Appearance Variants
-
Forms and Multi-Purpose
- Overview
- Button Group
- Field Set
-
Filter Builder
-
Form
- Radio Group
-
Range Selector
- Numeric Scale (Lightweight)
- Numeric Scale
- Date-Time Scale (Lightweight)
- Date-Time Scale
- Logarithmic Scale
- Discrete scale
- Custom Formatting
- Use Range Selection for Calculation
- Use Range Selection for Filtering
- Image on Background
- Chart on Background
- Customized Chart on Background
- Chart on Background with Series Template
- Range Slider
- Slider
-
Sortable
-
File Management
-
File Manager
- Overview
-
File System Types
-
Customization
-
File Uploader
-
-
Actions and Lists
- Overview
-
Action Sheet
-
Button
- Floating Action Button
- Drop Down Button
-
Context Menu
-
List
-
Lookup
-
Maps
- Overview
-
Map
-
Vector Map
-
Dialogs and Notifications
-
Localization
React Data Grid - Edit State Management
Our DataGrid component manages its edit state automatically. If your use case requires full control over the editing process, you can use the API members below to manage state manually. In this demo, we manage state with a help of the useReducer React hook.
If you have technical questions, please create a support ticket in the DevExpress Support Center.
import React, {
useCallback, useEffect, useMemo, useReducer,
} from 'react';
import DataGrid, {
Column, DataGridTypes, Editing, Pager,
} from 'devextreme-react/data-grid';
import { LoadPanel } from 'devextreme-react/load-panel';
import 'whatwg-fetch';
import reducer, { State } from './reducer.ts';
import {
saveChange, loadOrders, setChanges, setEditRowKey,
} from './actions.ts';
const initialState: State = {
data: [],
changes: [],
editRowKey: null,
isLoading: false,
};
const loadPanelPosition = { of: '#gridContainer' };
const App = () => {
const [state, dispatch] = useReducer(reducer, initialState);
const changesText = useMemo(() => JSON.stringify(state.changes.map((change) => ({
type: change.type,
key: change.type !== 'insert' ? change.key : undefined,
data: change.data,
})), null, ' '), [state.changes]);
useEffect(() => {
loadOrders(dispatch);
}, []);
const onSaving = useCallback((e: DataGridTypes.SavingEvent) => {
e.cancel = true;
e.promise = saveChange(dispatch, e.changes[0]);
}, []);
const onChangesChange = useCallback((changes: DataGridTypes.DataChange[]) => {
setChanges(dispatch, changes);
}, []);
const onEditRowKeyChange = useCallback((editRowKey) => {
setEditRowKey(dispatch, editRowKey);
}, []);
return (
<React.Fragment>
<LoadPanel
position={loadPanelPosition}
visible={state.isLoading}
/>
<DataGrid
id="gridContainer"
keyExpr="OrderID"
dataSource={state.data}
showBorders
repaintChangesOnly
onSaving={onSaving}>
<Editing
mode="row"
allowAdding
allowDeleting
allowUpdating
changes={state.changes}
onChangesChange={onChangesChange}
editRowKey={state.editRowKey}
onEditRowKeyChange={onEditRowKeyChange}
/>
<Pager visible={true} />
<Column dataField="OrderID" allowEditing={false}></Column>
<Column dataField="ShipName"></Column>
<Column dataField="ShipCountry"></Column>
<Column dataField="ShipCity"></Column>
<Column dataField="ShipAddress"></Column>
<Column dataField="OrderDate" dataType="date"></Column>
<Column dataField="Freight"></Column>
</DataGrid>
<div className="options">
<div className="caption">Options</div>
<div className="option">
<span>Edit Row Key:</span>
<div id="editRowKey">{state.editRowKey === null ? 'null' : state.editRowKey.toString()}</div>
</div>
<div className="option">
<span>Changes:</span>
<div id="changes">{changesText}</div>
</div>
</div>
</React.Fragment>
);
};
export default App;
xxxxxxxxxx
xxxxxxxxxx
xxxxxxxxxx
xxxxxxxxxx
xxxxxxxxxx
xxxxxxxxxx
xxxxxxxxxx
xxxxxxxxxx
xxxxxxxxxx
xxxxxxxxxx
xxxxxxxxxx
xxxxxxxxxx
Component Properties
-
editing.editRowKey
The key for the row being edited. -
editing.editColumnName
The name or data field of the column being edited. -
editing.changes
Pending row changes.
Bind these properties to state props and set the props to change edit state at runtime. In this demo, we bind the editRowKey and changes properties and use the corresponding on_OptionName_Change event handlers to set the bound props.
Utility Method
- DevExpress.data.applyChanges(data, changes, options)
Applies an array of changes to a source data array.
Event Handlers
-
onSaving / onSaved
Functions that are called before / after pending row changes are saved via the UI or programmatically. -
onEditCanceling / onEditCanceled
Functions that are called before / after editing is canceled and pending row changes are discarded.
Use these functions to perform custom actions. In this demo, the onSaving function sends pending changes to a server. The function's parameter e
contains fields for this capability. To implement the same in your application, follow these steps:
-
Disable built-in edit state management
Set thee.cancel
field to true. -
Send a request to the server
Pending changes are stored in thee.changes
array. This array has only a single element in all edit modes, except for batch. Check if this element is not empty and send it to the server (see thesaveChange
action inactions.js
). -
Apply the same changes to the DataGrid's data source and reset edit state
If the server successfully saves changes, call the applyChanges method to save the same changes in the DataGrid's data source. Assign null to the editRowKey and an empty array to the changes property. This resets edit state (see theSAVING_SUCCESS
handler inreducer.js
).