React TreeList - Editing

NOTE
Editing does not work with hierarchical data sources out of the box, but you can use the code sample from this KB to implement it.

Remote CRUD Operations Demo

User Interaction

The TreeList UI component allows a user to add, delete and update data. Assign true to the corresponding field of the editing object to enable an operation. You can set a column's allowEditing property to false if its data should not be edited.

App.js
  • import React from 'react';
  •  
  • import 'devextreme/dist/css/dx.light.css';
  •  
  • import TreeList, {
  • Editing,
  • Column
  • } from 'devextreme-react/tree-list';
  •  
  • class App extends React.Component {
  • render() {
  • return (
  • <TreeList ... >
  • <Editing
  • allowUpdating={true}
  • allowAdding={true}
  • allowDeleting={true}
  • />
  • <Column dataField="id" allowEditing={false} />
  • </TreeList>
  • );
  • }
  • }
  • export default App;

With the TreeList you can edit data in several modes. Use the editing.mode property to specify it.

App.js
  • import React from 'react';
  •  
  • import 'devextreme/dist/css/dx.light.css';
  •  
  • import TreeList, {
  • Editing
  • } from 'devextreme-react/tree-list';
  •  
  • class App extends React.Component {
  • render() {
  • return (
  • <TreeList ... >
  • <Editing ...
  • mode="row" /> {/* 'batch' | 'cell' | 'form' | 'popup' */}
  • </TreeList>
  • );
  • }
  • }
  • export default App;

The following sections detail the TreeList's appearance and behavior in each editing mode.

Row Mode

In this mode a user edits data row by row. When a user clicks an "Edit" button, the corresponding row enters the editing state, and the "Save" and "Cancel" buttons appear in the edit column. The UI component saves changes only if the "Save" button is clicked. If a user starts editing another row, sorts or filters data, it discards the changes.

DevExtreme HTML5 JavaScript jQuery Angular Knockout UI component TreeList Editing Row Mode Editing State

Clicking the "Delete" button invokes the confirmation dialog that allows a user to cancel row deletion. Use the confirmDelete property to hide this dialog.

App.js
  • import React from 'react';
  •  
  • import { TreeList, Editing } from 'devextreme-react/tree-list';
  •  
  • class App extends React.Component {
  • render() {
  • return (
  • <TreeList ... >
  • <Editing
  • mode="row"
  • confirmDelete={false}
  • allowDeleting={true}
  • />
  • </TreeList>
  • );
  • }
  • }
  • export default App;

View Demo

See Also

Cell Mode

In the cell mode, a user edits data cell by cell. Changes are saved once a cell loses the focus, or discarded if a user presses Esc. An added row is saved only when the focus is shifted from it. Choose this mode if any changes should be saved to the data source immediately.

DevExtreme HTML5 JavaScript jQuery Angular Knockout UI component TreeList Editing Cell Mode

If you validate data, a cell is locked in the editing state until a user enters a valid value or clicks the "Cancel Changes" button.

DevExtreme HTML5 JavaScript jQuery Angular Knockout UI component TreeList Editing Cell Mode Validation

Clicking the "Delete" button invokes the confirmation dialog that allows a user to cancel row deletion. Use the confirmDelete property to hide this dialog.

App.js
  • import React from 'react';
  •  
  • import { TreeList, Editing } from 'devextreme-react/tree-list';
  •  
  • class App extends React.Component {
  • render() {
  • return (
  • <TreeList ... >
  • <Editing
  • mode="row"
  • confirmDelete={false}
  • allowDeleting={true}
  • />
  • </TreeList>
  • );
  • }
  • }
  • export default App;

View Demo

See Also

Batch Mode

In the batch mode, like in the cell mode, a user edits data cell by cell. However, in this mode the UI component stores changes in a buffer until a user clicks the global "Save" button instead of saving them immediately. This mode is useful when you need to defer saving multiple changes.

DevExtreme HTML5 JavaScript jQuery Angular Knockout UI component TreeList Editing Batch Mode

When a user clicks a "Delete" button in a row, the UI component only marks the row as deleted. Users can restore this row by clicking the "Undelete" button if they have not saved the changes yet. They can also discard all unsaved changes by clicking the "Revert" button.

DevExtreme HTML5 JavaScript jQuery Angular Knockout UI component TreeList Editing Batch Mode Deletion

View Demo

See Also

Form Mode

In form edit mode, row cells become editable form fields. Form fields are sorted like columns in the columns array.

