Change dynamically data in datasource

1 Answer 5483 Views
Grid
Pierre
Top achievements
Rank 2
Iron
Iron
Pierre asked on 14 Jun 2013, 09:44 PM
HI, I create a Grid with a custom local dataSource. I give the grid un model and when initially created and displayed all work good.

If I whant to replace date in the datasource without change de model, i use:
grid.dataSource.data(collTemp);
collTemp containt array of objects in the same way i did at the creation phase.

The grid relaod the data OK, but I lost all my formating information (like the $ on the price columns). When I click on the price for edition I can see the $ sign breifelly appear.

When the user click on the "refresh" button, The initial old data is repopulated.

How I can change the "real" internal data without recreating all the datasource, Model and columns information? 

thanks

1 Answer, 1 is accepted

Sort by
0
Accepted
Alexander Valchev
Telerik team
answered on 18 Jun 2013, 11:33 AM
Hi Pierre,

The data method of the Grid will replace the data, but will not update the internal array which stores original data array. One possible solution to workaround this is to use a custom transport.

For example:
var dataSource = new kendo.data.DataSource({
    transport: {
        read: function(operation) {
            var data = operation.data.data || [];
            operation.success(data);
        }
    },
    schema: {
        model: {
            id: "foo",
            fields: {
                foo: { type: "number" }
            }
        }
    }
});
 
var data1 = [{foo: 1},{foo: 2}];
dataSource.read({ data: data1 });
 
var data2 = [{foo: 10},{foo: 20}];
dataSource.read({ data: data2 });

I hope this will help.

Regards,
Alexander Valchev
Telerik
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
Pierre
Top achievements
Rank 2
Iron
Iron
commented on 20 Jun 2013, 06:51 PM

Thank Alexander,
 I will do that, but if the dataSource is not global. I create the dataSource, then add it to the Grid. When I need to change data, I receive a event with the ID of the grid. Can I get the internal DataSource and do a read() to retreive the data contain again?  Or a need to redo all the "var dataSource = new kendo.data.DataSource" process and then rebind the datasource to the grid?

I other word, wath is the best way to change some information in a DataSource binded to a grid without using MVVM but local variable. I chan change all the dataSource like you did in your code, but I can add/remove item, and change only one item data.
Pierre
Top achievements
Rank 2
Iron
Iron
commented on 20 Jun 2013, 08:13 PM

Forgot my question! All is working with your code and no need to be global.
Thanks
Jared
Top achievements
Rank 1
commented on 06 Jul 2016, 01:08 PM

Im looking for a solution just like the one posted above by @Alexander

When I try the above code, exactly as written in the post, I get this error
"operation.success is not a function"

I also got and error with "dataSource.read({ data: data2 });" so I changed it to
dataSource.transport.read({ data: data2 });

Can you provide help with this. I have the data, I can pass it into the read() function but I dont know how to get the datasource to update. Thanks!

Jared
Top achievements
Rank 1
commented on 06 Jul 2016, 02:57 PM

I figured it out, I wasnt setting my dataSource to new kendo.data.DataSource(){}

with that,  the example you posted above works, thanks! complicated stuff but works greats

Jesse
Top achievements
Rank 1
Veteran
commented on 10 Sep 2018, 02:58 PM

@Alexander Valchev, is it possible to configure the MVC version of the grid with this custom transport?
Jesse
Top achievements
Rank 1
Veteran
commented on 10 Sep 2018, 02:58 PM

@Alexander Valchev, is it possible to configure the MVC version of the grid with this custom transport?
Tsvetina
Telerik team
commented on 12 Sep 2018, 11:56 AM

Hello Jesse,

You can use the ClientHandlerDescriptor class to set the Read setting to a client function in a Custom DataSource:
.DataSource(ds => ds
    .Custom()
    .Batch(true)
    .Schema(schema => schema
        .Model(m =>
        {
            m.Id(f => f.MeetingID);
            m.Field("title", typeof(string)).DefaultValue("No title").From("Title");
            m.Field("start", typeof(DateTime)).From("Start");
            m.Field("description", typeof(string)).From("Description");
        }))
    .Transport(new {
        //the ClientHandlerDescriptor is a special type that allows code rendering as-is (not as a string)
        read = new Kendo.Mvc.ClientHandlerDescriptor() {HandlerName = "customRead" }
    })

<script>
    function customRead(options) {
    .................
    }
</script>

This information, along with other tips for using the Custom DataSource type is available in the docs here:
Common Scenarios

Regards,
Tsvetina
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
Grid
Asked by
Pierre
Top achievements
Rank 2
Iron
Iron
Answers by
Alexander Valchev
Telerik team
Share this question
or