2016-11-09 38 views
0

在網格中的添加按鈕應該只在最後一個記錄,管理添加和使用電池模板

這是電池模板中使用的代碼

cellTemplate: '<button class="btn primary" ng-click="grid.appScope.deleteRow(row)">Delete</button>'+ 
    '<button class="btn primary" ng-click="grid.appScope.addRow(row)">Add</button>' 

這裏花掉http://plnkr.co/edit/vP8K039cwMsVymCdRelT?p=preview刪除UI的網格按鈕

在plunk中,所有行都具有按鈕,我怎樣才能從除最後一行以外的所有行中刪除添加按鈕?

如何使用網格行長度來管理?

回答

1

如果你得到gridApi的保持如下:

$scope.gridOptions.onRegisterApi = function(gridApi){ 
    //set gridApi on scope 
    $scope.gridApi = gridApi; 
} 

然後修改你的模板中添加按鈕,所以它有以下幾點:

ng-show="grid.appScope.isLast(row)" 

,並在定義一個isLast功能範圍:

$scope.isLast = function(row) { 
    return row.uid === $scope.gridApi.grid.renderContainers.body.visibleRowCache[$scope.gridApi.grid.renderContainers.body.visibleRowCache.length-1].uid; 
} 

此功能比較當前行的與最後一次渲染行的uid相比較,如果它是最後一行,則返回true;否則返回false。 ng-show根據此函數返回的值呈現「添加」按鈕。

這將僅在最後一行顯示Add按鈕。這也將作爲行添加/刪除(Add按鈕將保留在最後一行):

enter image description here

請參閱this Plunkr的一個例子。