DevExtreme HTML5 JavaScript jQuery Angular Knockout UI component TreeList Editing Form Mode

Configure the form using the editing.form object. The TreeList uses the DevExtreme Form UI component, so you can specify any Form properties in this object except those listed in its description.

For example, you can specify the items array to reorder editable fields (or "simple items", as they are called in the Form UI component), or organize them in groups and tabs.

In the following code, the items with the specified dataField are simple items. Identical dataFields connect a simple item with a column:

App.js
  • import React from 'react';
  • import 'devextreme/dist/css/dx.light.css';
  •  
  • import TreeList, {
  • Editing,
  • Form,
  • Column
  • } from 'devextreme-react/tree-list';
  •  
  • import {
  • SimpleItem,
  • GroupItem
  • } from 'devextreme-react/form';
  •  
  • import 'devextreme-react/text-area';
  •  
  • class App extends React.Component {
  • render() {
  • return (
  • <TreeList ... >
  • <Editing
  • allowUpdating={true}
  • mode="form">
  • <Form>
  • <GroupItem caption="Personal Data">
  • <SimpleItem dataField="Prefix" />
  • <SimpleItem dataField="Full_Name" />
  • <SimpleItem dataField="Position" />
  • <SimpleItem dataField="Duties" editorType="dxTextArea" />
  • </GroupItem>
  • <GroupItem caption="Contacts">
  • <SimpleItem dataField="Email" />
  • <SimpleItem dataField="Skype" />
  • </GroupItem>
  • </Form>
  • </Editing>
  • <Column dataField="Full_Name" />
  • <Column dataField="Prefix" />
  • <Column dataField="Position" />
  • <Column dataField="Duties" />
  • <Column dataField="Email" />
  • <Column dataField="Skype" />
  • </TreeList>
  • );
  • }
  • }
  • export default App;
See Also

You can use a column's formItem object to customize an individual simple item. See an example in its description.

If you need to validate form values, specify validation rules as described in the Data Validation article.

View Demo

Popup Mode

The popup mode is the form mode with the form placed in a popup window.

DevExtreme HTML5 JavaScript jQuery Angular Knockout UI component TreeList Editing Popup Mode

Use the editing.popup object to customize the popup window. This object can contain the Popup UI component's fields. However, you should refer to the popup object's description for information on restrictions that apply.

App.js
  • import React from 'react';
  •  
  • import 'devextreme/dist/css/dx.light.css';
  •  
  • import TreeList, {
  • Editing,
  • Form,
  • Popup
  • } from 'devextreme-react/tree-list';
  •  
  • class App extends React.Component {
  • render() {
  • return (
  • <TreeList ... >
  • <Editing
  • allowUpdating={true}
  • mode="popup">
  • <Form
  • labelLocation="top"
  • />
  • <Popup
  • showTitle={true}
  • title="Row in the editing state"
  • />
  • </Editing>
  • </TreeList>
  • );
  • }
  • }
  • export default App;

Since the popup and form modes are very similar, you can use the same columns.formItem and editing.form properties to customize items and layout in both modes. See the Form Mode topic for more details on form customization.

View Demo

See Also

API

NOTE
Editing does not work with hierarchical data sources out of the box, but you can use the code sample from this KB to implement it.

Add

Use the addRow() method to add an empty row.

App.js
  • import React from 'react';
  •  
  • import 'devextreme/dist/css/dx.light.css';
  •  
  • import TreeList from 'devextreme-react/tree-list';
  •  
  • class App extends React.Component {
  • constructor(props) {
  • super(props);
  • this.treeListRef = React.createRef();
  • this.addNewRow = this.addNewRow.bind(this);
  • }
  •  
  • addNewRow() {
  • this.treeListRef.current.instance.addRow();
  • }
  •  
  • render() {
  • return (
  • <TreeList ...
  • ref={this.treeListRef}>
  • </TreeList>
  • );
  • }
  • }
  • export default App;

You can specify initial values for a newly added row in the onInitNewRow event handler.

App.js
  • import React from 'react';
  •  
  • import 'devextreme/dist/css/dx.common.css';
  • import 'devextreme/dist/css/dx.light.css';
  •  
  • import TreeList, {
  • Column
  • } from 'devextreme-react/tree-list';
  •  
  • class App extends React.Component {
  • setHireDate(e) {
  • e.data.Hire_Date = new Date();
  • }
  •  
  • render() {
  • return (
  • <TreeList ...
  • onInitNewRow={this.setHireDate}>
  • <Column dataField="Hire_Date" dataType="date" />
  • </TreeList>
  • );
  • }
  • }
  • export default App;
