简体   繁体   English

Kendo网格取消导致删除行

[英]Kendo grid cancel causing deletion of row

I am using kendo grid and the grid.And on a particular situation i am adding data to the datasource of the grid using grid.dataSource.add() method.The following is the configuration of my grid. 我正在使用kendo网格和网格。在特定情况下,我使用grid.dataSource.add()方法将数据添加到网格的数据源。以下是我的网格的配置。

var itemcodedataSource = new kendo.data.DataSource({
            dataType: "json",
            transport: {
                    read: function(o){
                       o.success([]);
                    },
                    create: function(o) {                      
                        var item = o.data;
                            //assign a unique ID and return the record
                            item.id =  len;
                            o.success(item);
                            len++;
                    },
                    destroy: function(o) {
                          o.success();
                    },
                    update: function(o){
                          o.success();
                    }         
            },                      
            autoSync: true,
            schema: {                       
            model: {
                id    : "id",
                fields: {
                        type_flag: {validation: {}},
                        item_code:{validation:{}},
                        bill_no:{validation:{}},
                        item_code_desc: {validation: {},editable:false},
                        line_balance:{validation:{},type:"number",editable:false},
                        collection_status:{validation:{},editable:false},
                        amount:{validation:{required:false},type:"number",nullable:false },
                        item_detail:{},
                        total_balance:{type:"number",nullable:false},
                        total_due:{type:"number",nullable:false},
                        amt_edit_flag:{},
                    }   
                }
            },
        });

        $("#itemcode_grid").kendoGrid({
                dataSource: itemcodedataSource,
                selectable: "multiple",
                change     : calcTotals,
                toolbar: ["create"],
                    columns:[
                                { field: "type_flag", width: "90px", title: "Type",sortable:false,
                                },
                                { field: "item_code", width: "80px", title: "Item Code",sortable:false
                                },
                                { field: "bill_no", width: "80px", title: "Bill Number",sortable:false
                                },
                                { field: "line_balance", width: "50px", title: "Deferrals",sortable:false
                                },
                                { field: "collection_status", width: "50px", title: "Hold",sortable:false
                                },
                                { field: "amount", width: "70px", title: "Payment",sortable:false
                                },
                                { command: ["edit", "destroy"], title: "Options", width: "130px"},
                            ],
                    editable: "inline",
                });

And i am adding the data to datasource by this way 我通过这种方式将数据添加到数据源

var gridDs      =   $("#itemcode_grid").data("kendoGrid").dataSource;
            for (var i = 0; i < gridData.length; i++) {
                gridDs.add({
                    type_flag           : gridData[i].type_flag, 
                    item_code           : gridData[i].item_code,
                    bill_no             : detailsData[i].bill_no, 
                    item_code_desc      : detailsData[i].itemcode_details.item_code_desc,
                    line_balance        : gridData[i].line_deferred, 
                    collection_status   : detailsData[i].collection_status,
                    amount              : parseFloat(gridData[i].amount),
                    qty_pricing_type    : detailsData[i].itemcode_details.qty_pricing_type,
                    item_detail         : res[0][i],
                    total_balance       : parseFloat(detailsData[i].line_balance),
                    total_due           : detailsData[i].line_due,
                    id                  : gridDs._data.length+1,
                });

                gridDs.sync();
            }

Where detailsData and gridData are the response of a ajax.What my problem is that this method adds new data to the grid.But while click on edit and cancel deletes the selected row from the grid.Usually this happens when the items in the grid does not have a unique id.But i checked and the items all have a unique id.Whats wrong with this code.How to solve this error.Thanks in advance. 其中detailsDatagridDatagridData的响应。我的问题是这个方法是向网格添加新数据。但是点击编辑并取消时会从网格中删除所选行。通常当网格中的项目确实发生这种情况时会发生这种情况。没有唯一的id.But我检查和项目都有一个唯一的id.Whats错误的代码。如何解决这个错误。谢谢提前。

Your record is being removed because the just added data is being destroyed when you cancel the edition. 您的记录正在删除,因为取消版本时正在销毁刚添加的数据。

Place a trace on destroy method and you will see it being invoked when you hit cancel and destroy is being invoked because the have actually never been created into the server (place a trace on create handler and you will see that it is not being invoked). destroy方法上放置一个跟踪,当你点击cancel并且正在调用destroy时,你会看到它被调用,因为它实际上从未被创建到服务器中(在create处理程序上放置一条跟踪,你会看到它没有被调用) 。

And create is not being invoked because when you add them in the for loop you assign an id . 并且没有调用create因为当你在for循环中添加它时,你会分配一个id

Try the for loop as follow: 尝试使用for循环如下:

var gridDs      =   $("#itemcode_grid").data("kendoGrid").dataSource;
for (var i = 0; i < gridData.length; i++) {
    gridDs.add({
        type_flag           : gridData[i].type_flag,
        item_code           : gridData[i].item_code,
        bill_no             : detailsData[i].bill_no,
        item_code_desc      : detailsData[i].itemcode_details.item_code_desc,
        line_balance        : gridData[i].line_deferred,
        collection_status   : detailsData[i].collection_status,
        amount              : parseFloat(gridData[i].amount),
        qty_pricing_type    : detailsData[i].itemcode_details.qty_pricing_type,
        item_detail         : res[0][i],
        total_balance       : parseFloat(detailsData[i].line_balance),
        total_due           : detailsData[i].line_due
    });

    gridDs.sync();
}

Conclusion: it is bad not assigning id but it is bad assigning it to early. 结论:没有分配id很糟糕但很难将其分配给早期。

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

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