0
votes

I am getting kendo grid data from backend and setting it to kendo grid options alignedProcessesToRiskGridOptions now data is displaying in the grid , but i also wanted to get raw data to write some logic, How can i get data from dataSource or directly calling RiskService AngularJs factory and assign it to var gridData?

ctrl.js

   $scope.alignedProcessesToRiskGridOptions = RiskService.alignedProcessToRiskGrid();
    $scope.alignedProcessesToRiskGridOptions.dataSource = RiskService.getAlignedProcessesToRiskGridDataSource($stateParams.riskId);
    gridData = $scope.alignedProcessesToRiskGridOptions.dataSource.data();
    console.log('RISK DATA', gridData);

factory.js

getAlignedProcessesToRiskGridDataSource: function(riskKey) {
        var countNew = 0;
        return new kendo.data.DataSource({
                    type: 'json',
                    serverPaging: true,
                    serverSorting: true,
                    serverFiltering: true,
                    transport: {
                        read: function(options) {
                            var gridSearchObject = {
                                skip: options.data.skip,
                                take: options.data.take,
                                pageSize: options.data.pageSize,
                                page: options.data.page,
                                sorting: options.data.sort,
                                filter: options.data.filter
                            };
                            return $http.post(
                                'app/risk/rest/allAlignedProcessesToRisk/' + riskKey, gridSearchObject).success(
                                function(data) {
                                    countNew = data.totalCount;
                                    options.success(data.resultDTOList);
                                });
                        }

                    },
1
What is written to the console for console.log('RISK DATA', gridData);? - whipdancer
it returns the function but no data - hussain
Does it write RISK DATA undefined? Does it show gridData as an object? Does it write an empty string? - whipdancer
gridData as an object - hussain

1 Answers

2
votes

According to the kendo documentation, there are at least a couple ways to get at the actual data from a datasource.

There is the view method

Returns the data items which correspond to the current page, filter, sort and group configuration. Compare with the data method, which will return data items from all pages, if local data binding and paging are used.

There is the data method

Gets or sets the data items of the data source.

If the data source is bound to a remote service (via the transport option) the data method will return the service response. Every item from the response is wrapped in a kendo.data.ObservableObject or kendo.data.Model (if the schema.model option is set).

If the data source is bound to a JavaScript array (via the data option) the data method will return the items of that array. Every item from the array is wrapped in a kendo.data.ObservableObject or kendo.data.Model (if the schema.model option is set).

Since you are using the data method, you should be able to access the data via the properties and methods of the resulting object.

You should be able to check the length of gridData via console.log(gridData.length), as well checking any individual object within the data via an array index console.log(gridData[0]). Further you should be able to check properties of the objects via console.log(gridData[0].firstProperty).

If none of these work, then it is likely because the datasource is not being checked with the context of query or fetch. If you check out this example, you will see that the data is being evaluated inside of the fetch function.

You can find more information about the Kendo DataSource in their online documentation.

Lastly, you can see an example of the different ways I use the Kendo datasource in this question about the kendo grid.