See Also

Update

The cellValue (rowIndex, visibleColumnIndex, value) method updates a cell's value. This cell can be located using its row and column indexes. If the cell's data field is known, you can pass it instead of the column index. After a value is updated, save it to the data source by calling the saveEditData() method.

App.js
  • import React from 'react';
  •  
  • import 'devextreme/dist/css/dx.common.css';
  • import 'devextreme/dist/css/dx.light.css';
  •  
  • import TreeList from 'devextreme-react/tree-list';
  • import Button from 'devextreme-react/button';
  •  
  • class App extends React.Component {
  • constructor(props) {
  • super(props);
  • this.treeListRef = React.createRef();
  • this.updateCell = this.updateCell.bind(this);
  • }
  •  
  • get treeList() {
  • return this.treeListRef.current.instance;
  • }
  •  
  • updateCell() {
  • this.treeList.cellValue(1, "Position", "CTO");
  • this.treeList.saveEditData();
  • }
  •  
  • render() {
  • return (
  • <React.Fragment>
  • <TreeList ...
  • ref={this.treeListRef}>
  • </TreeList>
  • <Button
  • text="Update Cell"
  • onClick={this.updateCell}
  • />
  • </React.Fragment>
  • );
  • }
  • }
  • export default App;

The TreeList UI component allows you to process an updated cell value in the columns.setCellValue function before this value is saved to the data source. Refer to the function's description for an example.

Call the hasEditData() to check if there are any unsaved changes. You can save or cancel them using the saveEditData() or cancelEditData() method, respectively.

App.js
  • import React from 'react';
  •  
  • import 'devextreme/dist/css/dx.common.css';
  • import 'devextreme/dist/css/dx.light.css';
  •  
  • import TreeList from 'devextreme-react/tree-list';
  • import Button from 'devextreme-react/button';
  •  
  • class App extends React.Component {
  • constructor(props) {
  • super(props);
  • this.treeListRef = React.createRef();
  • this.saveChanges = this.saveChanges.bind(this);
  • }
  •  
  • get treeList() {
  • return this.treeListRef.current.instance;
  • }
  •  
  • saveChanges() {
  • if(this.treeList.hasEditData()) {
  • this.treeList.saveEditData().then(() => {
  • if(!this.treeList.hasEditData()) {
  • // Saved successfully
  • } else {
  • // Saving failed
  • }
  • });
  • }
  • }
  •  
  • render() {
  • return (
  • <React.Fragment>
  • <TreeList ...
  • ref={this.treeListRef}>
  • </TreeList>
  • <Button
  • text="Save changes"
  • onClick={this.saveChanges}
  • />
  • </React.Fragment>
  • );
  • }
  • }
  • export default App;
See Also

Delete

Call the deleteRow(rowIndex) method to delete a specific row from the data source. This method invokes a confirmation dialog that allows a user to cancel deletion. The confirmDelete property allows you to hide this dialog instantly deleting the selected row from the data source.

App.js
  • import React from 'react';
  •  
  • import { TreeList, Editing } from 'devextreme-react/tree-list';
  • import Button from 'devextreme-react/button';
  •  
  • class App extends React.Component {
  • constructor(props) {
  • super(props);
  •  
  • this.treeListRef = React.createRef();
  • this.deleteRow = this.deleteRow.bind(this);
  • }
  • get treeList() {
  • return this.treeListRef.current.instance;
  • }
  •  
  • deleteRow() {
  • this.treeList.deleteRow(1);
  • }
  •  
  • render() {
  • return (
  • <TreeList
  • ref={this.treeListRef}>
  • <Editing
  • mode="row"
  • confirmDelete={false}
  • allowDeleting={true}
  • />
  • </TreeList>
  • <Button
  • text="Delete Row"
  • onClick={this.deleteRow}
  • />
  • );
  • }
  • }
  • export default App;

Note that in the batch mode a row is only marked as deleted. To save changes, call the saveEditData() method. Calling the undeleteRow(rowIndex) method cancels row deletion.


jQuery
JavaScript
  • $("#treeListContainer").dxTreeList("undeleteRow", 1);
