Return metadata with Grid data

4 posts, 1 answers
  1. Jesse
    Jesse avatar
    54 posts
    Member since:
    Aug 2013

    Posted 07 Nov 2017 Link to this post

    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?

     

     

     

  2. Stefan
    Admin
    Stefan avatar
    2913 posts

    Posted 09 Nov 2017 Link to this post

    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.
  3. Jesse
    Jesse avatar
    54 posts
    Member since:
    Aug 2013

    Posted 09 Nov 2017 in reply to Stefan Link to this post

    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?

  4. Answer
    Konstantin Dikov
    Admin
    Konstantin Dikov avatar
    2466 posts

    Posted 13 Nov 2017 Link to this post

    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.
Back to Top