1

I have one controller in angularJs(1.6) which is using ui-grid. Problem is there is one cell template. I want to get its value from a function. I have tried creating a function and tried to call it using grid.appScope.function. But it did not work. I have tried to put my function with vm also. But to not avail.

Could anyone please help me with this?

Here is my controller code:

 (function () {
    'use strict';

     define([
         'angular'
     ], function (angular) {

     function SelectToolController($scope, $timeout, $filter, uiGridConstants) {
                  var vm = this,
                      _gridApi,
                      _cellTemplate,
                      _columnDefs,
                      _starRatingTemplate,
                      _starRatingColumn,
                      _starEnable;
            };
            _starRatingTemplate = [
                  '<div class="opr-star-rating"  >',
                  '<opr-star-rating rating-percentage="getRating()">',
                  '</opr-star-rating>',
                  '</div>'
            ].join('');

      //this func need to be called from _starRatingTemplate 
       vm.getRating = function(){
           return 10;
        }

        function gridInitialized(gridApi) {
            _gridApi = gridApi;
          }

      });
johey
  • 1,139
  • 1
  • 9
  • 25
Anubha Gupta
  • 189
  • 2
  • 17

2 Answers2

1

We can access parent scope inside grid cell template using "grid.appScope" object.

If you have created getRating function as $scope.getRating(){..} in parent controller then you can access it as following

rating-percentage="grid.appScope.getRating()"

If you have created getRating function inside vm object of controller and used controller as following

 <div ng-controller="SelectToolController as SelectToolCtl">
....
 </div>

then simple use rating-percentage="grid.appScope.SelectToolCtl.getRating()"

alok modi
  • 26
  • 3
  • Hi, Thanks for your answer. Problem is when I used your approach I see this error: "The controller with the name 'SelectToolController' is not registered." Though it is there as you can see. Could you please tell me what could be the possible reason? – Anubha Gupta Nov 19 '18 at 05:12
  • you have to register your controller angular.controller('SelectToolController', ['$scope', '$timeout', '$filter', 'uiGridConstants', SelectToolController]); – alok modi Nov 20 '18 at 09:04
0

I am posting answer of my own query.

Below setting in cellTemplate worked for me:

  _starRatingTemplate = [
                  '<div class="opr-star-rating"  >',
                  '<opr-star-rating rating-percentage="+vm.getRating()+">',
                  '</opr-star-rating>',
                  '</div>'
   ].join('');
Anubha Gupta
  • 189
  • 2
  • 17
  • This will call the function only once when creating the string, as you already found out in your other question posted: https://stackoverflow.com/questions/53371278/how-to-call-a-function-multiple-times-from-cell-template-in-ui-grid-in-angularjs – ArielGro Nov 21 '18 at 07:57