Change dynamically data in datasource

6 posts, 1 answers
  1. Pierre
    Pierre avatar
    212 posts
    Member since:
    Apr 2007

    Posted 14 Jun 2013 Link to this post

    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
  2. Answer
    Alexander Valchev
    Admin
    Alexander Valchev avatar
    2875 posts

    Posted 18 Jun 2013 Link to this post

    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!
  3. Kendo UI is VS 2017 Ready
  4. Pierre
    Pierre avatar
    212 posts
    Member since:
    Apr 2007

    Posted 20 Jun 2013 Link to this post

    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.
  5. Pierre
    Pierre avatar
    212 posts
    Member since:
    Apr 2007

    Posted 20 Jun 2013 Link to this post

    Forgot my question! All is working with your code and no need to be global.
    Thanks
  6. Jared
    Jared avatar
    2 posts
    Member since:
    Nov 2015

    Posted 06 Jul in reply to Alexander Valchev Link to this post

    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!

  7. Jared
    Jared avatar
    2 posts
    Member since:
    Nov 2015

    Posted 06 Jul in reply to Jared Link to this post

    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

Back to Top
Kendo UI is VS 2017 Ready