DataManger uses adaptors to process data. There are three types of adaptors in the DataManger. They are
JSON Adaptor
URL Adaptor
OData Adaptor
Here, you can learn when and how each adaptor is used.
JSON Adaptor
JSONAdaptor is used to process JSON data. It contains methods to process the given JSON data based on the queries. The following code example illustrates on how to use the JSONAdaptor.
JSONAdaptor has the following unique in-built methods,
Properties
Parameters
Description
processQuery(dataManagerObj, query)
dataManagerObj
Object
ej.DataManager object
query
ej.Query
Sets the default query for the data source
Used to prepare query string for the request data
processResponse(data, dataObj, query, xhr)
data
Object
JSON data or JSON array
dataObj
Object
ej.DataManager object
query
ej.Query
Sets the default query for the data source
xhr
Object
XMLHTTPRequest object
Used to precess the response which is return from the Data Source
The result of above code example is illustrated as follows.
JSON adaptor
URL Adaptor
URL Adaptor of the DataManager can be used when you are required to use remote service to retrieve data. It interacts with server-side for all DataManager Queries and CRUD operations. Now, in the following code example, the data is retrieved from the MVCController.
UrlAdaptor has the following unique in-built methods,
The result of the above code example is illustrated as follows.
URL Adaptor
OData Adaptor
OData Adaptor that is extended from URL Adaptor is used for consuming data through OData Service. You can use the following code example to use OData adaptor.
ODataAdaptor has the following unique in-built methods,
publicclassEmployeeController:ApiController{staticreadonlyIEmployeeRepositoryrepository=newEmployeeRepository();// GET API/<controller> [HttpGet]publicobjectGet(){vardata=repository.GetAll().ToList();returnnew{Items=data,Count=data.Count()};}}
Result of the above code example is illustrated as follows.
Web API Adaptor
RemoteSave Adaptor
RemoteSaveAdaptor extended from the JsonAdaptor of theDataManager is used for binding local data and performs all DataManager queries in client-side. It interacts with server-side only for CRUD operations to pass the modified records. Refer to the following code example.
RemoteSaveAdaptor has the following unique in-built methods,
Result of the above code example is illustrated as follows.
RemoteSave Adaptor
Custom Adaptor
Custom Adaptor is a key technique to customize adaptors in the DataManager. It is useful to write own adaptor. Normally ej.Adaptor is base class for all adaptors. Therefore, first inherit the ej.Adaptor to develop the customized one and then override functionality in Custom Adaptor with base class. The following code example illustrates you how to create the Custom Adaptor.
@(Html.EJ().DataManager("FlatData”))
@(Html.EJ().Grid<MVCdoc.OrdersView>("FlatGrid")
.DataManagerID("FlatData")
.Query("new ej.Query().select(['OrderID', 'CustomerID', 'EmployeeID', 'ShipCity', 'Freight']).take(5)")
.Columns(col =>
{
col.Field("OrderID").HeaderText("Order ID").IsPrimaryKey(true).TextAlign(TextAlign.Right).Width(75).Add();
col.Field("CustomerID").HeaderText("Customer ID").Width(80).Add();
col.Field("EmployeeID").HeaderText("Employee ID").TextAlign(TextAlign.Right).Width(75).Add();
col.Field("Freight").HeaderText("Freight").TextAlign(TextAlign.Right).Width(75).Format("{0:C}").Add();
col.Field("ShipCity").HeaderText("Ship City").Width(110).Add();
})
)
<scripttype="text/javascript">$(function(){// Document is ready.//new custom adaptor implementation//able to implement more option in custom adaptor other than insertvarcustomAdaptor=newej.Adaptor().extend({insert:function(dataManagerObj,data){returndataManagerObj.dataSource.json.push(data);},processQuery:ej.JsonAdaptor.prototype.processQuery// reused process query from json adaptor});window.FlatData.Adaptor=newcustomAdaptor()window.FlatData.insert({OrderID:10240,CustomerID:"HANAR",EmployeeID:3,ShipCity:"Reims",Freight:"23.4"});window.FlatData.insert({OrderID:10241,CustomerID:"HANAR",EmployeeID:2,ShipCity:"Reims",Freight:"21.4"});window.FlatData.insert({OrderID:10242,CustomerID:"VINET",EmployeeID:5,ShipCity:"Lyon",Freight:"13.4"});varobj=$("#FlatGrid").ejGrid("instance");obj.dataSource(window.FlatData.executeLocal(newej.Query().take(7)));})</script>
Result of above code example is as follows.
Custom adaptor
Using Custom Adaptor, you can override the existing method of Extended Adaptor,
Properties
Description
beforeSend
Custom headers can be set using pre-request callback beforeSend, by using the setRequestHeader method can be used to modify the XMLHTTPRequest
processQuery
Used to prepare query string for the request data
processResponse
Used to precess the response which is return from the Data Source
insert
The insert method of the data manager is used to add a new record to the table
remove
The remove action submits the data items that should be deleted
update
The update method is used to update the modified changes made to a record in the data source of the DataManager.
Cache Adaptor
Cache Adaptor is used to cache the data of the visited pages. It prevents new requests for the previously visited pages. It can be enabled by using the EnableCaching property. You can configure cache page size and duration of caching by using CachingPageSize and TimeTillExpiration properties of the “DataManager”.
CacheAdaptor has the following unique in-built methods,