salesforce - Adding a mass update feature to UI-Grid (AngularJS) -
we calling ui-grid in angularjs salesforce visualforce page. used create editor external users can see list of students , update student information.
my goal add function allow user update multiple rows @ once fields/values (defined in menus). appreciate advice whether i'm on right track here or more efficient/functional methodology.
the normal row save working, implemented in controller
$scope.saverow = function(studentterm) { var promise = $q.defer(); $scope.gridapi.rowedit.setsavepromise(studentterm, promise.promise); var updatablefieldsonstudentterm = { studenttermid: studentterm.studenttermid, appliedforasset: studentterm.appliedforasset, studentidatcollege: studentterm.studentidatcollege, comments: studentterm.comments, dsfscholarshipstatus: studentterm.dsfscholarshipstatus, eligibilityallrequirementsforaward: studentterm.eligibilityallrequirementsforaward, //eligibilityhsgpamet: studentterm.eligibilityhsgpamet, enrollmentstatus: studentterm.enrollmentstatus, enrollmenttype: studentterm.enrollmenttype, fafilecompletiondeadline: studentterm.fafilecompletiondeadline, finalefcuponfafilecompletion: studentterm.finalefcuponfafilecompletion, financialaidcompletedbydeadline: studentterm.financialaidcompletedbydeadline, financialaidstatus: studentterm.financialaidstatus, hasstudentcompleted4yeardegree: studentterm.hasstudentcompleted4yeardegree, //partnerscholarshiprecipient: studentterm.partnerscholarshiprecipient, preliminaryefcrange: studentterm.preliminaryefcrange, programenrolled: studentterm.programenrolled, cumulativecollegegpa: studentterm.cumulativecollegegpa, devedreqcompletedwithin1styear: studentterm.devedreqcompletedwithin1styear, sapstatus: studentterm.sapstatus, ssrrequirementscomplete: studentterm.ssrrequirementscomplete, applicationid: studentterm.applicationid, scholarshippaymentamount: studentterm.scholarshippaymentamount, studentid: studentterm.studentid, //danielsscholar: studentterm.danielsscholar } termsservice.updatestudenttermbyid(updatablefieldsonstudentterm).then(function(st) { var rowindex = $scope.studentterms.indexof(studentterm); if(rowindex != -1) { st.options = $scope.fieldoptions; $scope.studentterms[rowindex].scholarshippaymentamount = st.scholarshippaymentamount; $scope.gridoptions.data = $scope.studentterms; if($scope.searchtext) { $scope.refreshdata(); } notification.success('changes saved ' + studentterm.studentname); } promise.resolve(); }).catch(function(err) { console.log(err); notification.error('save failed ' + studentterm.studentname); promise.reject(); }); };
and app script
var app = angular.module('scholarshipapp', [ 'app.controllers', 'app.templates', 'ngsanitize', 'ui.grid', 'ui.grid.edit', 'ui.grid.cellnav', 'ui.grid.pagination', 'ui.grid.exporter', 'ui.grid.selection', 'ui.grid.rowedit', 'ui.grid.pinning', 'ui.bootstrap', 'cgbusy', 'ui-notification' ]); app.config(function(notificationprovider) { notificationprovider.setoptions({ delay: 2000, starttop: 20, startright: 10, verticalspacing: 20, horizontalspacing: 20, positionx: 'top', positiony: 'right' }); }); app.factory('termsservice', function($rootscope, $q, $window) { var _updatestudentterm = function(studentterm) { var deferred = $q.defer(); var action = $window.scholarship_app.vfremoting.termsservice.updatestudentterm; visualforce.remoting.manager.invokeaction(action, studentterm, function(result, event) { $rootscope.$apply(function() { if (event.status) { deferred.resolve(result); } else { deferred.reject(event); } }); }, {buffer: true, escape: false, timeout: 30000}); return deferred.promise; }; return { getstudentterms: _getstudentterms, updatestudenttermbyid: _updatestudentterm, getselectoptions: _getselectoptions, notifyupdatesdone: _notifyupdatesdone, isegtc: _isegtc, massupdate: _massupdate }; });
ui-grid allows row row updates trying call massupdate function in turn calls apex method in salesforce through remote feature. here code i've added (inside same factory , app won't include in snippets).
controller
$scope.massupdate = function() { var idlist = []; var rows; confirm('update ' + editrows + ' rows set ' + editfield + ' ' + editvalue + '?'); switch (editrows) { case "all" : rows = $scope.studentterms.rows; break; case "visible" : rows = $scope.studentterms.getvisiblerows(); break; case "selected" : rows = $scope.studentterms.api.selection.getselectedgridrows(); break; } switch (editfield) { case "program" : for(var = 0; < rows.length; i++) { idlist[i] = rows[i].id; $scope.studentterms[i].program_enrolled__c = editvalue; } break; case "appl. asset" : for(var = 0; < rows.length; i++) { idlist[i] = rows[i].id; $scope.studentterms[i].applied_for_asset__c = editvalue; } break; case "4 year degree" : for(var = 0; < rows.length; i++) { idlist[i] = rows[i].id; $scope.studentterms[i].has_student_completed_4_year_degree__c = editvalue; } break; } termsservice.massupdate(idlist,editfield,editvalue).then(function() { $scope.gridoptions.data = $scope.studentterms; if($scope.searchtext) { $scope.refreshdata(); } notification.success(editrows + 'rows updated - ' + editfield + 'set ' + editvalue); promise.resolve(); }).catch(function(err) { console.log(err); notification.error('mass update failed.'); promise.reject(); }); }
and app
var _massupdate = function(editrows,editfield,editvalue) { var deferred = $q.defer(); var action = $window.scholarship_app.vfremoting.termsservice.massupdate; visualforce.remoting.manager.invokeaction(action, function(result, event) { $rootscope.$apply(function() { if (event.status) { deferred.resolve(result); } else { deferred.reject(event); } }); }, {buffer: true, escape: false, timeout: 30000}); return deferred.promise; };
Comments
Post a Comment