2016-02-04 21 views
1

我有一個kendo一個網格,從angularJs服務填充。在angularJs服務中,我已經爲調用API服務的CRUD編寫了方法。但在網格上插入新數據後,update命令不起作用,而是激發了create方法。並說數據沒有定義。結果行數據在網格行上保持可編輯狀態,不執行刷新。Kendo網格更新命令激發創建方法

這裏的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"); 
    }); 
}; 



}]); 
+0

你在'updateRoadInventory'函數中錯過'e.data'這裏'e'丟失了。 –

+0

nope,那不工作... :( –

+0

你應該使用kendo角度。你使用jquery劍道還是angular? –

回答

0

其實型號的id是沒有定義。

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

       } 
      } 
     } 

因此它激發了創建方法。原因基本上,DataSource檢查正在編輯的項目的ID,如果它不是0(或ID字段的默認值),則調用Update,否則 - Create。

相關問題