How do I add image in a angular ui-grid cell
Asked Answered
L

1

5

How do I add image in a angular ui-grid cell.

var app = angular.module('app', ['ngAnimate', 'ngTouch', 'ui.grid']);

   app.controller('MainCtrl', ['$scope', '$http', function ($scope, $http) {
     $scope.gridOptions = {
       enableSorting: true,
       columnDefs: [
         { field: 'name' },
         { field: 'company'  },
         { field: 'image'}
       ],
       data:[
         {name:"Name1",company:"Company1",image:"http://cdn.flaticon.com/png/256/70689.png"},
         {name:"Name2",company:"Company2",image:"http://cdn.flaticon.com/png/256/70689.png"},]
   };  
 }]);
Leggat answered 15/5, 2015 at 19:16 Comment(0)
N
13

You can use a custom cell template to render the image in the cell.

var app = angular.module('app', ['ngAnimate', 'ngTouch', 'ui.grid']);

app.controller('MainCtrl', ['$scope', '$http', function ($scope, $http) {
  $scope.gridOptions = {
    enableSorting: true,
    rowHeight:100,
    columnDefs: [
      { field: 'name' },
      { field: 'company'  },
      { field: 'image', cellTemplate:"<img width=\"50px\" ng-src=\"{{grid.getCellValue(row, col)}}\" lazy-src>"}
    ],
    data:[
      {name:"Name1",company:"Company1",image:"http://cdn.flaticon.com/png/256/70689.png"},
      {name:"Name2",company:"Company2",image:"http://cdn.flaticon.com/png/256/70689.png"},
      {name:"Name3",company:"Company3",image:"http://cdn.flaticon.com/png/256/70689.png"}
      ]
  };

}]);

Here is a working plnkr.

http://plnkr.co/edit/awQ7B0WmmZhythlCZmgt?p=preview

Numen answered 15/5, 2015 at 19:42 Comment(2)
How this line ng-src=\"{{grid.getCellValue(row, col)}}\" give you the picture ? what if you have multiple images fields how you choose one?Portia
what is the use of lazy-src attribute? is it a custom directive or something in any module?Julissajulita

© 2022 - 2024 — McMap. All rights reserved.