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

Grid DataSource read() does not work if data is assigned to a new object

2 Answers 735 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Quotient
Top achievements
Rank 1
Quotient asked on 26 Dec 2013, 06:30 PM
Our grid data comes as a part of a larger object returned by ajax call. Other properties of this object are used outside of the grid. Thus instead of dataSource.transport we use dataSource.data.
To let you better understand the following code snippets, I'll mention that the grid DataSource is defined inside AngularJS controller. 
$scope.ticket = {"Comments":[]};
$scope.commentsDS = new kendo.data.DataSource({
  data:$scope.ticket.Comments,
  schema:{model:{fields:{
    txt: {type:"string"},
    by: {type:"string"},
    on: {type:"string"}
  }}},
  pageSize:10
});
When the grid originally loads, it has no rows as the ticket.Comments array is empty.
Now, the Comments data comes in the ajax response returned by "Tickets" service:
if ($scope.objId > 0) {
  Tickets.ticketPromise($scope.objId).then(function(response){
    if (response.data) {
      $scope.ticket = response.data;
      $scope.commentsDS.read();
    }
  });
}
However the grid does not show any records after this. I can step through the code and verify, that $scope.ticket gets an array of Comments before the read() is called.
I found a workaround by using a different data array and then updating elements of this array. 
$scope.tmp = [{}];
$scope.ticket = {"Comments":$scope.tmp};
$scope.comments = new kendo.data.DataSource({
    data: $scope.tmp,
    schema: { ... },
    pageSize: 20
});
if ($scope.objId > 0) {
    Tickets.ticketPromise($scope.objId).then(function(response){
        if (response.data) {
            $scope.ticket = response.data;
            for (var i = 0; i < $scope.ticket.Comments.length; i++){
                $scope.tmp[i]=$scope.ticket.Comments[i];
            }
            $scope.comments.read();
        }
    });
}
I had to use this extra loop to assign tmp elements because one liner "$scope.tmp = $scope.ticket.Comments;" does not work!
Is there a better way???

Thanks, 
-Pavel

2 Answers, 1 is accepted

Sort by
0
Vladimir Iliev
Telerik team
answered on 30 Dec 2013, 10:25 AM
Hi,

Basically current behavior is expected as the "data" option is intended to be used only for passing data before initialization of the grid. In current case I would suggest to use different approach for loading the grid with data after it's initialization - please check the example below:

$scope.commentsDS = new kendo.data.DataSource({
    transport: {
        read: function (request) {
            var data = $scope.ticket.Comments;
            request.success(data);
        }
    },
    schema: {
        model: {
            fields: {
                txt: {
                    type: "string"
                },
                by: {
                    type: "string"
                },
                on: {
                    type: "string"
                }
            }
        }
    },
    pageSize: 10
});


Regards,
Vladimir Iliev
Telerik
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Quotient
Top achievements
Rank 1
answered on 31 Dec 2013, 09:13 PM
Volodya, 
Thanks a lot for the response! It works nicely. 
It would be great to add to the docs the notion that "data" should be used only to "pre-populate" the grid. And this form of the read function in DataSource configuration.  
Happy NY! :)
-Pavel
Tags
Grid
Asked by
Quotient
Top achievements
Rank 1
Answers by
Vladimir Iliev
Telerik team
Quotient
Top achievements
Rank 1
Share this question
or