Kendo Grid Update命令触发create方法

时间:2016-02-04 10:37:01

标签: angularjs kendo-ui kendo-grid

我有一个kendo网格,从angularJs服务填充。在angularJs服务中,我为CRUD编写了调用API服务的方法。但是在网格上插入新数据后,更新命令无法正常工作,而是触发了create方法。并说数据没有定义。因此,行数据在网格行上保持可编辑状态,而不执行刷新。

这是controller.js

app.controller("roadInventoryCtrl", function ($scope, services) {


$scope.gridOptions = {
    columns: [
        {
            field: "RoadCode",
            title: "Road Code",
            width: "",
            attributes: { class: "ob-right" }
        },
        {
            field: "RoadTypeId",
            title: "Type",
            readonly: true,
            width: "",
            attributes: { class: "ob-right" }
        },
        {
            field: "SerialNo",
            title: "Serial",
            width: "",
            attributes: { class: "ob-right" }
        },
        {
            field: "Name",
            width: "38%"
        },
        {
            field: "Length",
            filterable: false,
            attributes: { class: "ob-right" }
        },
        {
            field: "CrestWidth",
            title: "Crest width",
            filterable: false,
            attributes: { class: "ob-right" }
        },
        {
            field: "EmbkHeight",
            title: "Embk height",
            filterable: false,
            attributes: { class: "ob-right" }
        },
        {
            field: "Remarks",
            filterable: false
        },
        {
            command: [
                {
                    name: "edit",
                    template: "<a data-toggle='tooltip' data-placement='left' title='edit' class='k-grid-edit k-grid-update red-tooltip'  style='width: 26px; height: 26px; vertical-align: middle; text-align: center; cursor:pointer'><span style='margin: 4px;' class='glyphicon glyphicon-edit'></span></a>",
                    text: " "

                },
                {
                    name: "destroy",
                    template: "<a if-role-permission=\"['PERMISSION_WORKFLOW_DEFINITION_DELETE']\" class='k-grid-delete'  style='width: 26px; height: 26px; vertical-align: middle; text-align: center; cursor:pointer' ><span style='margin: 4px;' class='glyphicon glyphicon-remove-circle target' title='delete'></span></a>",
                    text: " "
                }, {
                    name: "map",
                    template: "<a class='k-grid-delete'  style='width: 26px; height: 26px; vertical-align: middle; text-align: center; cursor:pointer' ><span style='margin: 4px;' class='glyphicon glyphicon-map-marker target' title='map' ></span></a>",
                    text: " "
                }, {
                    name: "info",
                    template: "<a class='k-grid-delete'  style='width: 26px; height: 26px; vertical-align: middle; text-align: center; cursor:pointer' ><span style='margin: 4px;' class='glyphicon glyphicon-info-sign target' title='info' ></span></a>",
                    text: " "
                }, {
                    name: "pic",
                    template: "<a class='k-grid-delete'  style='width: 26px; height: 26px; vertical-align: middle; text-align: center; cursor:pointer' ><span style='margin: 4px;' class='glyphicon glyphicon-picture target' title='picture'></span></a>",
                    text: " "
                }
            ],
            title: "",
            width: "11%"

        }
    ],

    editable: 'inline',

    toolbar: [ "create" ],
    pageable: true,

    dataSource: {
        pageSize: 25,
        transport: {
            read: function(e) {
                services.getRoadInventroy()
                    .then(function success(response) {
                        e.success(response.data);
                    }, function error(response) {
                        alert('something went wrong');
                        console.log(response);
                    });
            },
            update: function (e) {
                services.updateRoadInventory()
                    .then(function success(response) {
                        e.success(response.data);
                    }, function error(response) {
                        console.log(response);
                    });
            },

            destroy: function (e) {
                services.destroyRoadInventory()
                    .then(function success(response) {
                            e.success(response.data);
                        }, function error(response) {
                            console.log(response);
                        }
                    );

            },
            create: function (e) {
                services.createRoadInventory()
                    .then(function success(response) {
                        e.success(response.data);
                    },function error(response) {
                        console.log(response);    
                    }
                    );
            }
        },
        schema: {
            model: {
                fields: {
                    RoadCode: {
                        editable: false
                    }
                }
            }
        }
    },

    sortable: true,

    scrollable: true,

    selectable: "row",

    filterable: {
        extra: false,
        operators: {
        string: {
            startswith: "Starts with",
            contains:"Contains"

         },
         number: {
         }
       }
    }


    }
});

这是service.js

app.service('services', ['$http', function ($http) {
var result;
this.getRoadInventroy = function () {
    result = $http.get("/api/InventoryService").success(function (data) {
        result = (data);
    }).error(function () {
        alert("Not hiting the Api Controller");
    });

    return result;
};

this.createRoadInventory = function (e) {
    //alert(JSON.stringify(e));
    //alert(e.data);
    $http.post("/api/InventoryService",e.data).success(function () {
    }).error(function() {
        alert("Not Hitting the POST of API but hitting the services");
    });
};
this.destroyRoadInventory = function () {

};
this.updateRoadInventory = function () {
    $http.put("/api/InventoryService", e.data).success(function () {
    }).error(function (){
        alert("Not Hitting the put of API but hitting the services");
    });
};



}]);

1 个答案:

答案 0 :(得分:0)

实际上模型的id没有在那里定义。

            schema: {
            model: {
                id:"Id",
                fields: {
                    RoadCode: {
                        editable: false
                    }

                }
            }
        }

结果它触发了create方法。原因基本上是,DataSource检查正在编辑的项目的ID,如果它不同于0(或ID字段的默认值),则调用Update,否则 - Create。