我一直在寻找有关如何实现按钮以删除选定行的示例,但到目前为止我找不到任何有用的东西。
谁能给我一个提示?这是plunker的例子。
我实现的功能很奇怪,因为它删除了其他行。
谢谢你。
这不是删除任何行的正确方法
试试这样:
$scope.removeRow = function() {
var index = this.row.rowIndex;
$scope.gridOptions.selectItem(index, false);
$scope.myData.splice(index, 1);
};
PLUNKER -->
它的工作和测试
感谢您的提示,但我尝试了该片段但它不起作用,所以我将其更改为
var removeTemplate = '<input type="button" value="remove" ng-click="removeRow()" />';
$scope.removeRow = function() {;
var index = this.row.rowIndex;
alert(index);
$scope.gridOptions.selectItem(index, false);
$scope.items.splice(index, 1);
};
它就像一个魅力:) 希望这有帮助。
这可能会对您有所帮助,这也是用于删除网格中的多行。
$scope.mySelections = [];
$scope.gridOptions = {
data :'gridData',
selectedItems : $scope.mySelections,
showSelectionCheckbox : true
}
$scope.deleteSelected = function() {
angular.forEach($scope.mySelections, function(rowItem) {
$scope.gridData.splice($scope.gridData.indexOf(rowItem),1);
});
}
mySelections 是已选择行的数组
对数组进行排序后,此问题的先前答案将不起作用,因为 row.index 会根据数组的排序方式发生变化,但数组中的原始数据仍保留在其原始索引中。我们必须在数据数组中找到正确的索引才能删除正确的行。该行包含对 row.entity 中原始数据的引用,因此我们可以使用 indexOf 找到正确的索引。
$scope.actionTemplate = '<input type="button" value="Delete" ng-click="delete($event);" />';
$scope.delete = function($event) {
$event.stopPropagation(); //keep the row from being selected
$scope.data.selectAll(false); //remove all selections: necessary for issues with the selection array
var index = $scope.data.indexOf(this.row.entity); //get the correct index to splice
$scope.metrics.splice(index, 1); //remove the element
};
编辑:最初的解决方案当时可能有效,但 ng-grid 已经更新,不再有效。
它可能会帮助你
<!doctype html>
<html ng-app="deleteApp">
<head>
<title>Example - www.code-sample.com</title>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.0-rc.1/angular.min.js"></script>
<script>
angular.module('deleteApp', [])
.controller('deleteController', ['$scope', function ($scope) {
$scope.Rows = [
{ row: 'RowCount1'},
{ row: 'RowCount2'},
{ row: 'RowCount3'},
{ row: 'RowCount4'},
{ row: 'RowCount5'}];
$scope.delete = function(index){
$scope.Rows.splice(index, 1);
}
}]);
</script>
</head>
<body ng-controller="deleteController">
<div ng-repeat="ro in Rows">
<div>{{$index + 1}} : {{ro.row}} <input type="button" value="delete" ng-click="delete($index)" /></div>
</div>
</body>
</html>
这有效:
showSelectionCheckbox : true -> 将复选框添加到网格和 $scope.delItem = function() -> 它适用于多行或单行选择
$scope.mySelections = [];
$scope.gridOptions = {
data :'data',
selectedItems : $scope.mySelections,
showSelectionCheckbox : true
}
$scope.delItem = function() {
for (var i = 0; i < $scope.mySelections.length; i++) {
var index = $scope.data.indexOf($scope.mySelections[i]);
if (index != -1) {
$scope.data.splice(index, 1);
}
}
}