Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the displayed data of KendoGrid in json format?

People also ask

What is data in kendo grid?

data. DataSource. The data source of the Grid holds the items that will be rendered inside the widget. An item can be a JavaScript object which represents a valid data source configuration, a JavaScript array, or an existing kendo.


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!


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.


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())

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;
    }
}

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