【问题标题】:What is ".toJSON" and how can I "undo" or "reverse" it什么是“.toJSON”以及如何“撤消”或“反转”它
【发布时间】:2012-10-22 18:03:06
【问题描述】:

我正在做一个项目,我们使用 KENDOUI 作为前端,使用 jQuery/Javascript 编码来管理我们无法用框架本身做的任何事情。

我有一个需要客户端排序的 KENDO Grid,这就是我想要做的 -

    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 或者有人可以建议一种解决方法,以便我的全局变量始终更新为最新排序的数据?

【问题讨论】:

  • api.jquery.com/jQuery.parseJSON 应该可以工作
  • 为什么toJSON() 在该代码中? tPositiondata.toJSON().sort() 没有意义,你不能对字符串进行排序。先排序,然后使用toJSON()tPositiondata.sort(/*your sort function here*/).toJSON()。另请注意,如果两项相等,您的排序函数应返回 0。
  • TiesonT. - 不,它不起作用,我得到一个空值:| @nnnnnn:我在收到错误消息之前尝试过,说 [object Object] 没有名为“sort”的方法

标签: javascript javascript-framework kendo-ui gridview-sorting


【解决方案1】:

桑迪普, 为什么不能使用内置排序功能?您能否解释一下如何对数据进行排序。

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

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

【讨论】:

  • 嗨,谢谢 - 但我相信这会进行服务器端排序。
【解决方案2】:

非常感谢您提供的所有提示。最终我的解决方案是这样的:

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

}

这对我来说效果很好。再次感谢你:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-26
    • 2020-08-06
    • 2023-04-09
    相关资源
    最近更新 更多