jQuery Sparkline in a cell in ng-grid using CellTemplate and Directive

3.1k views Asked by At

I am trying to bring jQuery Sparkline at each row in one cell in ng-grid. The column contains numeric array data.

Plunkr --> http://plnkr.co/edit/1eNEcx6FQWcJynlVYvFw?p=preview

I am using directive with cell template to achieve this.

Cell Template:

Directive:

app.directive('ngAge', function() {
  return{
    restrict: 'C',
    replace: true,
    translude: true,
    scope: {ngAgeData: '@'},
    template: '<div>' +
              '<div class="sparklines"></div>' +
                '</div>',
    link: function(scope,element,attrs){
     // var arrvalue= "3386.24000,1107.04000,3418.80000,3353.68000,4232.80000,3874.64000,3483.92000,2735.04000,2474.56000,3288.56000,4395.60000,1107.04000";
     //console.log(attrs.ngAgeData);
     var arrvalue = attrs.ngAgeData;
     var myvalues = new Array();
     myvalues = arrvalue.split(",");
     $('.sparklines').sparkline(myvalues); 

    }
  }
});

I am having difficulty in getting the attrs.ngAgeData value inside the link function. I am not sure what I am missing. Please help!!!

Thanks

1

There are 1 answers

0
mainguy On

Use this celltemplate:

cellTemplate: '<div age-line agedata=row.entity.age></div>'

Note that the values are passed as attributes.

The directive should be:

app.directive('ageLine', function () {
    return {
        restrict: 'A',
        scope: { agedata: '=' },
        link: function (scope, elem) {
            scope.$watch('agedata', function (newval) {
                elem.sparkline(scope.agedata);
            });
        }
    };
});

Also note that you dont need the $., since the element is already an jquery(lite) object.

Here is a working plunker: http://plnkr.co/edit/oDbPp9DGFCGGZF30Bzsi?p=preview