DevExtreme v24.2 is now available.

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

Your search did not match any results.

JavaScript/jQuery Data Grid - CRUD Operations

This demo shows how to implement remote CRUD operations in the CustomStore. You can view the server implementation under the DataGridWebApiController tab in the ASP.NET MVC version of this demo. The requests sent to the server are displayed under the JavaScript DataGrid.

After a cell is edited, the JavaScript DataGrid can behave differently depending on the selected refresh mode: reload data from the server (the refreshMode is full), reapply data processing operations (reshape), or merely rerender the changed cells (repaint).

Backend API
$(() => { const URL = 'https://js.devexpress.com/Demos/Mvc/api/DataGridWebApi'; const ordersStore = new DevExpress.data.CustomStore({ key: 'OrderID', load() { return sendRequest(`${URL}/Orders`); }, insert(values) { return sendRequest(`${URL}/InsertOrder`, 'POST', { values: JSON.stringify(values), }); }, update(key, values) { return sendRequest(`${URL}/UpdateOrder`, 'PUT', { key, values: JSON.stringify(values), }); }, remove(key) { return sendRequest(`${URL}/DeleteOrder`, 'DELETE', { key, }); }, }); const dataGrid = $('#grid').dxDataGrid({ dataSource: ordersStore, repaintChangesOnly: true, showBorders: true, editing: { refreshMode: 'reshape', mode: 'cell', allowAdding: true, allowUpdating: true, allowDeleting: true, }, scrolling: { mode: 'virtual', }, columns: [{ dataField: 'CustomerID', caption: 'Customer', lookup: { dataSource: { paginate: true, store: new DevExpress.data.CustomStore({ key: 'Value', loadMode: 'raw', load() { return sendRequest(`${URL}/CustomersLookup`); }, }), }, valueExpr: 'Value', displayExpr: 'Text', }, }, { dataField: 'OrderDate', dataType: 'date', }, { dataField: 'Freight', }, { dataField: 'ShipCountry', }, { dataField: 'ShipVia', caption: 'Shipping Company', dataType: 'number', lookup: { dataSource: new DevExpress.data.CustomStore({ key: 'Value', loadMode: 'raw', load() { return sendRequest(`${URL}/ShippersLookup`); }, }), valueExpr: 'Value', displayExpr: 'Text', }, }, ], summary: { totalItems: [{ column: 'CustomerID', summaryType: 'count', }, { column: 'Freight', valueFormat: '#0.00', summaryType: 'sum', }], }, }).dxDataGrid('instance'); $('#refresh-mode').dxSelectBox({ items: ['full', 'reshape', 'repaint'], value: 'reshape', inputAttr: { 'aria-label': 'Refresh Mode' }, onValueChanged(e) { dataGrid.option('editing.refreshMode', e.value); }, }); $('#clear').dxButton({ text: 'Clear', onClick() { $('#requests ul').empty(); }, }); function sendRequest(url, method = 'GET', data) { const d = $.Deferred(); logRequest(method, url, data); $.ajax(url, { method, data, cache: false, xhrFields: { withCredentials: true }, }).done((result) => { d.resolve(method === 'GET' ? result.data : result); }).fail((xhr) => { d.reject(xhr.responseJSON ? xhr.responseJSON.Message : xhr.statusText); }); return d.promise(); } function logRequest(method, url, data) { const args = Object.keys(data || {}).map((key) => `${key}=${data[key]}`).join(' '); const logList = $('#requests ul'); const time = DevExpress.localization.formatDate(new Date(), 'HH:mm:ss'); const newItem = $('<li>').text([time, method, url.slice(URL.length), args].join(' ')); logList.prepend(newItem); } });
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" lang="en"> <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=5.0" /> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <script>window.jQuery || document.write(decodeURIComponent('%3Cscript src="js/jquery.min.js"%3E%3C/script%3E'))</script> <link rel="stylesheet" type="text/css" href="https://cdn3.devexpress.com/jslib/24.2.3/css/dx.light.css" /> <script src="js/dx.all.js"></script> <link rel="stylesheet" type="text/css" href="styles.css" /> <script src="index.js"></script> </head> <body class="dx-viewport"> <div class="demo-container"> <div id="grid"></div> <div class="options"> <div class="caption">Options</div> <div class="option"> <span>Refresh Mode:</span> <div id="refresh-mode"></div> </div> <div id="requests"> <div> <div class="caption">Network Requests</div> <div id="clear"></div> </div> <ul></ul> </div> </div> </div> </body> </html>
#grid { height: 440px; } .options { padding: 20px; margin-top: 20px; background-color: rgba(191, 191, 191, 0.15); } .caption { margin-bottom: 10px; font-weight: 500; font-size: 18px; } .option { margin-bottom: 10px; } .option > span { position: relative; top: 2px; margin-right: 10px; } .option > .dx-widget { display: inline-block; vertical-align: middle; } #requests .caption { float: left; padding-top: 7px; } #requests > div { padding-bottom: 5px; } #requests > div::after { content: ""; display: table; clear: both; } #requests #clear { float: right; } #requests ul { list-style: none; max-height: 100px; overflow: auto; margin: 0; } #requests ul li { padding: 7px 0; border-bottom: 1px solid #ddd; } #requests ul li:last-child { border-bottom: none; }
using DevExtreme.AspNet.Data; using DevExtreme.AspNet.Mvc; using Newtonsoft.Json; using System; using System.Linq; using System.Net; using System.Net.Http; using System.Net.Http.Formatting; using System.Web.Http; using DevExtreme.MVC.Demos.Models.Northwind; using DevExtreme.MVC.Demos.Models.DataGrid; using System.Collections.Generic; namespace DevExtreme.MVC.Demos.Controllers { [Route("api/DataGridWebApi/{action}", Name = "DataGridWebApi")] public class DataGridWebApiController : ApiController { InMemoryNorthwindContext _nwind = new InMemoryNorthwindContext(); [HttpGet] public HttpResponseMessage Orders(DataSourceLoadOptions loadOptions) { return Request.CreateResponse(DataSourceLoader.Load(_nwind.Orders, loadOptions)); } [HttpPost] public HttpResponseMessage InsertOrder(FormDataCollection form) { var values = form.Get("values"); var newOrder = new Order(); JsonConvert.PopulateObject(values, newOrder); Validate(newOrder); if(!ModelState.IsValid) return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState.GetFullErrorMessage()); _nwind.Orders.Add(newOrder); _nwind.SaveChanges(); return Request.CreateResponse(HttpStatusCode.Created, newOrder); } [HttpPut] public HttpResponseMessage UpdateOrder(FormDataCollection form) { var key = Convert.ToInt32(form.Get("key")); var values = form.Get("values"); var order = _nwind.Orders.First(o => o.OrderID == key); JsonConvert.PopulateObject(values, order); Validate(order); if(!ModelState.IsValid) return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState.GetFullErrorMessage()); _nwind.SaveChanges(); return Request.CreateResponse(HttpStatusCode.OK, order); } [HttpDelete] public void DeleteOrder(FormDataCollection form) { var key = Convert.ToInt32(form.Get("key")); var order = _nwind.Orders.First(o => o.OrderID == key); _nwind.Orders.Remove(order); _nwind.SaveChanges(); } // additional actions [HttpGet] public HttpResponseMessage OrderDetails(int orderID, DataSourceLoadOptions loadOptions) { return Request.CreateResponse(DataSourceLoader.Load( from i in _nwind.Order_Details where i.OrderID == orderID select new { Product = i.Product.ProductName, Price = i.UnitPrice, i.Quantity, Sum = i.UnitPrice * i.Quantity }, loadOptions )); } [HttpGet] public HttpResponseMessage ShippersLookup(DataSourceLoadOptions loadOptions) { var lookup = from i in _nwind.Shippers orderby i.CompanyName select new { Value = i.ShipperID, Text = i.CompanyName }; return Request.CreateResponse(DataSourceLoader.Load(lookup, loadOptions)); } [HttpGet] public HttpResponseMessage CustomersLookup(DataSourceLoadOptions loadOptions) { var lookup = from i in _nwind.Customers let text = i.CompanyName + " (" + i.Country + ")" orderby i.CompanyName select new { Value = i.CustomerID, Text = text }; return Request.CreateResponse(DataSourceLoader.Load(lookup, loadOptions)); } [HttpPost] public HttpResponseMessage Batch(List<DataChange> changes) { foreach(var change in changes) { Order order; if(change.Type == "update" || change.Type == "remove") { var key = Convert.ToInt32(change.Key); order = _nwind.Orders.First(o => o.OrderID == key); } else { order = new Order(); } if(change.Type == "insert" || change.Type == "update") { JsonConvert.PopulateObject(change.Data.ToString(), order); Validate(order); if(!ModelState.IsValid) return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState.GetFullErrorMessage()); if(change.Type == "insert") { _nwind.Orders.Add(order); } change.Data = order; } else if(change.Type == "remove") { _nwind.Orders.Remove(order); } } _nwind.SaveChanges(); return Request.CreateResponse(HttpStatusCode.OK, changes); } } }
using System; using System.Collections.Generic; using System.Data.Entity; namespace DevExtreme.MVC.Demos.Models.Northwind { public class InMemoryNorthwindContext : InMemoryDataContext<Order> { readonly NorthwindContext _nwind = new NorthwindContext(); public DbSet<Customer> Customers => _nwind.Customers; public DbSet<Order_Detail> Order_Details => _nwind.Order_Details; public ICollection<Order> Orders => ItemsInternal; public DbSet<Shipper> Shippers => _nwind.Shippers; protected override IEnumerable<Order> Source => _nwind.Orders; protected override int GetKey(Order item) => item.OrderID; protected override void SetKey(Order item, int key) => item.OrderID = key; } }