When updating the datasource of a grid the schema parse doesn't work

1 Answer 444 Views
Grid
Lee
Top achievements
Rank 2
Bronze
Bronze
Bronze
Lee asked on 24 Mar 2022, 09:50 PM | edited on 24 Mar 2022, 10:30 PM

I have a grid that is initiated with a local datasource. I have a schema and a parse that manipulates the data received. I need to replace the datasource but don't want to retype the entire schema over again. How can I do that? I have an example dojo here. In my example, I am adding 100 to the age. Notice that after reloading the new records don't have the 100 added to the age. I need to know how to do this for the entire grid and also for updating a single record in the grid. I tried pushUpdate for a single record and it ignores the parse also. 

http://dojo.telerik.com/OxazIqAG

Here is the relevant code:
<script>
  $("#grid").kendoGrid({
    columns: [
      { field: "name" },
      { field: "age" },
      { field: "id" }
    ],
 
    dataSource: {
      data: [
        { id: 1, name: "Jane Doe", age: 30 },
        { id: 2, name: "John Doe", age: 33 }
      ],
      schema: {
        parse: function(response) {
            var users = [];
            for (var i = 0; i < response.length; i++) {
                let record = response[i];
                var user = {
                    id: record.id,
                    name: record.name,
                    age: record.age + 100
                };
                users.push(user);
            }
            return users;
        },
        model: {
         id: "id",
          fields: {
           id: { type: "number" } 
          }
        }
      }
    }
  });
    
    $("#setData").click(function(){
      $("#grid").data("kendoGrid")
      			.dataSource
      			.data([{id: 3, name: "new item", age: 20}]);
    });
  </script>

1 Answer, 1 is accepted

Sort by
0
Georgi Denchev
Telerik team
answered on 29 Mar 2022, 09:38 AM

Hello, Lee,

Thank you for the provided Dojo.

The schema.parse configuration is used to parse the initial dataSource data only. You could retrieve the function from the dataSource and re-use it whenever you need to:

const parse = $("#grid").data("kendoGrid").dataSource.options.schema.parse;
      
      $("#grid").data("kendoGrid")
      			.dataSource
      			.data(parse([{id: 3, name: "new item", age: 20}]));

Let me know if you have any questions.

Best Regards,
Georgi Denchev
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Lee
Top achievements
Rank 2
Bronze
Bronze
Bronze
commented on 30 Mar 2022, 06:16 PM | edited

Thanks. This helped out a lot. I did have to edit your response to use the below code instead of dataSource.data.
dataSource.pushUpdate(parse([{id: 3, name: "new item", age:20}]));
The way you have it it replaced the existing records with this new one. 
Georgi Denchev
Telerik team
commented on 04 Apr 2022, 09:18 AM

Hi, Lee,

You are correct, the approach that you demonstrated is the appropriate one if you wish to update a record instead of replacing all of the current data.

Best Regards,

Georgi

Tags
Grid
Asked by
Lee
Top achievements
Rank 2
Bronze
Bronze
Bronze
Answers by
Georgi Denchev
Telerik team
Share this question
or