我有一个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 )
感谢任何帮助,非常感谢!
答案 0 :(得分:54)
我认为你正在寻找
var displayedData = $("#YourGrid").data().kendoGrid.dataSource.view()
然后将其字符串化如下:
var displayedDataAsJSON = JSON.stringify(displayedData);
希望这有帮助!
答案 1 :(得分:19)
如果您想获取过滤数据的所有页面,可以使用:
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;
确保在尝试应用过滤器之前检查过滤器是否存在,否则剑道会抱怨。
答案 2 :(得分:10)
获取网格中所有行的计数
$('#YourGridName').data("kendoGrid").dataSource.total()
获取特定的行项目
$('#YourGridName').data("kendoGrid").dataSource.data()[1]
获取网格中的所有行
$('#YourGridName').data("kendoGrid").dataSource.data()
Json到网格中的所有行
JSON.stringify($('#YourGridName').data("kendoGrid").dataSource.data())
答案 3 :(得分: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;
}
}
答案 4 :(得分:1)
对于JSON部分,有一个辅助函数来提取JSON格式的数据,这可以提供帮助:
var displayedData = $("#YourGrid").data().kendoGrid.dataSource.view().toJSON()
编辑:由于kendo网格行为导致上述方法出现一些错误后,我发现这篇文章解决了这个问题: Kendo DataSource view not always return observablearray