31

我有一个kendoGrid,我想JSON在过滤和排序后摆脱它,我该如何实现?

类似于以下内容,

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 )

非常感谢任何帮助!

4

5 回答 5

61

我想你正在寻找

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

然后将其字符串化如下:

var displayedDataAsJSON = JSON.stringify(displayedData);

希望这可以帮助!

于 2012-04-28T06:44:03.830 回答
22

如果要获取过滤数据的所有页面,可以使用:

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;

确保在尝试应用过滤器之前检查是否存在过滤器,否则 Kendo 会抱怨。

于 2013-08-21T16:32:12.913 回答
12

获取网格中所有行的计数

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

获取特定的行项目

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

获取网格中的所有行

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

Json到网格中的所有行

JSON.stringify($('#YourGridName').data("kendoGrid").dataSource.data())
于 2013-11-09T22:59:54.673 回答
2

像这样,仅显示当前正在查看的数据。还扩展了网格以在整个应用程序中提供这些功能。

 /**
 * 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;
    }
}
于 2014-10-30T14:01:18.903 回答
1

对于 JSON 部分,有一个辅助函数可以提取 JSON 格式的数据,可以提供帮助:

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

编辑:由于剑道网格行为,上述方法出现一些错误后,我发现这篇文章解决了这个问题: Kendo DataSource view not always return observablearray

于 2014-09-29T14:35:23.357 回答