選択した行を削除するボタンを実装する方法の例を探していましたが、これまでのところ有用なものは見つかりませんでした。
誰でも私にヒントを教えてもらえますか?これがプランカーの例です。
私が実装する関数は、他の行を削除するため、奇妙な動作をします。
ありがとうございました。
それは行を削除する適切な方法ではありません
このようにしてみてください:
$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>