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

Create Default Cell Template for Dynamically created column

Open aalokits opened this issue 9 years ago • 0 comments

I have a code as follows

$scope.gridOptions.columnDefs = [ { name: 'id', displayName: 'No.', width: '5%', cellTemplate: '

{{grid.renderContainers.body.visibleRowCache.indexOf(row) + 1}}
'}, { name: 'status', displayName: 'Status',editType: 'dropdown', width: '10%', editableCellTemplate: 'ui-grid/dropdownEditor',
editDropdownValueLabel: 'status', editDropdownIdLabel: 'status',editDropdownOptionsArray: $scope.statusValues, default : '', cellClass: function(grid, row, col, rowRenderIndex, colRenderIndex) { if (grid.getCellValue(row,col) === 'Not Started') { return 'grey'; } else if (grid.getCellValue(row,col) === 'On Track') { return 'green'; } else if (grid.getCellValue(row,col) === 'Off Track - with Mitigation') { return 'yellow'; } else if (grid.getCellValue(row,col) === 'Off Track - No Mitigation') { return 'red'; } else if (grid.getCellValue(row,col) === 'Complete') { return 'blue'; } } }, { name: 'owner', displayName: 'Owner', width: '10%', default : ''}, { name: 'activity', displayName: 'Activity', width: '40%', default : ''}, { name: 'dueDate', displayName: 'Due Date' , type: 'date', cellFilter: 'date:"yyyy-MM-dd"', width: '10%', default : '' }, ];

    var maxIndex= ($scope.gridOptions.data.map(function (record, index) {
      return {len: Object.keys(record).length, ind: index};
    })).sort(function (elem1, elem2) {
      if (elem1.len < elem2.len)
        return 1;
      else if (elem1.len === elem2.len)
        return 0;
      else 
        return -1;
    })[0].ind;

      //var codeTemplate = '<div ng-if="row.entity.code">{{row.entity.code}}</div>' +'<div ng-if="!row.entity.code">My Default Value for code column</div>';


    for (var i=6; i<Object.keys($scope.gridOptions.data[maxIndex]).length; i++) {
      var colValue = Object.keys($scope.gridOptions.data[maxIndex])[i];
      $scope.gridOptions.columnDefs.push ({ name: colValue, field: colValue, displayName: colValue,editType: 'dropdown',
       width: '10%', editableCellTemplate: 'ui-grid/dropdownEditor', editDropdownValueLabel: 'status', editDropdownIdLabel: 'status', editDropdownOptionsArray: $scope.statusValues , 

      cellTemplate: '<div>"row.entity["'+colValue+'"] == "" ? "" : row.entity["'+colValue+'"]"</div>',
      cellClass: function(grid, row, col, rowRenderIndex, colRenderIndex) {
            if (grid.getCellValue(row,col) === 'Not Started') {
              return 'grey';
            }
            else if (grid.getCellValue(row,col) === 'On Track') {
              return 'green';
            }
            else if (grid.getCellValue(row,col) === 'Off Track - with Mitigation') {
              return 'yellow';
            }
            else if (grid.getCellValue(row,col) === 'Off Track - No Mitigation') {
              return 'red';
            }
            else if (grid.getCellValue(row,col) === 'Complete') {
              return 'blue';
            }
          }});
    };

In the Above code inside the for loop i am dynamically creating the columns in ui grid. there are many instances where i can have empty cell values, or no value but i want empty cell to show some default value, for this i am trying to use cell template. the default value can be " " --space--.

I was following this plunker but was not able to access my cell as it was created dynamically.

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

Please help.

aalokits avatar Jul 21 '17 14:07 aalokits