根据条件选择Kendo网格数据源

问题描述 投票:0回答:1

我有一个Kendo网格,我想根据一个标志值来切换数据源。我为此创建了两个DataSource变量,并根据标志值将它们传递给Kendo Grid DataSource属性。像这样dataSource: flag == 1 ? originalDataSource : backupDataSource

var flag = 1;
//DataSource 1
var backupDataSource = new kendo.data.DataSource({
    transport: {
        read: {
            url: "/MyController/MyAction1",
            dataType: "json",
            data: {
                ID : "1"
            },
        }
    },
    change: function (e) {},
    error: function () {},
    pageSize: 10,
    schema: {
        model: {
            fields: {
                Id: { type: "number" },
                Date: { type: "date" },
                Name: { type: "string" }
            },
        },
    }
});

// DataSource 2
var originalDataSource = new kendo.data.DataSource({
    transport: {
        read: {
            url: "/MyController/MyAction2",
            dataType: "json",
            data: {
                ID : "1"
            },
        }
    },
    change: function (e) {},
    error: function () {},
    pageSize: 10,
    schema: {
        model: {
            fields: {
                Id: { type: "number" },
                Date: { type: "date" },
                Name: { type: "string" }
            },
        },
    }
});

//Calling Kendo Grid
$("#campaign-queue").kendoGrid({
            dataSource: flag == 1 ? originalDataSource : backupDataSource,
            filterable: true,
            sortable: true,
            columns: [
                {
                    field: "Id",
                    width: 55,
                    filterable: {
                        search: true,
                        extra: false
                    }
                },
                {
                    field: "Name",
                    width: 143,
                    filterable: {
                        extra: false
                    }
                }
            ],
            pageable: {
                previousNext: true,
                pageSize: 10
            }
        }); 

但是这不符合我的预期。仅创建originalDataSource,但backupDataSource始终为空。我究竟做错了什么?任何帮助将不胜感激。谢谢。

jquery asp.net-mvc kendo-ui kendo-grid kendo-asp.net-mvc
1个回答
0
投票

如果第二个为空,则从控制器读取时肯定有问题。

© www.soinside.com 2019 - 2024. All rights reserved.