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

后端 未结 5 1911
北海茫月
北海茫月 2020-12-28 13:45

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 followi

相关标签:
5条回答
  • 2020-12-28 14:04

    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!

    0 讨论(0)
  • 2020-12-28 14:10

    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

    0 讨论(0)
  • 2020-12-28 14:12

    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;
        }
    }
    
    0 讨论(0)
  • 2020-12-28 14:17

    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.

    0 讨论(0)
  • 2020-12-28 14:18

    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())
    
    0 讨论(0)
提交回复
热议问题