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

允我心安 提交于 2019-12-10 10:57:27

问题


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:


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



来源:https://stackoverflow.com/questions/18159347/jquery-sparkline-in-a-cell-in-ng-grid-using-celltemplate-and-directive

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!