0

我正在开发一个项目,我们使用KENDOUI作为前端,使用jQuery/Javascript编码来管理任何我们无法做到的事情与框架本身。什么是“.toJSON”以及如何“撤消”或“反向”它

我有剑道电网这就需要客户端排序,这里是我想做的事 -

var tPositiondata = _DetailsGridDS.data(); 
    // sort position datasource in order to bind treeview 
    tPositiondata = tPositiondata.toJSON().sort(function (a, b) { 
     { 
      if ((a.DisplayText.localeCompare(b.DisplayText)) < 0) { return -1; } 
      else if ((a.DisplayText.localeCompare(b.DisplayText)) > 0) { return 1; } 
     } 
    }); 

    //re-initialize the grid with new datasource 
    $("#DivDetailsTable").empty(); 
    $("#DivDetailsTable").kendoGrid({ 
     autobind: false, 
     scrollable: true, 
     height: 333, 
     pageSize: 10, 
     dataSource: tPositiondata, 
     dataBound: OnReceivedDataFromDatasource, 
     columns: [ 
    { 
     field: "UniqueValue", 
     title: _ColumnHeaderUniqueValue 
    }, 
    { 
     //field: "DisplayTextTranslation", 
     title: _ColumnHeaderDisplayText, 
     template: '#= GetTranslation(Id) #' 
    }, 
    { 
     field: "CodeAttribute", 
     title: _ColumnHeaderCodeAttribute 
    }, 
    { command: [{ text: _ButtonEdit, className: "k-button k-button-icontext buttonEdit k-grid-Edit" }, { text: _ButtonDelete, className: "k-button k-button-icontext buttonDelete k-grid-Delete" }, { text: "&nbsp;", className: "buttonUp", width: 15 }, { text: "&nbsp;", className: "buttonDown", width: 15}], text: "", title: "&nbsp;", width: 230 } 
    ], 
     editable: "inline" 
    }); 

现在麻烦的是,变量“tPositionData”不能被重新分配到“_DetailsGridDS “因为它们显然不是同一类型或格式。正因为如此,我的全局变量“_DetailsGridDS”没有更新后的排序数据。如果我在代码中的其他地方引用它,那么我没有对数据进行排序。

有人可以帮我在排序后“撤消”/“反向”.toJSON调用,以便我可以重新分配给_DetailsGridDS,或者可以有人提出解决方法,以便我的全局变量总是更新与最新的排序数据?

+1

http://api.jquery.com/jQuery.parseJSON/应该工作 –

+2

为什么'toJSON()'在那个代码中呢? 'tPositiondata.toJSON()。sort()'没有意义,你不能排序字符串。首先排序,然后使用'toJSON()':'tPositiondata.sort(/ *您的排序函数* /)。toJSON()'。还要注意,如果两个项目相等,你的排序函数应该返回0。 – nnnnnn

+0

TiesonT。 - nopes它不工作,我得到一个空值:| @nnnnnn:我试过,之前我得到一个错误,说[对象对象]没有一种方法称为“排序” – Sandeep

回答

0

非常感谢所有提示。最终,我的解决办法是这样的:

function SortGrid() { 
if ($("#DivDetailsTable").data("kendoGrid") != undefined) { 
    if ($("#inputSortByValueName").is(":checked")) { 
     $("#DivDetailsTable").data("kendoGrid").dataSource.sort({ field: "DisplayText", dir: "asc" }); 
    } 
    else { 
     $("#DivDetailsTable").data("kendoGrid").dataSource.sort({ field: "DisplaySequence", dir: "asc" }); 
    } 
} 

}

这个工作只是罚款我。再次感谢你:)

0

Sandeep, 为什么你不能使用构建排序功能?你能解释一下你如何分类数据。

例如。将“可排序”添加到您的剑道网格定义中,并添加到您对排序感兴趣的列上。

$("#DivDetailsTable").kendoGrid({ 
    autobind: false, 
    scrollable: true, 
    sortable: { mode: "single", 
       allowUnsort: false }, 
+0

嗨谢谢 - 但我相信这将做服务器端排序。 – Sandeep

相关问题