This is a migrated thread and some comments may be shown as answers.

How to reset datasource in Grid

1 Answer 2132 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Chenkai
Top achievements
Rank 1
Chenkai asked on 22 May 2014, 03:33 PM
I really don't know How to reset datasouce  with Grid. I searched some materials, and some info said use setDataSource() method. It didn't work. I have no idea. please help me.
<button onclick="LoadPerson1()">Load User 1</button>
<button onclick="LoadPerson2()">Load User 2</button>
<div id="grid"></div>
 
<script>
    var personModel
    var grid;
    $(function () {
        personModel = {
            user: {
                ObjectId: "",
                PrincipalName: "",
                Userinfos: []
            }
        };
 
        grid = $("#grid").kendoGrid({
            toolbar: [
                { name: "create" },
                { name: "save" },
                { name: "cancel" }
            ],
            columns: [
                { field: "Key" },
                { field: "Value" }
            ],
            dataSource: personModel.user.Userinfos,
            editable: true
        }).data("kendoGrid");
    });
 
 
    function LoadPerson1() {
        $.ajax({
            cache: false,
            type: "post",
            dataType: "json",
            url: "Home/GetUser",
            success: function (data) {
                personModel.user.ObjectId = data.ObjectId;
                personModel.user.PrincipalName = data.PrincipalName;
                personModel.user.Userinfos = data.Userinfos;
 
                                                     //I really want to reset datasouce here, thanks
                grid.dataSource.data(personModel.user.Userinfos);
                grid.dataSource.read();
            }
        });
    }
 
    function LoadPerson2() {
        $.ajax({
            cache: false,
            type: "post",
            dataType: "json",
            url: "Home/GetUser2",
            success: function (data) {
                personModel.user.ObjectId = data.ObjectId;
                personModel.user.PrincipalName = data.PrincipalName;
                personModel.user.Userinfos = data.Userinfos;
                 
            }
        });
    }
</script>

above sentences are my all code. The pic I attach is data in personModel.user.UserInfos

1 Answer, 1 is accepted

Sort by
0
Dimiter Madjarov
Telerik team
answered on 22 May 2014, 04:00 PM
Hello Chenkai,


The setDataSource method of the Grid expects a dataSource object as a parameter. You could find an example included in the documentation.

The data method of the dataSource accepts an array as a parameter, so it is suitable for the current case. Calling the read method is not needed. Here is a small example, which demonstrates the approach.

Regards,
Dimiter Madjarov
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
Tags
Grid
Asked by
Chenkai
Top achievements
Rank 1
Answers by
Dimiter Madjarov
Telerik team
Share this question
or