ng-grid how to show/hide button in a column cell for last row

Deadly 提交于 2019-12-01 12:39:11

问题


I have a ng-grid table and one of the column Defs has a cell template to show icon. Currently icon shows up across all rows.

Can anyone help me how to display icon for the last row in ng-grid.

I tried using ng-show='$last' and didn't work.

$scope.reasonsGrid = { 
         data: 'myReasons',                    
        columnDefs: [
            {field: 'add', displayName:'', cellTemplate: addTemplate,enableCellEdit: false,width:30},
          {field: 'code', displayName: 'Reasons',cellTemplate: dropdownTemplate,enableCellEdit: false}]
        };

var addTemplate ='span class="glyphicon glyphicon-plus-sign" ng-click="addItem(row.rowIndex)" ng-show="$last"/>';;

回答1:


Try this code:

  var ctpl = '<div ng-show="row.rowIndex==myData.length-1" class="ngCellText"><button ng-click="addItem()"">Add Item</button></div>';

  $scope.gridOptions = {
    data: 'myData',
    enableRowSelection:false,
    columnDefs: [{
      field: 'name',
      displayName: 'Name'
    }, {
      field: 'age',
      displayName: 'Age',
    }, {
      displayName: 'Action',
      cellTemplate: ctpl
    }]
  };

  $scope.addItem=function(){
    $scope.myData.push({name:'new',age:''});
  }

See it working here




回答2:


Try this sample out...use ng-show for hiding and showing

Working Demo

var myapp = angular.module('myapp', [ 'ngGrid' ]);
myapp.controller('MyCtrl', function($scope)
{
    $scope.editableInPopup = '<button id="editBtn" type="button" class="btn btn-primary" ng-click="edit(row)" >Edit</button> '

    $scope.edit = function edit(row)
    {
        console.log("Here I need to know which button was selected " + row.entity.name)
    }

    $scope.myData = [ {
        name : "Moroni",
        age : 50
    }, {
        name : "Tiancum",
        age : 43
    }, {
        name : "Jacob",
        age : 27
    }, {
        name : "Nephi",
        age : 29
    }, {
        name : "Enos",
        age : 34
    } ];
    $scope.gridOptions = {
        data : 'myData',
        columnDefs : [ {
            field : 'name',
            displayName : 'Name'
        }, {
            field : 'age',
            displayName : 'Age',
            editableCellTemplate : self.editableCellTempate,
            enableCellEdit : true
        }, {
            displayName : 'Popup',
            cellTemplate : $scope.editableInPopup
        } ]
    };
});



回答3:


You can use $index also.. Use ng-show/ng-hide and put the condiltion $index== $last.



来源:https://stackoverflow.com/questions/22828722/ng-grid-how-to-show-hide-button-in-a-column-cell-for-last-row

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