How would I update the grid data to the Web API when i Add / Updata /Delete the grid data?

How would I update the grid data to the Web API when I Add / Update /Delete the grid data?

I am using the Dialog Template grid for my project. The data are locally updated when I Add / Update /Delete the grid data. I need to send the changed data to web API for Remote API Update.


This the whole code of the Grid.

index.js

ej.base.enableRipple(true);
$('#content').html('<img id="loader-img" alt="" src="loading.gif" width="30" height="30" align="center" />');

//var countryData = ej.data.DataUtil.distinct(orderData, 'deviceTypeID', true);
//var shipCityData = ej.data.DataUtil.distinct(orderData, 'ShipCity', true);
(function () {
    $.ajax({
        type: 'GET',
        contentType: "application/json",
        dataType: "json",
        url: "https://secsystemeventsapi.azurewebsites.net/api/Devices"
    }).done(function (data) {
    $('#content').html('<style ="visibility:hidden;"></style>');
        window.gridData = data;
        var index = 0;
        while (index < window.gridData.length)
        {gridData
            window.gridData[index].deviceID = window.gridData[index].deviceID.toString();
            window.gridData[index].deviceTypeID = window.gridData[index].deviceTypeID.toString();
            window.gridData[index].fwid = window.gridData[index].fwid.toString();
            index++;
        }
        grid.dataSource = window.gridData;

    });
})(); 
var grid = new ej.grids.Grid({
dataSource: window.gridData,
editSettings: { allowEditing: true, allowAdding: true, allowDeleting: true, mode: 'Dialog', template: '#dialogtemplate' },
toolbar: ['Add', 'Edit', 'Delete'],
columns: [
{
field: 'deviceID', isPrimaryKey: true, headerText: 'Device ID', textAlign: 'Center',
validationRules: { required: true, number: true }, width: 100, defaultValue: ''
},
{
field: 'deviceTypeID', headerText: 'Device Type ID', textAlign: 'Center',
validationRules: { required: true, number: true }, editType: 'dropdownedit', width: 100, defaultValue: ''
},
{
field: 'serialNO', headerText: 'Serial No',
validationRules: { required: true }, textAlign: 'Center', width: 100, defaultValue: ''
},
{
field: 'version', headerText: 'Version', textAlign: 'Center',
width: 100, defaultValue: ''},
{
field: 'brandName', headerText: 'Brand Name', textAlign: 'Center',
width: 100, defaultValue: ''
},
{
field: 'fwid', headerText: 'FWID', textAlign: 'Center', width: 100, defaultValue: ''
}
],



actionComplete: function(args) {
if ((args.requestType === 'beginEdit' || args.requestType === 'add')) {
var data = args.rowData;
var countryData =[{name:'Gateway',id:'1'},
{name:'Accelometer',id:'2'},
{name:'PanicButton',id:'3'},
{name:'Motion',id:'4'},
{name:'Smoke',id:'5'},
{name:'GlassBreak',id:'6'},
{name:'DoorContact',id:'7'}];

                // Convert Widget for the ShipCountry field
                new ej.dropdowns.DropDownList({value: data.deviceTypeID, popupHeight: '300px', floatLabelType: 'Always',
                dataSource: countryData, fields: {text: 'name', value: 'id'}, placeholder: 'Device Type ID'},
                args.form.elements.namedItem('deviceTypeID'));

                if (ej.base.Browser.isDevice) {
                args.dialog.height = window.innerHeight - 90 + 'px';
                args.dialog.dataBind();
                }

                // Set initail Focus
                if (args.requestType === 'beginEdit') {
                args.form.elements.namedItem('serialNO').focus();
                } else {
                args.form.elements.namedItem('deviceID').focus();
                }
            }
        }
    });
grid.appendTo('#Grid');





 Thank You.

3 Replies

MS Manivel Sellamuthu Syncfusion Team May 1, 2020 05:49 AM UTC

Hi Arshad, 

Greetings from Syncfusion support. 

