Saving row data with AngularJS ui-grid $ scope.saveRow

I am working on a small interface that will display various data on the delivery of goods in ui-grid.

I have the following code:

HTML:

<body ng-controller="MyCtrl"> <p ng-repeat="row in myData">{{row.name}} works at {{row.company}}</p> <button type="button" class="btn btn-success" ng-click="getCurrentFocus()">Get Current focused cell</button> <span ng-bind="currentFocused"></span> <br/> <br/> <div id="grid1" ui-grid="gridOptions" external-scopes="editRows" ui-grid-edit ui-grid-row-edit ui-grid-pinning ui-grid-paging ui-grid-cellnav class="grid"></div> </body> 

AngularJS:

 var app = angular.module('webapps', ['ngAnimate', 'ui.grid', 'ui.grid.edit', 'ui.grid.rowEdit', 'ui.grid.pinning', 'ui.grid.paging', 'ui.grid.cellNav']); app.controller('MyCtrl', ['$scope', '$http', '$q', '$interval', function ($scope, $http, $q, $interval) { //Column definitions $scope.columns = [ { field: 'name', displayName: 'First Name', width: 300}, { field: 'lastname', displayName: 'Last Name', width: 300 }, { field: 'email', displayName: 'Email', width: 300 }, { field: 'company', displayName: 'Company', width: '50%' } ]; //Options for displaying the grid $scope.gridOptions = { data: 'myData', enableCellEditOnFocus: false, pagingPageSizes: [2, 5, 7], pagingPageSize: 5, enableSorting: true, enableGridMenu: true, columnDefs: $scope.columns, onRegisterApi: function (gridApi) { $scope.gridApi = gridApi; //var cellTemplate = '<button class="btn primary" ng-click="getExternalScopes().showMe(rowCol.row.entity)">Edit</button>'; //'ui-grid/selectionRowHeader'; // you could use your own template here //$scope.gridApi.core.addRowHeaderColumn({ name: 'rowHeaderCol', displayName: '', width: 50, pinnedLeft: true, cellTemplate: cellTemplate }); gridApi.rowEdit.on.saveRow($scope, $scope.saveRow); } }; // Save row data on edit 1 - TESTING $scope.saveRow = function (rowEntity) { // create a fake promise - normally you'd use the promise returned by $http or $resource var promise = $http.post("Webapps/Home/SaveRow"); $scope.gridApi.rowEdit.setSavePromise($scope.gridApi.grid, rowEntity, promise.promise); console.log(rowEntity); // fake a delay of 3 seconds whilst the save occurs, return error if gender is "male" $interval(function () { if (rowEntity.lastname === ' ') { promise.reject(); } else { promise.resolve(); } }, 3000, 1); }; }]); // End of MyCtrl 

My question is about the $ scope.saveRow method. I looked through the documentation here and tried to do a google search (not much for ui-grid), but I'm still dead because I'm a little inexperienced and I'm not sure how to code this correctly with promises.

The MVC application will work for this, which will handle the transfer of data to the foreground and saving changes to the table on SQL Server db. Obviously, I would like this $ scope.saveRow function to correctly send rowEnttiy data back to my MVC application, but again, I don’t understand how to encode the function. The β€œfake” promises included in the example are not enough for me to understand what I need to do, apparently. First I tried the following:

 $scope.saveRow = function (rowEntity) { try { // Need actual URL for post...controller has to accept 'row' $http.post("Webapps/Home/SaveRow", { row: rowEntity }).success(function (data, status, headers, config) { console.log("post success"); console.log(rowEntity); }).error(function (data, status, headers, config) { console.log("post failure"); console.log(rowEntity); }); } catch (e) { alert("Something went wrong"); } }; 

but this only throws an exception in my console that the promise was not returned when the saveRow event was raised.

+6
source share
3 answers

Usually you make a prom / api call in a separate repository, but basically the code you are looking for looks something like this:

  $scope.saveRow = function( rowEntity ) { var promise = $scope.someRepositoryFunction(rowEntity); $scope.gridApi.rowEdit.setSavePromise($scope.gridApi.grid, rowEntity, promise); // fake a delay of 3 seconds whilst the save occurs, return error if gender is "male" //$interval( function() { // if (rowEntity.firstName === 'male' ){ // promise.reject(); // } else { // promise.resolve(); // } //}, 3000, 1); }; $scope.someRepositoryFunction = function(row) { return $http.put('/Home/UpdateRow',row); } 
+8
source

I am not allowed to comment, so I have to send a response. The answer above is incorrect in that the $ scope.gridApi.grid parameter for the setSavePromise function is NOT required. Although the function does require a grid parameter, it is added by the shell during the .apply call.

+6
source

I really used the code provided by the punkologist and it really worked, but there seemed to be errors in the console when resolving promises after including $ interval.

I assume the following code is suitable for it to work without errors. Hope this works.

 $scope.saveRow = function( rowEntity ) { var promise = $q.defer(); $http.put('/Home/UpdateRow',row).success(function(){ $interval(function(){ promise.resolved(); },3000, 1) }).error(promise.reject); $scope.gridApi.rowEdit.setSavePromise($scope.gridApi.grid, rowEntity, promise); }; 
+2
source

Source: https://habr.com/ru/post/979337/


All Articles