Kendo网格行选择客户端持久性问题

时间:2015-10-02 00:51:26

标签: javascript jquery kendo-ui telerik kendo-grid

我们有选择坚持,可能只是有点太多。 :d

例如,如果您有一个带有客户端数据的多页kendo网格,请使用客户端kendo网格执行此操作:

  • 在第1页

  • 上选择一行
  • 转到第2页

  • 在第2页上选择一行然后取消选择并选择另一行

  • 返回第1页(行选择仍然存在)

  • 返回第2页

行选择仍然存在,但也会选择先前取消选择的行。

有解决方案吗?我们可以在变更事件中使用的东西:

http://dojo.telerik.com/@crunchfactory/uhEZe/7

谢谢,

Ĵ

1 个答案:

答案 0 :(得分:1)

请尝试使用以下代码段。

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <link rel="stylesheet" href="https://kendo.cdn.telerik.com/2015.2.902/styles/kendo.common-material.min.css" />
    <link rel="stylesheet" href="https://kendo.cdn.telerik.com/2015.2.902/styles/kendo.material.min.css" />

    <script src="https://kendo.cdn.telerik.com/2015.2.902/js/jquery.min.js"></script>
    <script src="https://kendo.cdn.telerik.com/2015.2.902/js/kendo.all.min.js"></script>
</head>
<body>
    <script src="http://demos.telerik.com/kendo-ui/content/shared/js/products.js"></script>

    <div id="example">
        <div id="grid"></div>

        <script>
            $(document).ready(function () {

                var selectedOrders = [];
                var idField = "ProductID";


                $("#grid").kendoGrid({
                    dataSource: {
                        data: products,
                        schema: {
                            model: {
                                fields: {
                                    ProductName: { type: "string" },
                                    UnitPrice: { type: "number" },
                                    UnitsInStock: { type: "number" },
                                    Discontinued: { type: "boolean" }
                                }
                            }
                        },
                        pageSize: 20
                    },
                    height: 550,
                    scrollable: true,
                    sortable: true,
                    selectable: "multiple, row",
                    pageable: {
                        input: true,
                        numeric: false
                    },
                    columns: [
                        "ProductName",
                        { field: "UnitPrice", title: "Unit Price", format: "{0:c}", width: "130px" },
                        { field: "UnitsInStock", title: "Units In Stock", width: "130px" },
                        { field: "Discontinued", width: "130px" }
                    ],
                    change: function (e, args) {
                        var grid = e.sender;
                        var items = grid.items();
                        items.each(function (idx, row) {
                            var idValue = grid.dataItem(row).get(idField);
                            if (row.className.indexOf("k-state-selected") >= 0) {
                                selectedOrders[idValue] = true;
                            } else if (selectedOrders[idValue]) {
                                delete selectedOrders[idValue];
                            }
                        });
                    },
                    dataBound: function (e) {
                        var grid = e.sender;
                        var items = grid.items();
                        var itemsToSelect = [];
                        items.each(function (idx, row) {
                            var dataItem = grid.dataItem(row);
                            if (selectedOrders[dataItem[idField]]) {
                                itemsToSelect.push(row);
                            }
                        });

                        e.sender.select(itemsToSelect);
                    }
                });
            });
        </script>
    </div>


</body>
</html>

如果有任何疑虑,请告诉我。