简体   繁体   English

更新Kendo UI数据源

[英]Update Kendo UI Datasource

I'm currently working on a project in which I am using Spring MVC in conjunction with the Kendo UI jquery library (the latest version). 我目前正在开发一个项目,我将Spring MVC与Kendo UI jquery库(最新版本)结合使用。 The problem that I am having is updating the datasource of the kendo grid inline locally(kendo datasource) as well as remotely. 我遇到的问题是本地内联(kendo数据源)以及远程更新kendo网格的数据源。 I used the synch and set methods of the datasource object but neither of these worked. 我使用了数据源对象的synch和set方法,但这两种方法都不起作用。 Please see my jquery code below. 请参阅下面的我的jquery代码。

/*global $:false */

$(document).ready(function () {
"use strict";
var request;

$("#tabstrip").kendoTabStrip();



var applicationDataSource = new kendo.data.DataSource({
    transport: {
        read: {
            url: "/appinfo/findApplications",
            dataType: "json"
        },
        create: {
            url: "/appinfo/submit",
            dataType: "json",
            type: "POST"
        },
        update: {
            url: "/appinfo/updateApplication",
            dataType: "json",
            type: "POST"
        },
        destroy: {
            url: "/appinfo/deleteApplication",
            dataType: "json"
        },
        schema: {
            model: {
                id: "applicationId",
                fields: {
                    applicationId: {type: "number"},
                    applicationName: {type: "string"},
                    url: {type: "string"},
                    serverName: {type: "string"},
                    environmentName: {type: "string"},
                    ipAddress: {type: "string"},
                    genericUserName: {type: "string"},
                    genericPassword: {type: "string"}
                }
            }
        },
        parameterMap: function (options, operation) {
            if (operation !== "read" && options.models) {
                return {models: kendo.stringify(options.models)};
            }

            if (operation == "destroy" && options.models) {
                console.log("Delete worked");
                return { models: kendo.stringify(data.models) };
            }
        }
    },
    batch: true,
    pageSize: 10
});




var applicationGrid = $("#applicationsGrid").kendoGrid({
    dataSource: applicationDataSource,
    pageable: true,
    height: 600,
    scrollable: true,
    sortable: true,
    filterable: true,
    toolbar: [
        {name: "create", text: "Add New Application"}
    ],
    columnMenu: true,
    editable: {
        update: true,
        destroy: true,
        create: true,
        mode: "inline",
        confirmation: "Are you sure you want to delete this record?"
    },
    columns: [
        {field: "applicationName", title: "Application Name"},
        {field: "url", title: "URL"},
        {field: "serverName", title: "Server", editor: serverDropDownEditor, width: "300px"},
        {field: "environmentName", title: "Environment", editor: environmentDropDownEditor, width: "300px"},
        {field: "ipAddress", title: "Database", editor: databaseIpAddressDropDownEditor, width: "300px"},
        {field: "genericUserName", title: "Default Username"},
        {field: "genericPassword", title: "Default Password"},
        {title: "Modify", command: ["edit" , "destroy"]}
    ],
    edit: function (e) {           
        var dataItem = applicationDataSource.at(e.currentTarget);
        console.log("DataSource Count: " + applicationDataSource.total());
    },
    save: function (e) {
        var dataItem = applicationDataSource.at(e.currentTarget);          
        console.log("DataSource Count: " + applicationDataSource.total());
        console.log("The  model on save: " + e.model.applicationName);
        applicationDataSource.sync();
    },
    create: function (e) {
        console.log("Create this: " + e.values);
        applicationDataSource.insert(e.model);
        applicationDataSource.sync();
    },
    delete: function (e) {
        console.log("Delete this: " + e.model);
        applicationDataSource.remove(e.model);
    }
});


function serverDropDownEditor(container, options) {
    $('<input required data-text-field="serverName" data-value-field="serverId" data-bind="value:' + options.field + '"/>')
        .appendTo(container)
        .kendoDropDownList({
            autoBind: false,
            optionLabel: " - Select - ",
            dataTextField: "serverName",
            dataValueField: "serverId",
            dataSource: {
                transport: {
                    read: {
                        url: "/appinfo/findServers",
                        dataType: "json"
                    }
                }
            }
        });
}

function environmentDropDownEditor(container, options) {
    $('<input required data-text-field="environmentName" data-value-field="environmentId" data-bind="value:' + options.field + '"/>')
        .appendTo(container)
        .kendoDropDownList({
            autoBind: false,
            optionLabel: " - Select - ",
            dataTextField: "environmentName",
            dataValueField: "environmentId",
            dataSource: {
                transport: {
                    read: {
                        url: "/appinfo/findEnvironments",
                        dataType: "json"
                    }
                }
            }
        });
}

function databaseIpAddressDropDownEditor(container, options) {
    $('<input required data-text-field="ipAddress" data-value-field="databaseInfoId" data-bind="value:' + options.field + '"/>')
        .appendTo(container)
        .kendoDropDownList({
            autoBind: false,
            optionLabel: " - Select - ",
            dataTextField: "ipAddress",
            dataValueField: "databaseInfoId",
            dataSource: {
                transport: {
                    read: {
                        url: "/appinfo/findDatabases",
                        dataType: "json"
                    }
                }
            }
        });
}


});

