ui-grid icon indicating copy to clipboard operation
ui-grid copied to clipboard

Problem using a directive inside a cell template

Open davidagee opened this issue 13 years ago • 4 comments

I'm attempting to use a custom directive inside a treegrid implementation of ng-grid to show a bullet graph. The bullet graph also utilizes the ui-bootstrap popover directive. What I'm trying to accomplish is to pass dynamically generated popover information from my data model into the popover. I already modified the popover template to accept html-unsafe, so that's taken care of. However, I'm seeing weird behavior inside my directive:

    .directive( 'bulletGraph', function ($timeout) {
        return {
            restrict: 'E',
            replace: true,
            scope: {
                model: '=',
            },
            link: function(scope, element, attrs) {
                $timeout(function() {
                    console.log(scope.model);
                },1000);
            },

A bit of exposition: Each grid row contains a +/- toggle which collapses/expands the tree. This only manipulates the data model bound to the ng-grid, so the grid automatically updates itself. However, there seems to be some weird timing issue. When I expand a node, the base templateURL is showing the correct info from the isolate scope 'model', but the data I'm attempting to manipulate inside the link function is returning the wrong model info - it's showing the info from the model that previously occupied that row index -- hence the $timeout, which doesn't seem to solve anything. Any idea how to get around this, or if it's a problem with ng-grid?

davidagee avatar Jul 02 '13 03:07 davidagee

In short, the model defined in the isolate scope declaration is not the same model that i'm logging to the console inside the link function.

davidagee avatar Jul 02 '13 03:07 davidagee

Can you show an example in a plunker so I can work through it? Thanks.

jonricaurte avatar Jul 03 '13 05:07 jonricaurte

I believe I am seeing the same issue. If you use the grid's built-in search filter and type in 2, you will see that the row.entity is not in sync with the directive's output. I modified the directive somewhat to demonstrate...

  .directive('bulletGraph', function ($timeout) {
    return {
      restrict: 'E',
      replace: true,
      scope: {
        model: '='
      },
      template: '<span>{{ id }}</span>',
      link: function (scope, element, attrs) {
        scope.id = scope.model.id;
      }}});

Using the following grid options...

      $scope.gridOptions = {
        showFilter: true,
        data: '[{id:1},{id: 2},{id:3}]',
        columnDefs: [{field: 'id', displayName: 'Id', 
          cellTemplate: '<span data-ng-cell-text>{{ row.entity }}&nbsp;<bullet-graph model="row.entity"></bullet-graph></span>'}],
      };

kaerfredoc avatar Sep 20 '13 04:09 kaerfredoc

This looks like it was solved for you in #743?

roblarsen avatar Oct 09 '13 18:10 roblarsen