This is a migrated thread and some comments may be shown as answers.

Return metadata with Grid data

3 Answers 325 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Jesse
Top achievements
Rank 1
Veteran
Jesse asked on 07 Nov 2017, 05:30 PM

I'm building a Grid that lists a collection of data that comes from multiple sources. One of those sources is a web service so it's possible that the web service call might fail. If it does, I still want to display the data from the other sources, but I want to display a warning on the page that the web service data is not included in the results. I have a ViewModel containing this property:

public class MyVM
{
    public bool ServiceUnavailable { get; set; }
    public List<RowVM> GridData { get; set; }
}

 

So I could show the warning when the Grid first loads. However, I'm not actually going to populate GridData during the initial page load. I want the Grid to make an AJAX call to load the first page (and for any other paging.) So after each Read action completes, I need to check if ServiceUnavailable and display the warning if it is true. The problem is, the Read action only returns a List<RowVM>.ToDataSourceResult(). How can I return additional data that is separate from the Grid row data? Is there a way to override the client-side Read function to make my own AJAX call and then on success update the datasource with the returned data myself?

 

 

 

3 Answers, 1 is accepted

Sort by
0
Stefan
Telerik team
answered on 09 Nov 2017, 11:21 AM
Hello, Jesse,

The ToDataSourceResult method is making an object which has an error property which can be used to pass the information that the second service call is failed:



Then the request can be handled in the requestEnd event and based on the value of the error property to show the warning:

https://docs.telerik.com/kendo-ui/api/javascript/data/datasource#events-requestEnd

I hope this is helpful.

Regards,
Stefan
Progress Telerik
Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
0
Jesse
Top achievements
Rank 1
Veteran
answered on 09 Nov 2017, 04:06 PM

Thanks Stefan, but when I set Errors, the Grid fires the Error event and does not display the data. I still want to display the data but also add a warning to the page. 

I see here that the DataSourceResult can be sub-classed to add my own properties. This works, but isn't a great option because it requires a copy constructor to copy the public properties of the DataSourceResult to sub class. Luckily, it looks like those public properties are all that is required from the DataSourceResult, but a future Kendo MVC release could cause it to break.

Is this the only option? What if I make my own AJAX request to retrieve a response that includes a bunch of my own data and a DataSourceResult. Can I set that DataSourceResult on the Grid programmatically or is the Grid required to initiate the request?

0
Accepted
Konstantin Dikov
Telerik team
answered on 13 Nov 2017, 11:17 AM
Hello,

The approach suggested in the forum thread is the one that I would recommend, because it is very unlikely that the structure of the DataSourceResult will be changed, because this will cause a major breaking change in existing applications. 

As for the custom AJAX call, it is possible to retrieve the data manually (a simple array) and set it to the Grid`s DataSource through its "data" method:
Hope this helps.


Regards,
Konstantin Dikov
Progress Telerik
Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Tags
Grid
Asked by
Jesse
Top achievements
Rank 1
Veteran
Answers by
Stefan
Telerik team
Jesse
Top achievements
Rank 1
Veteran
Konstantin Dikov
Telerik team
Share this question
or