31

I have a kendoGrid and i would like to get the JSON out of it after filtering and sorting how do I achieve this?

something like the following,

var grid = $("#grid").data("kendoGrid");

alert(grid.dataSource.data.json); // I could dig through grid.dataSource.data and I see a function ( .json doen't exist I put it there so you know what i want to achieve )

Thanks any help is greatly appreciated!

sonyisda1
  • 422
  • 1
  • 8
  • 21
  • A fiddle would help, but does this get you anywhere? `console.log( $("#grid").data("kendoGrid").dataSource.options.data );` – Marc Apr 25 '12 at 23:26

5 Answers5

62

I think you're looking for

var displayedData = $("#YourGrid").data().kendoGrid.dataSource.view()

Then stringify it as follows:

var displayedDataAsJSON = JSON.stringify(displayedData);

Hope this helps!

thomaux
  • 19,133
  • 10
  • 76
  • 103
Petur Subev
  • 19,983
  • 3
  • 52
  • 68
  • 1
    perfect!!! Thanks!!! :) in my app after digging through the api looking at your example i used $("#YourGrid").data().kendoGrid.dataSource.data() and $("#YourGrid").data().kendoGrid.dataSource.at(index) :) –  May 01 '12 at 20:14
  • How do you get the datasource (all pages) with the current filters applied? – carter Aug 21 '13 at 16:23
  • You cant, you can just get the number of all the records via $('#YourGrid').data().kendoGrid.dataSource.total(), when there is paging applied the records just for that specific page are fetched. – Petur Subev Aug 22 '13 at 07:44
  • Hi, I am not able to get the displayed data of kendo grid.In my case i have three levels of grid. In second and third level of grid datasource if i assign the data with .toJSON() then the grid datasource not getting updated. If i don't use .toJSON() then i during cancel i am not able to revert the changes back.Can you tell me the solution – Nadendla May 08 '16 at 19:25
22

If you want to get all pages of the filtered data you can use this:

var dataSource = $("#grid").data("kendoGrid").dataSource;
var filters = dataSource.filter();
var allData = dataSource.data();
var query = new kendo.data.Query(allData);
var data = query.filter(filters).data;

Make sure to check if filters exist before trying to apply them or Kendo will complain.

carter
  • 5,074
  • 4
  • 31
  • 40
  • any update on a more efficient way to get this data? When dealing with large data sets (+5k), there is noticeable lag when a query like this runs. I would think that since the view is already calculated for the current page that it should be readily available for all pages – TabsNotSpaces Jun 27 '17 at 19:10
  • 1
    Nice, this totally just worked for me. I was just pulling the dataSource and wondering why when I viewed my record in the grid it only showed 1 record, but when I viewed that same record from my dataSource it was showing 5. This got it to work! Thank you sir! – IamBatman Sep 27 '17 at 23:11
  • Glad it helped. – carter Sep 28 '17 at 18:17
12

To get count of all rows in grid

$('#YourGridName').data("kendoGrid").dataSource.total()

To get specific row items

$('#YourGridName').data("kendoGrid").dataSource.data()[1]

To get all rows in grid

$('#YourGridName').data("kendoGrid").dataSource.data()

Json to all rows in grid

JSON.stringify($('#YourGridName').data("kendoGrid").dataSource.data())
Mahib
  • 3,977
  • 5
  • 53
  • 62
2

For the JSON part, there's a helper function to extract the data in JSON format that can help:

var displayedData = $("#YourGrid").data().kendoGrid.dataSource.view().toJSON()

EDIT: after some errors with the above method due to kendo grid behavior, I found this article that solves the problem: Kendo DataSource view not always return observablearray

AkerbeltZ
  • 569
  • 1
  • 8
  • 13
2

Something like this, to display only data that is being viewed at the moment. Also extended the grid to provide these functions all over the app.

 /**
 * Extends kendo grid to return current displayed data
 * on a 2-dimensional array
 */
var KendoGrid = window.kendo.ui.Grid;
KendoGrid.fn.getDisplayedData = function(){
    var items = this.items();
    var displayedData = new Array();
    $.each(items,function(key, value) {
        var dataItem = new Array();
        $(value).find('td').each (function() {
            var td = $(this);
            if(!td.is(':visible')){
                //element isn't visible, don't show
                return;//continues to next element, that is next td
            }
            if(td.children().length == 0){
                //if no children get text
                dataItem.push(td.text());
            } else{
                //if children, find leaf child, where its text is the td content
                var leafElement = innerMost($(this));
                dataItem.push(leafElement.text());
            }
        }); 
        displayedData.push(dataItem);
    });
    return displayedData;
};

KendoGrid.fn.getDisplayedColumns = function(){
    var grid = this.element;
    var displayedColumns = new Array();
    $(grid).find('th').each(function(){
        var th = $(this);
        if(!th.is(':visible')){
            //element isn't visible, don't show
            return;//continues to next element, that is next th
        }
        //column is either k-link or plain text like <th>Column</th>
        //so we extract text using this if:
        var kLink = th.find(".k-link")[0];
        if(kLink){
            displayedColumns.push(kLink.text);
        } else{
            displayedColumns.push(th.text());
        }

    });
    return displayedColumns;
};

/**
 * Finds the leaf node of an HTML structure
 */
function innerMost( root ) {
    var $children = $( root ).children();

    while ( true ) {
        var $temp = $children.children();
        if($temp.length > 0) $children = $temp;
        else return $children;
    }
}
Alex Arvanitidis
  • 4,403
  • 6
  • 26
  • 36