BTW.... I am using the latest version of Kendo-UI Web. BTW ....我正在使用最新版本的Kendo-UI Web。

You don't really say what your actual problem is, but my guess is that it isn't making any network requests to the server. 你并没有真正说出你的实际问题是什么,但我的猜测是它没有向服务器发出任何网络请求。 I think this is because you have batch mode enabled on the DataSource, so it isn't going to automatically send changes to the server unless either the Grid tells it to, or you manually call .sync() 我认为这是因为您在DataSource上启用了batch模式,因此它不会自动将更改发送到服务器,除非Grid告诉它,或者您手动调用.sync()

I see you are trying to call .sync() in the save and create event handlers, but you don't need to do that. 我看到你试图在savecreate事件处理程序中调用.sync() ,但是你不需要这样做。 The grid will sync the datasource when the Save button is clicked. 单击“保存”按钮时,网格将同步数据源。 However, you don't have a Save button. 但是,您没有“保存”按钮。 Normally you would add one to the Grid's toolbar: 通常你会在Grid的工具栏中添加一个:

toolbar: ["create", "save", "cancel"],

Then you will get all 3 buttons on the grid toolbar. 然后,您将获得网格工具栏上的所有3个按钮。 You would make your edits to all your data rows, then click "Save", and the grid will call .sync() on your DataSource for you. 您可以对所有数据行进行编辑,然后单击“保存”,网格将为您调用DataSource上的.sync() This will also trigger the save event callback, but it doesn't look like your callback is doing anything but logging the data to the console. 这也会触发save事件回调,但看起来你的回调没有做任何事情,只是将数据记录到控制台。 You don't need to call .sync() in the event callbacks. 您不需要在事件回调中调用.sync()

The Grid : Batch Editing demo is set up like this as an example. 网格:批量编辑演示设置如下所示。


If you are expecting data to be sent to the server the moment that you edit, delete, or create a row, then you should set batch to false instead. 如果您希望在编辑,删除或创建行时将数据发送到服务器,则应将batch设置为false Then the DataSource won't wait for more (a batch) of changes, and will immediately send the data. 然后DataSource将不会等待更多(一批)更改,并将立即发送数据。

In addition to my other answer on DataSource batch mode, I also see that you are initializing 3 dropdown lists to use as editors. 除了我在DataSource batch模式上的其他答案之外,我还看到您正在初始化3个下拉列表以用作编辑器。 The Kendo Grid has a built-in feature for doing this, called ForeignKey Columns . Kendo Grid有一个内置的功能,称为ForeignKey Columns Their demo only shows a synchronous load of the FK data to use in a dropdown editor, but I have an example that loads multiple asynchronously here: 他们的演示只显示了在下拉编辑器中使用的FK数据的同步加载,但我有一个在这里异步加载多个的示例:

Kendo Music Store Docs 剑道音乐商店文档

Kendo Music Store Source (GitHub) 剑道音乐商店来源(GitHub)

var dataSource = new kendo.data.DataSource({
            //define datasource parameters as per your requirement
        });
    var grid = jQuery("#grid").kendoGrid({
                        dataSource: dataSource,
});

jQuery('#changeevent').change(function()
         {
                 dataSource.read({
                    parametername:jQuery("#valueoffeild").val()
                 });

                 var grid = jQuery("#grid").data("kendoGrid")
             grid.refresh();
         });

Above code pass extra parameter to your url. 上面的代码将额外参数传递给您的网址。

I am using kendo-ui 2014.3.1119 and this is what I did to get kendo-ui to leverage ngResource Restful API. 我正在使用kendo-ui 2014.3.1119,这就是我为了让kendo-ui利用ngResource Restful API所做的。

dataSource: {
    transport: {
        read: function (options) {
            RestService.query(function (result) {
                options.success(result);
            });
        },
        update: function (options) {
            RestService.update(options.data, function (result) {
                options.success(result);
            });
        },
        create: function (options) {
            RestService.save(options.data, function (result) {
                options.success(result);
            });
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM