DevExtreme v23.1 is now available.

Explore our newest features/capabilities and share your thoughts with us.

Your search did not match any results.
Data Grid

Custom Editors

Different editors can be used to edit cell values in grid columns. The default editor depends on the column configuration. The dependency is illustrated in the editorOptions object's description (this object is used to customize the default editor). In this demo, the SelectBox component is the Status column's default editor, and the editorOptions object is used to specify the component's itemTemplate.

If the default editor is unsuitable, you can replace it with a custom editor. For this, implement an editCellTemplate that allows you to configure the replacement editor's appearance and behavior. To change the cell value and, optionally, the displayed value after the editor's value is changed, use the setValue() method of the editCellTemplate. In this demo, the default editors in the Owner and Assignees columns are replaced with the DropDownBox and TagBox components.

Backend API
Copy to CodeSandBox
Apply
Reset
import React from 'react'; import DataGrid, { Paging, HeaderFilter, SearchPanel, Editing, Column, Lookup, RequiredRule, } from 'devextreme-react/data-grid'; import { createStore } from 'devextreme-aspnet-data-nojquery'; import SelectBox from 'devextreme-react/select-box'; import { statuses } from './data.js'; import EmployeeDropDownBoxComponent from './EmployeeDropDownBoxComponent.js'; import EmployeeTagBoxComponent from './EmployeeTagBoxComponent.js'; const url = 'https://js.devexpress.com/Demos/Mvc/api/CustomEditors'; const statusLabel = { 'aria-label': 'Status' }; const employees = createStore({ key: 'ID', loadUrl: `${url}/Employees`, onBeforeSend(method, ajaxOptions) { ajaxOptions.xhrFields = { withCredentials: true }; }, }); const tasks = createStore({ key: 'ID', loadUrl: `${url}/Tasks`, updateUrl: `${url}/UpdateTask`, insertUrl: `${url}/InsertTask`, onBeforeSend(method, ajaxOptions) { ajaxOptions.xhrFields = { withCredentials: true }; }, }); class App extends React.Component { constructor(props) { super(props); this.statusEditorRender = this.statusEditorRender.bind(this); } cellTemplate(container, options) { const noBreakSpace = '\u00A0'; const text = (options.value || []).map((element) => options.column.lookup.calculateCellValue(element)).join(', '); container.textContent = text || noBreakSpace; container.title = text; } calculateFilterExpression(filterValue, selectedFilterOperation, target) { if (target === 'search' && typeof (filterValue) === 'string') { return [this.dataField, 'contains', filterValue]; } return function(data) { return (data.AssignedEmployee || []).indexOf(filterValue) !== -1; }; } onValueChanged(cell, e) { cell.setValue(e.value); } statusEditorRender(cell) { const onValueChanged = this.onValueChanged.bind(this, cell); return <SelectBox defaultValue={cell.value} {...cell.column.lookup} onValueChanged={onValueChanged} inputAttr={statusLabel} itemRender={this.itemRender} />; } itemRender(data) { const imageSource = `images/icons/status-${data.id}.svg`; if (data != null) { return <div> <img src={imageSource} className="status-icon middle"></img> <span className="middle">{data.name}</span> </div>; } return <span>(All)</span>; } onRowInserted(e) { e.component.navigateToRow(e.key); } render() { return ( <div> <DataGrid dataSource={tasks} showBorders={true} onRowInserted={this.onRowInserted} > <Paging enabled={true} defaultPageSize={15} /> <HeaderFilter visible={true} /> <SearchPanel visible={true} /> <Editing mode="cell" allowUpdating={true} allowAdding={true} /> <Column dataField="Owner" width={150} allowSorting={false} editCellComponent={EmployeeDropDownBoxComponent} > <Lookup dataSource={employees} displayExpr="FullName" valueExpr="ID" /> <RequiredRule /> </Column> <Column dataField="AssignedEmployee" caption="Assignees" width={200} allowSorting={false} editCellComponent={EmployeeTagBoxComponent} cellTemplate={this.cellTemplate} calculateFilterExpression={this.calculateFilterExpression}> <Lookup dataSource={employees} valueExpr="ID" displayExpr="FullName" /> <RequiredRule /> </Column> <Column dataField="Subject"> <RequiredRule /> </Column> <Column dataField="Status" width={200} editCellRender={this.statusEditorRender} > <Lookup dataSource={statuses} displayExpr="name" valueExpr="id" /> <RequiredRule /> </Column> </DataGrid> </div> ); } } export default App;
import React from 'react'; import ReactDOM from 'react-dom'; import App from './App.js'; ReactDOM.render( <App />, document.getElementById('app'), );
<!DOCTYPE html> <html> <head> <title>DevExtreme Demo</title> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0" /> <link rel="stylesheet" type="text/css" href="https://cdn3.devexpress.com/jslib/23.1.5/css/dx.light.css" /> <link rel="stylesheet" type="text/css" href="styles.css" /> <script src="https://unpkg.com/core-js@2.6.12/client/shim.min.js"></script> <script src="https://unpkg.com/systemjs@0.21.3/dist/system.js"></script> <script type="text/javascript" src="config.js"></script> <script type="text/javascript"> System.import("./index.js"); </script> </head> <body class="dx-viewport"> <div class="demo-container"> <div id="app"></div> </div> </body> </html>
.status-icon { height: 16px; width: 16px; display: inline-block; margin-right: 8px; } .middle { vertical-align: middle; }
import React from 'react'; import DataGrid, { Column, Paging, Scrolling, Selection, } from 'devextreme-react/data-grid'; import DropDownBox from 'devextreme-react/drop-down-box'; const dropDownOptions = { width: 500 }; const ownerLabel = { 'aria-label': 'Owner' }; export default class EmployeeDropDownBoxComponent extends React.Component { constructor(props) { super(props); this.state = { selectedRowKeys: [props.data.value], isDropDownOpened: false, }; this.onSelectionChanged = this.onSelectionChanged.bind(this); this.contentRender = this.contentRender.bind(this); this.boxOptionChanged = this.boxOptionChanged.bind(this); } boxOptionChanged(e) { if (e.name === 'opened') { this.setState({ isDropDownOpened: e.value, }); } } contentRender() { return ( <DataGrid dataSource={this.props.data.column.lookup.dataSource} remoteOperations={true} height={250} selectedRowKeys={this.state.selectedRowKeys} hoverStateEnabled={true} onSelectionChanged={this.onSelectionChanged} focusedRowEnabled={true} defaultFocusedRowKey={this.state.selectedRowKeys[0]} > <Column dataField="FullName" /> <Column dataField="Title" /> <Column dataField="Department" /> <Paging enabled={true} defaultPageSize={10} /> <Scrolling mode="virtual" /> <Selection mode="single" /> </DataGrid> ); } onSelectionChanged(selectionChangedArgs) { this.setState({ selectedRowKeys: selectionChangedArgs.selectedRowKeys, isDropDownOpened: false, }); this.props.data.setValue(this.state.selectedRowKeys[0]); } render() { return ( <DropDownBox onOptionChanged={this.boxOptionChanged} opened={this.state.isDropDownOpened} dropDownOptions={dropDownOptions} dataSource={this.props.data.column.lookup.dataSource} value={this.state.selectedRowKeys[0]} displayExpr="FullName" valueExpr="ID" inputAttr={ownerLabel} contentRender={this.contentRender}> </DropDownBox> ); } }
import React from 'react'; import TagBox from 'devextreme-react/tag-box'; const nameLabel = { 'aria-label': 'Name' }; export default class EmployeeTagBoxComponent extends React.Component { constructor(props) { super(props); this.onValueChanged = this.onValueChanged.bind(this); this.onSelectionChanged = this.onSelectionChanged.bind(this); } onValueChanged(e) { this.props.data.setValue(e.value); } onSelectionChanged() { this.props.data.component.updateDimensions(); } render() { return <TagBox dataSource={this.props.data.column.lookup.dataSource} defaultValue={this.props.data.value} valueExpr="ID" displayExpr="FullName" showSelectionControls={true} maxDisplayedTags={3} inputAttr={nameLabel} showMultiTagOnly={false} applyValueMode="useButtons" searchEnabled={true} onValueChanged={this.onValueChanged} onSelectionChanged={this.onSelectionChanged} />; } }
export const statuses = [{ id: 1, name: 'Not Started', }, { id: 2, name: 'In Progress', }, { id: 3, name: 'Deferred', }, { id: 4, name: 'Need Assistance', }, { id: 5, name: 'Completed', }];
window.config = { transpiler: 'plugin-babel', meta: { 'devextreme/localization.js': { 'esModule': true, }, 'devextreme-aspnet-data-nojquery': { 'esModule': true, }, }, paths: { 'npm:': 'https://unpkg.com/', }, defaultExtension: 'js', map: { 'react': 'npm:react@17.0.2/umd/react.development.js', 'react-dom': 'npm:react-dom@17.0.2/umd/react-dom.development.js', 'prop-types': 'npm:prop-types@15.8.1/prop-types.js', 'devextreme-aspnet-data-nojquery': 'npm:devextreme-aspnet-data-nojquery@2.9.0/index.js', 'rrule': 'npm:rrule@2.6.4/dist/es5/rrule.js', 'luxon': 'npm:luxon@1.28.1/build/global/luxon.min.js', 'es6-object-assign': 'npm:es6-object-assign@1.1.0', 'devextreme': 'npm:devextreme@23.1.5/cjs', 'devextreme-react': 'npm:devextreme-react@23.1.5', 'jszip': 'npm:jszip@3.7.1/dist/jszip.min.js', 'devextreme-quill': 'npm:devextreme-quill@1.6.2/dist/dx-quill.min.js', 'devexpress-diagram': 'npm:devexpress-diagram@2.2.1/dist/dx-diagram.js', 'devexpress-gantt': 'npm:devexpress-gantt@4.1.48/dist/dx-gantt.js', '@devextreme/runtime': 'npm:@devextreme/runtime@3.0.11', 'inferno': 'npm:inferno@7.4.11/dist/inferno.min.js', 'inferno-compat': 'npm:inferno-compat/dist/inferno-compat.min.js', 'inferno-create-element': 'npm:inferno-create-element@7.4.11/dist/inferno-create-element.min.js', 'inferno-dom': 'npm:inferno-dom/dist/inferno-dom.min.js', 'inferno-hydrate': 'npm:inferno-hydrate@7.4.11/dist/inferno-hydrate.min.js', 'inferno-clone-vnode': 'npm:inferno-clone-vnode/dist/inferno-clone-vnode.min.js', 'inferno-create-class': 'npm:inferno-create-class/dist/inferno-create-class.min.js', 'inferno-extras': 'npm:inferno-extras/dist/inferno-extras.min.js', // SystemJS plugins 'plugin-babel': 'npm:systemjs-plugin-babel@0.0.25/plugin-babel.js', 'systemjs-babel-build': 'npm:systemjs-plugin-babel@0.0.25/systemjs-babel-browser.js', // Prettier 'prettier/standalone': 'npm:prettier@2.8.4/standalone.js', 'prettier/parser-html': 'npm:prettier@2.8.4/parser-html.js', }, packages: { 'devextreme': { defaultExtension: 'js', }, 'devextreme-react': { main: 'index.js', }, 'devextreme/events/utils': { main: 'index', }, 'devextreme/events': { main: 'index', }, 'es6-object-assign': { main: './index.js', defaultExtension: 'js', }, }, packageConfigPaths: [ 'npm:@devextreme/*/package.json', 'npm:@devextreme/runtime@3.0.11/inferno/package.json', ], babelOptions: { sourceMaps: false, stage0: true, react: true, }, }; System.config(window.config);