1. 程式人生 > >全選、取消全選、單選

全選、取消全選、單選

選中項 else $scope put scope () font ont 時間

<!--html-->

<table class="table1">
<thead>
<tr>
<th> <input id="flag" type="checkbox" ng-model="select_all" ng-change="selectAll()"> </th>
<th>合同編號</th>
<th>合同名稱</th>
<th>分配公司</th>
<th>更新時間</th>


</tr>
</thead>
<tbody>
<tr ng-repeat="list in lists">
<td> <input type="checkbox" ng-model="list.checked" ng-change="selectOne()"> </td>
<td>{{list.contractnum}}</td>
<td>{{list.template.name}}</td>
<td>{{list.office.name}}</td>

<td>{{list.updateDate}}</td>
</tr>
</tbody>
</table>

<!--js-->

$scope.checked = [];//用來存放選中項的id
$scope.select_all = “”;
$scope.list = {
checked: “”
}

//全選
$scope.selectAll = function() {
if($scope.select_all) {
$scope.checked = [];
angular.forEach($scope.lists, function(i) {


i.checked = true;
$scope.checked.push(i.id);
})
} else {
angular.forEach($scope.lists, function(i) {
i.checked = false;
$scope.checked = [];
})
}
};

//單選
$scope.selectOne = function() {
angular.forEach($scope.lists, function(i) {
var index = $scope.checked.indexOf(i.id);
if(i.checked && index === -1) {
$scope.checked.push(i.id);
} else if(!i.checked && index !== -1) {
$scope.checked.splice(index, 1);
};
})

if($scope.lists.length === $scope.checked.length) {
$scope.select_all = true;
} else {
$scope.select_all = false;
}
}

全選、取消全選、單選