Angular
TypeScript
  • import { DxTreeListModule } from "devextreme-angular";
  • // ...
  • export class AppComponent {
  • @ViewChild(DxTreeListComponent, { static: false }) treeList: DxTreeListComponent;
  • // Prior to Angular 8
  • // @ViewChild(DxTreeListComponent) treeList: DxTreeListComponent;
  • undeleteRow () {
  • this.treeList.instance.undeleteRow(1);
  • }
  • }
  • @NgModule({
  • imports: [
  • // ...
  • DxTreeListModule
  • ],
  • // ...
  • })
Vue
App.vue
  • <template>
  • <DxTreeList
  • ref="treeList">
  • </DxTreeList>
  • </template>
  •  
  • <script>
  • import { DxTreeList } from 'devextreme-vue/tree-list';
  •  
  • export default {
  • components: {
  • DxTreeList
  • },
  • methods: {
  • undeleteRow() {
  • this.$refs.treeList.instance.undeleteRow(1);
  • }
  • },
  • data() {}
  • }
  • </script>
React
App.js
  • import React from 'react';
  •  
  • import { TreeList } from 'devextreme-react/tree-list';
  •  
  • class App extends React.Component {
  • constructor(props) {
  • super(props);
  •  
  • this.treeListRef = React.createRef();
  • this.undeleteRow = this.undeleteRow.bind(this);
  • }
  • get treeList() {
  • return this.treeListRef.current.instance;
  • }
  •  
  • undeleteRow() {
  • this.treeList.undeleteRow(1);
  • }
  •  
  • render() {
  • return (
  • <TreeList
  • ref={this.treeListRef}
  • />
  • );
  • }
  • }

export default App;

See Also

Events

The TreeList UI component raises events before and after a row is inserted, updated or removed from the data source. If the event handlers are going to remain unchanged during the UI component's lifetime, assign them to corresponding onEventName properties:

App.js
  • import React from 'react';
  •  
  • import 'devextreme/dist/css/dx.common.css';
  • import 'devextreme/dist/css/dx.light.css';
  •  
  • import TreeList from 'devextreme-react/tree-list';
  •  
  • class App extends React.Component {
  • onRowInserting(e) {
  • // Handler of the "rowInserting" event
  • }
  •  
  • render() {
  • return (
  • <TreeList ...
  • onRowInserting={this.onRowInserting}>
  • </TreeList>
  • );
  • }
  • }
  • export default App;

If you are going to change the event handlers at runtime, or if you need to attach several handlers to a single event, subscribe to this event using the on(eventName, eventHandler) method. This approach is more typical of jQuery.

JavaScript
  • var rowUpdatingEventHandler1 = function(e) {
  • // First handler of the "rowUpdating" event
  • };
  •  
  • var rowUpdatingEventHandler2 = function(e) {
  • // Second handler of the "rowUpdating" event
  • };
  •  
  • $("#treeListContainer").dxTreeList("instance")
  • .on("rowUpdating", rowUpdatingEventHandler1)
  • .on("rowUpdating", rowUpdatingEventHandler2);

In addition, the TreeList raises the initNewRow event when a new row is added and the editingStart event when a row enters the editing state. These events can be handled just like others - using the onEventName property or the on(eventName, eventHandler) method. In the following example, the onInitNewRow event handler specifies initial values for an added row:

App.js
  • import React from 'react';
  •  
  • import 'devextreme/dist/css/dx.common.css';
  • import 'devextreme/dist/css/dx.light.css';
  •  
  • import TreeList from 'devextreme-react/tree-list';
  •  
  • class App extends React.Component {
  • onInitNewRow(e) { // Handler of the "initNewRow" event
  • // Sets an initial value for the "Hire_Date" field
  • e.data.Hire_Date = new Date();
  • }
  •  
  • render() {
  • return (
  • <TreeList ...
  • onInitNewRow={this.onInitNewRow}>
  • </TreeList>
  • );
  • }
  • }
  • export default App;
See Also

Customize Editors

The columns's dataType defines a cell's editor that can be configured using the editorOptions object. However, this object cannot be used to change the editor's type or onValueChanged event handler. Instead, use the onEditorPreparing function as shown in the following code. The function's parameter provides the editorName and editorOptions fields for changing the used editor and its configuration.

