$scope.data = [{
'id': '1',
'itemName': 'mousepad',
'price': '20'
}, {
'id': '2',
'itemName': 'keyboard',
'price': '20'
}, {
'id': '3',
'itemName': 'charger',
'price': '20'
}]
$scope.checkedTrue = function(h) {
$scope.demoArr = [];
$scope.demo = [];
$scope.demoArr.push(h);
function check() {
var deee = $.grep($scope.demoArr, function(record) {
console.log('iijjdkkdkkdkd======>', record);
return record
});
$scope.checkDemo = deee;
}
check();
$scope.demo.push($scope.checkDemo);
};
<table class="table m-t-30">
<thead>
<tr>
<th>#</th>
<th>ItemName {{selected}}</th>
<th>ItemPrice</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="tic in data track by $index">
<td>
<input id="checkbox2" type="checkbox" ng-model="selected[tic.id]" ng-change="checkedTrue(tic)"/>
</td>
<td><span>{{tic.itemName}}</span></td>
<td><span>{{tic.price}}</span></td>
</tr>
</tbody>
</table>
我在ng-repeat中有这个总数组:
var data = [{
'id': '1',
'itemName': 'mousepad',
'price': '20'
}, {
'id': '2',
'itemName': 'keyboard',
'price': '20'
}, {
'id': '3',
'itemName': 'charger',
'price': '20'
}];
所以我每行都有复选框,所以如果用户选中复选框我需要特定的选中行对象并形成数组
例如:
仅检查对象数据到另一个数组
var Checked = [{
'id': '1',
'itemName': 'mousepad',
'price': '20'
},{
'id': '3',
'itemName': 'charger',
'price': '20'
}];
如果用户取消选中我想从已检查数组
中删除该对象如果再次检查,则该对象再次添加到Checked数组
答案 0 :(得分:2)
1 - 使用ng-click
并触发功能
2 - 创建一个已选中复选框数组
3 - 如果有任何复选框更改,请检查数组是否存在项目并在数组中添加或删除
这里有一个简单的例子
var app = angular.module("myApp", []);
app.controller("myCtrl", function($scope) {
$scope.data = [{
id: "1",
itemName: "mousepad",
price: "20"
},
{
id: "2",
itemName: "keyboard",
price: "20"
},
{
id: "3",
itemName: "charger",
price: "20"
}
];
$scope.tempModel = '';
$scope.checkedArray = [];
$scope.updateCheckArray = function(itm) {
let index = $scope.checkedArray.findIndex(function(r) {
return r.id == itm.id;
});
if (index == -1) {
$scope.checkedArray.push(itm);
} else {
$scope.checkedArray.splice(index, 1);
}
};
});
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.js"></script>
<body>
<div ng-app="myApp" ng-controller="myCtrl">
<hr>
<div class="post-content" ng-repeat="d in data">
<input ng-click="updateCheckArray(d)" type="checkbox" ng-model="tempModel" />{{d.itemName}}
<hr>
</div>
{{checkedArray}}
</div>
</body>
</html>
<强>更新强>
1 - 将功能正文更改为此
$scope.checkedTrue = function(itm) {
let index = $scope.checkedArray.findIndex(function(r) {
return r.id == itm.id;
});
if (index == -1) {
$scope.checkedArray.push(itm);
} else {
$scope.checkedArray.splice(index, 1);
}
};
2 - 为选定的空模型添加空数组
$scope.tempModel = "";
$scope.checkedArray = [];
您自己代码的更改:
var app = angular.module("myApp", []);
app.controller("myCtrl", function($scope) {
$scope.data = [{
id: "1",
itemName: "mousepad",
price: "20"
},
{
id: "2",
itemName: "keyboard",
price: "20"
},
{
id: "3",
itemName: "charger",
price: "20"
}
];
$scope.tempModel = "";
$scope.checkedArray = [];
$scope.checkedTrue = function(itm) {
let index = $scope.checkedArray.findIndex(function(r) {
return r.id == itm.id;
});
if (index == -1) {
$scope.checkedArray.push(itm);
} else {
$scope.checkedArray.splice(index, 1);
}
};
});
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.js"></script>
<body>
<div ng-app="myApp" ng-controller="myCtrl">
<hr>
<table class="table m-t-30">
<thead>
<tr>
<th>#</th>
<th>ItemName {{selected}}</th>
<th>ItemPrice</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="tic in data track by $index">
<td>
<input id="checkbox2" type="checkbox" ng-model="tempModel" ng-change="checkedTrue(tic)" />
</td>
<td><span>{{tic.itemName}}</span></td>
<td><span>{{tic.price}}</span></td>
</tr>
</tbody>
</table>
{{checkedArray}}
</div>
</body>
</html>
答案 1 :(得分:0)
你可以检查小提琴手,我已经实现了你需要的东西
<div ng-app>
<h2>Todo</h2>
<div ng-controller="TodoCtrl">
<ul>
<li ng-repeat="item in data">
<input type="checkbox" value="{{item.id}}" ng-click="addTo($event)">
{{item.itemName}}
</li>
</ul>
<br>
<h4>
Added Array
</h4>
{{itemArray}}
</div>
</div>
function TodoCtrl($scope) {
$scope.data = [{
'id': '1',
'itemName': 'mousepad',
'price': '20'
}, {
'id': '2',
'itemName': 'keyboard',
'price': '20'
}, {
'id': '3',
'itemName': 'charger',
'price': '20'
}];
$scope.itemArray = [];
$scope.addTo = function(event){
if(event.currentTarget.checked){
$scope.itemArray.push(this.item)
}else{
$scope.itemArray.pop(this.item)
}
}
}