简体   繁体   English

Kendo Grid:如何使数据源触发更新和创建事件

[英]Kendo Grid: how to make datasource trigger update and create events

I am starting to look at how I would get my grid edits back to a service via the datasource. 我开始研究如何通过数据源将网格编辑返回到服务。

Following the documentation, I have set a local test data source as follows.. 根据文档,我将本地测试数据源设置如下。

function getDataSource() {
var gridData = [
{
  col1: new CellData('1', 'data1-1'),
  col2: new CellData('2', 'data1-2')
  },
{
  col1: new CellData('3', 'data2-1'),
  col2: new CellData('4', 'data2-2')
  },
];

var dataSrc = new kendo.data.DataSource({
batch: true,

transport: {
  read: function (e) {
    e.success(gridData);
  },

  update: function (e) {
    // batch is enabled
    var updateItems = e.data.models;

    // This is not called

    // on success
    e.success();
  },

  create: function (e) {
    e.success(e.data);
  },
  destroy: function (e) {
    e.success();
  }
 }
});

 return dataSrc;
}

I have a toolbar setup (with the "Save Changes"), and this is calling the SaveChanges configuration event, how ever, just cannot see what else I need to do to get the following to occur.. 我有一个工具栏设置(带有“保存更改”),并且这正在调用SaveChanges配置事件,但是,只是看不到我需要做些什么才能使以下事件发生。

  1. Have the data sources update called 让数据源更新称为
  2. Mark the grid "on dirty" so that the red "edited" indicators on the edited cells disappear 将网格标记为“脏”,以使已编辑单元格上的红色“已编辑”指示消失

I am having the same problem with the Add New record (though I can't get the grids "addRow" even to fire here) 我在“添加新记录”中遇到了同样的问题(尽管我无法在此处触发网格“ addRow”)

I have the running example here 这里有运行示例

Any help would be great appreciated! 任何帮助将不胜感激!

You need to specify the DataSource schema for this to work: 您需要指定数据源架构才能使其工作:

var dataSrc = new kendo.data.DataSource({
    batch: false, // true would mean transport functions get multipe models  in e.data 
    transport: {
        // ....
    },
    schema: {
        data: function (response) {
            return response;
        },
        model: {
            id: "id",
            fields: {
                id: {
                    editable: false,
                    defaultValue: 0 // 0 == new / unsaved row
                },
                col1: {
                    editable: true,
                    // new items would have that using default add button
                    defaultValue: {
                        id: 0,
                        CategoryName: ""
                    }, 
                    fields: { id: { editable: true }, display: { editable: true }
                },
                col2: {
                    editable: true,
                    fields: { id: { editable: true }, display: {  editable: true } }
                }
            }
        }
    }
});

Also note: 另请注意:

  • grid.saveChanges will sync the DS, so you don't need to do anything in the event grid.saveChanges将同步DS,因此您无需在事件中进行任何操作
  • There is no addRow event. 没有addRow事件。
  • The default "create" button will try to add an empty object; 默认的“创建”按钮将尝试添加一个空对象。 since you work with nested objects, you need to add the row yourself so you can initialize the properties; 由于使用嵌套对象,因此需要自己添加行,以便可以初始化属性。 thus you need a custom button and bind your action manually 因此,您需要一个自定义按钮并手动绑定操作

( demo ) 演示

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

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