Search code examples
javascriptjquerykendo-uikendo-datasource

How to get part of JSON for KendoDataSource


I need to fill a KendoListView from a external server and I need to use just part of the response I am reading for a Java Servlet

       var srcListView = new kendo.data.DataSource({
            transport: {
            read: {
                dataType: "json",
                url: "MainServlet",
                data:{event:"Test"},
            },
        },
    });

And the response is:

{
  "status": "ok",
  "response": {
    "trucks": [
      "A6U-905",
      "AHF-888",
      "AHP-779",
      "buzz"
    ]
  }
}

I need just the trucks array for kendoListView, I am using Kendo-ui, How I can do this?


Solution

  • As an alternative to what @DontVoteMeDown said about using schema.parse you can use a simpler solution for cases like this where you don't need to process the result but just return what is in an element. This solution is define schema.data as the name of the field containing the data. In your example:

    new kendo.data.DataSource({
        transport: {
            read: {
                    dataType: "json",
                    url: "MainServlet",
                    data:{event:"Test"},
            }
        },
        schema: {
            data: "response.trucks"
        }
    });
    

    I recommend using schema.parse when you need to do some transformation on the data received and schema.data when is simply returning some sub-element of the received JSON.