Remote Data in ASP.NET MVC Tree Grid Component
18 Nov 20189 minutes to read
To bind remote data to TreeGrid component, assign service data as an instance of DataManager to the DataSource property. To interact with remote data source, provide the endpoint url and define the HasChildMapping property of treegrid.
The HasChildMapping property maps the field name in data source, that denotes whether current record holds any child records. This is useful internally to show expand icon while binding child data on demand.
The TreeGrid provides Load on Demand support for rendering remote data. The Load on demand is considered in TreeGrid for the following actions.
- Expanding root nodes.
- Navigating pages, with paging enabled in TreeGrid.
When load on demand is enabled, all the root nodes are rendered in collapsed state at initial load.
When load on demand support is enabled in TreeGrid with paging, the current or active page’s root node alone will be rendered in collapsed state. On expanding the root node, the child nodes will be loaded from the remote server.
When a root node is expanded, its child nodes are rendered and are cached locally, such that on consecutive expand/collapse actions on root node, the child nodes are loaded from the cache instead from the remote server.
Similarly, if the user navigates to a new page, the root nodes of that specific page, will be rendered with request to the remote server.
NOTE
Remote Data Binding supports only Self-Referential Data and by default the
pageSizeModefor Remote Data isRootmode. i.e only root node’s count will be shown in pager while using Remote Data
Service code snippet:
namespace Controllers
{
[Produces("application/json")]
[Route("api/SelfReferenceData")]
public class SelfReferenceDataController : Controller
{
public static List<SelfReferenceData> tree = new List<SelfReferenceData>();
// GET: api/SelfReferenceData
[HttpGet]
public object Get()
{
var queryString = Request.Query;
if (tree.Count == 0)
tree = SelfReferenceData.GetTree();
//Filtering
if (queryString.Keys.Contains("$filter") && !queryString.Keys.Contains("$top"))
{
StringValues filter;
queryString.TryGetValue("$filter", out filter);
int fltr = Int32.Parse(filter[0].ToString().Split("eq")[1]);
IQueryable<SelfReferenceData> data1 = tree.Where(f => f.ParentItem == fltr).AsQueryable();
return new { result = data1.ToList(), count = data1.Count() };
}
List<SelfReferenceData> data = tree.ToList();
if (queryString.Keys.Contains("$select"))
{
data = (from ord in tree
select new SelfReferenceData
{
ParentItem = ord.ParentItem
}
).ToList();
return data;
}
data = data.Where(p => p.ParentItem == null).ToList();
int count = data.Count;
//Paging
if (queryString.Keys.Contains("$inlinecount"))
{
StringValues Skip;
StringValues Take;
int skip = (queryString.TryGetValue("$skip", out Skip)) ? Convert.ToInt32(Skip[0]) : 0;
int top = (queryString.TryGetValue("$top", out Take)) ? Convert.ToInt32(Take[0]) : data.Count();
return new { result = tree.Skip(skip).Take(top), count = tree.Count };
}
else
{
return SelfReferenceData.GetTree();
}
}NOTE
By default, DataManager uses ODataAdaptor for remote data-binding.
Based on the RESTful web services, set the corresponding adaptor to DataManager. Referherefor more details.
Filtering and searching server-side data operations are not supported in load on demand
LoadChildOnDemand
While binding remote data to Tree Grid component, by default Tree Grid renders parent rows in collapsed state. Tree Grid provides option to load the child records also during the initial rendering itself for remote data binding by setting LoadChildOnDemand as false.
When LoadChildOnDemand is enabled parent records are rendered in collapsed state.
The following code example describes the behavior of the LoadChildOnDemand feature of Tree Grid.
NOTE
Also while using LoadChildOnDemand we need to handle the child records on server end and it is applicable for CRUD operations also.
Offline mode
On remote data binding, all treegrid actions such as paging, loading child on-demand, will be processed on server-side. To avoid postback, set the treegrid to load all data on initialization and make the actions process in client-side. To enable this behavior, use the offline property of DataManager.
Custom adaptor
You can create your own adaptor by extending the built-in adaptors. The following demonstrates custom adaptor approach and how to add a serial number for the records by overriding the built-in response processing using the processResponse method of the ODataAdaptor.
Sending additional parameters to the server
To add a custom parameter to the data request, use the addParams method of Query property. Assign the Query object with additional parameters to the treegrid Query property.
Handling HTTP error
During server interaction from the treegrid, some server-side exceptions may occur, and you can acquire those error messages or exception details
in client-side using the ActionFailure event.
The argument passed to the ActionFailure event contains the error details returned from the server.
NOTE
The
ActionFailureevent will be triggered not only for the server errors, but also when there is an exception while processing the treegrid actions.
You can refer to ourASP.NET MVC Tree Gridfeature tour page for its groundbreaking feature representations. You can also explore ourASP.NET MVC Tree Grid exampleto knows how to present and manipulate data.
The
ActionFailureevent will be triggered not only for the server errors, but also when there is an exception while processing the treegrid actions.
Load on demand with virtualization
While binding remote data to Tree Grid component, by default Tree Grid renders parent rows in collapsed state. When expanding the root node, the child nodes will be loaded from the remote server.
When using virtualization with remote data binding, it helps you to improve the tree grid performance while loading a large set of data by setting EnableVirtualization as true. The Tree Grid UI virtualization allows it to render only rows and columns visible within the view-port without buffering the entire datasource.
The HasChildMapping property maps the field name in data source, that denotes whether current record holds any child records. This is useful internally to show expand icon while binding child data on demand.
Load parent rows in expanded state with virtualization
Tree Grid provides an option to load the child records in the initial rendering itself for remote data binding by setting the LoadChildOnDemand as true. When the LoadChildOnDemand is enabled, parent records are rendered in expanded state.
When using virtualization with LoadChildOnDemand , it helps you to improve the tree grid performance while loading the child records during the initial rendering for remote data binding by setting EnableVirtualization as true and LoadChildOnDemand as true.
You can refer to our
ASP.NET MVC Tree Gridfeature tour page for its groundbreaking feature representations. You can also explore ourASP.NET MVC Tree Grid exampleto knows how to present and manipulate data.