Anubha Gupta
Anubha Gupta

Reputation: 189

How to call a function from cell template in ui-grid in angularjs?

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;
          }

      });

Upvotes: 1

Views: 1365

Answers (2)

Anubha Gupta
Anubha Gupta

Reputation: 189

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('');

Upvotes: 0

alok modi
alok modi

Reputation: 26

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()"

Upvotes: 1

Related Questions