From the shared code we could see that you are getting the data from the webApi with ajax post and binding the resulting data to the Grid. So it will act as local data. To handle WebApi service already we do have a Data manager support webApi Adaptor. Please refer the below documentation and sample for more information. 

While you binding the webapi data to the Grid, the response object should contain properties, Items and Count, whose values are a collection of entities and total count of the entities, respectively. While performing CRUD actions we can get the modified data in the server side through network requests with PUT, POST, DELETE  Request types. 



Please let us know, if you need further assistance. 

Regards, 
Manivel 



AR Arshad May 2, 2020 12:53 PM UTC

Hi Thanks for your reply,

How to post or update or delete using web API adapter in javascript on grid.


MS Manivel Sellamuthu Syncfusion Team May 4, 2020 09:40 AM UTC

Hi Arshad, 

Thanks for your update. 

For your convenience we have prepared a sample for webApi Adaptor with crud actions. Please find the below code example and sample for more information. 

While binding webApi adator to the Grid, While performing CRUD actions The dataManager will make the server side requests through network requests with PUT, POST, DELETE  Request types. Please refer the below screenshots for more information. 

While adding a record 

 

While deleting 
 
 
 
While updating: 
 
 
Based on the request we need to handle perform the CRUD operations in the data source and return the data. 

 
    var data = new ej.data.DataManager({ 
    url: '/api/Order', 
    adaptor: new ej.data.WebApiAdaptor() 
}); 
 
var grid = new ej.grids.Grid({ 
    dataSource: data, 
. . . 
    height: 273 
}); 
    grid.appendTo('#Grid'); 
 
[OrderController] 
namespace EJ2Grid.Controllers 
{ 
    [Produces("application/json")] 
    [Route("api/Order")] 
    public class OrderController : Controller 
    { 
        // GET: api/Orders 
        [HttpGet] 
 
        public object Get() 
        { 
            var queryString = Request.Query; 
 
            int skip = Convert.ToInt32(queryString["$skip"]); 
            int take = Convert.ToInt32(queryString["$top"]); 
            var data = OrdersDetails.GetAllRecords().ToList(); 
            return take != 0 ? new { Items = data.Skip(skip).Take(take).ToList(), Count = data.Count() } : new { Items = data, Count = data.Count() }; 
 
        } 
 
        // GET: api/Orders/5 
        [HttpGet("{id}", Name = "Get")] 
        public string Get(int id) 
        { 
            return "value"; 
        } 
 
        // POST: api/Orders 
        [HttpPost] 
// for insert action 
        public object Post([FromBody]OrdersDetails value) 
        { 
            OrdersDetails.GetAllRecords().Insert(0, value); 
            return value; 
        } 
 
 
 
        // PUT: api/Orders/5 
        [HttpPut] 
// for update action 
        public object Put(int id, [FromBody]OrdersDetails value) 
        { 
            var ord = value; 
            OrdersDetails val = OrdersDetails.GetAllRecords().Where(or => or.OrderID == ord.OrderID).FirstOrDefault(); 
            val.OrderID = ord.OrderID; 
            val.EmployeeID = ord.EmployeeID; 
            val.CustomerID = ord.CustomerID; 
            val.Freight = ord.Freight; 
            val.OrderDate = ord.OrderDate; 
            val.ShipCity = ord.ShipCity; 
            return value; 
        } 
 
        // DELETE: api/ApiWithActions/5 
        [HttpDelete("{id:int}")] 
        [Route("Orders/{id:int}")] 
// for delete action 
        public object Delete(int id) 
        { 
            OrdersDetails.GetAllRecords().Remove(OrdersDetails.GetAllRecords().Where(or => or.OrderID == id).FirstOrDefault()); 
            return Json(id); 
        } 
 
        public class Data 
        { 
 
            public bool requiresCounts { get; set; } 
            public int skip { get; set; } 
            public int take { get; set; } 
        } 
    } 
} 



Please let us know, if you need further assistance. 

Regards, 
Manivel 


Loader.
Up arrow icon