App.js
  • import React from 'react';
  •  
  • import 'devextreme/dist/css/dx.common.css';
  • import 'devextreme/dist/css/dx.light.css';
  •  
  • import TreeList, {
  • Column
  • } from 'devextreme-react/tree-list';
  • import 'devextreme-react/text-area';
  •  
  • class App extends React.Component {
  • textAreaOptions = { height: 200 };
  • onEditorPreparing(e) {
  • if(e.dataField == "Note" && e.parentType === "dataRow") {
  • const defaultValueChangeHandler = e.editorOptions.onValueChanged;
  • e.editorName = "dxTextArea"; // Change the editor's type
  • e.editorOptions.onValueChanged = function (args) { // Override the default handler
  • // ...
  • // Custom commands go here
  • // ...
  • // If you want to modify the editor value, call the setValue function:
  • // e.setValue(newValue);
  • // Otherwise, call the default handler:
  • defaultValueChangeHandler(args);
  • }
  • }
  • }
  •  
  • render() {
  • return (
  • <TreeList ...
  • onEditorPreparing={this.onEditorPreparing}>
  • <Column
  • dataField="Note"
  • editorOptions={this.textAreaOptions}
  • />
  • </TreeList>
  • );
  • }
  • }
  • export default App;

Implement the column's editCellTemplate for more extensive customization. In this template, you should specify your custom component's appearance and behavior in full. The following code uses the template to substitute the Switch UI component for a default editor. This configuration may be useful in batch editing mode.

App.js
  • import React from 'react';
  •  
  • import 'devextreme/dist/css/dx.common.css';
  • import 'devextreme/dist/css/dx.light.css';
  •  
  • import TreeList, {
  • Column,
  • Editing
  • } from 'devextreme-react/tree-list';
  •  
  • import Switch from 'devextreme-react/switch';
  •  
  • class App extends React.Component {
  • renderSwitch(cellInfo) {
  • const setEditedValue = valueChangedEventArg => {
  • cellInfo.setValue(valueChangedEventArg.value);
  • }
  • return (
  • <Switch
  • width={50}
  • switchedOnText="YES"
  • switchedOffText="NO"
  • defaultValue={cellInfo.value}
  • onValueChanged={setEditedValue}
  • />
  • )
  • }
  •  
  • render() {
  • return (
  • <TreeList ... >
  • <Column
  • dataField="isChecked"
  • editCellRender={this.renderSwitch}
  • />
  • <Editing
  • mode="batch"
  • allowUpdating={true}
  • />
  • </TreeList>
  • );
  • }
  • }
  • export default App;

Editors are displayed in cells in the normal state too if you set the columns.showEditorAlways property to true.

App.js
  • import React from 'react';
  •  
  • import 'devextreme/dist/css/dx.common.css';
  • import 'devextreme/dist/css/dx.light.css';
  •  
  • import TreeList, {
  • Column
  • } from 'devextreme-react/tree-list';
  •  
  • class App extends React.Component {
  • render() {
  • return (
  • <TreeList ... >
  • <Column
  • dataField="Hidden"
  • dataType="boolean"
  • showEditorAlways={true}
  • />
  • </TreeList>
  • );
  • }
  • }
  • export default App;
See Also

Data Validation

User input is validated against a set of validation rules. You can configure them in a column's validationRules array. Validation rules are attached to the editors and do not depend on the current edit mode.

App.js
  • import React from 'react';
  •  
  • import 'devextreme/dist/css/dx.common.css';
  • import 'devextreme/dist/css/dx.light.css';
  •  
  • import TreeList, {
  • Column,
  • RequiredRule,
  • StringLengthRule
  • } from 'devextreme-react/tree-list';
  •  
  • class App extends React.Component {
  • render() {
  • return (
  • <TreeList ... >
  • <Column dataField="Full_Name">
  • <RequiredRule />
  • </Column>
  • <Column dataField="Login">
  • <StringLengthRule
  • min={3}
  • message="Login should be at least 3 symbols long"
  • />
  • </Column>
  • </TreeList>
  • );
  • }
  • }
  • export default App;

The onRowValidating handler allows you to perform an action before a notification that a validation rule has been broken is displayed. For instance, you can perform additional checks in this handler and change the validation result by changing the handler parameter's isValid field.

App.js
  • import React from 'react';
  •  
  • import 'devextreme/dist/css/dx.common.css';
  • import 'devextreme/dist/css/dx.light.css';
  •  
  • import TreeList from 'devextreme-react/tree-list';
  •  
  • class App extends React.Component {
  • denyAdminLogin(e) {
  • if(e.isValid && e.newData.Login === "Administrator") {
  • e.isValid = false;
  • e.errorText = "You cannot log in as Administrator";
  • }
  • }
  •  
  • render() {
  • return (
  • <TreeList ...
  • onRowValidating={this.denyAdminLogin}>
  • </TreeList>
  • );
  • }
  • }
  • export default App;
See Also