javascript - 如何使用 "Select All"复选框传递所有对象?

标签 javascript angularjs

我创建了一个页面,用户可以在其中选择/取消选择员工。我正在使用 .push 将这些员工添加到数组 users 中。我有单个用户的复选框和一个用于选择/取消选择所有用户的复选框。选中和取消选中个别用户是可行的,但我坚持使用“全选”复选框将其绑定(bind)。

这是 HTML:

<div ng-app="myApp">
  <div ng-controller="MainCtrl">

    <label>Select All</label><br>
    <input type="checkbox" ng-model="all" ng-change="addremoveall(all, user.ID)">
    <table>
      <tr>
        <th></th>
        <th>User</th>
        <th>First Name</th>
        <th>Last Name</th>
      </tr>
      <tr ng-repeat="user in users">
        <td><input type="checkbox" ng-model="checked" ng-change="addremoveuser(checked, user.ID)"></td>
        <td>{{user.Title}}</td>
        <td>{{user.FirstName}}</td>
        <td>{{user.LastName}}</td>
      </tr>
    </table>
  </div>
</div>

这是JS:

var app = angular.module('myApp', ['ngSanitize']);
app.controller('MainCtrl', function($scope, $http, $q){
    var users = [];
    $scope.addremoveuser = function (checked, id) {
        if (checked) {
            console.log("user selected", id);
            users.push(id)
            console.log("if test", users);
        }
        else {
            console.log("user unselected", id);
            var index = users.indexOf(id);
            users.splice(index);
            console.log("else test", users);
        }
    };
    $scope.addremoveall = function (all, id) {
            if (all) {
                console.log("all selected", id);
                users.push(id)
                console.log("if test", users);
            }
            else {
                console.log("all unselected", id);
                var index = users.indexOf(id);
                users.splice(index);
                console.log("else test", users);
            }
    };

});

最佳答案

您必须将每个复选框与关联用户绑定(bind)。

尝试类似的东西

<div ng-app="myApp">
    <div ng-controller="MainCtrl">
    <label>Select All</label><br>
    <input type="checkbox" ng-model="globalChecked" ng-change="addremoveall()">
    <table>
      <tr>
        <th></th>
        <th>User</th>
        <th>First Name</th>
        <th>Last Name</th>
      </tr>
      <tr ng-repeat="user in users">
        <td><input type="checkbox" ng-model="user.checked" ng-change="addremoveuser(user)"></td>
        <td>{{user.Title}}</td>
        <td>{{user.FirstName}}</td>
        <td>{{user.LastName}}</td>
      </tr>
    </table>
  </div>
</div>

$scope.globalChecked = false;

$scope.addremoveuser = function (user) {
    if (user.checked) {
        $scope.users.push(user);
    } else {
        $scope.user.splice(user, 1);
    }
}

$scope.addremoveall = function () {
    for (let i in $scope.users) {
        $scope.users[i].checked = $scope.globalChecked;
    }
}

关于javascript - 如何使用 "Select All"复选框传递所有对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48325620/

相关文章:

javascript - 为所有 ng-repeat 分开 ng-repeat..?

javascript - ng-change 和自定义指令使范围变量在初始化时未定义

javascript - 使用 Javascript 收集鼠标在 Canvas 上的位置

javascript - 真的适合 "node.js"开发的 OS X 编辑器(或 IDE)?

javascript - 错误 : *. default is not a constructor

jquery - session 超时插件

javascript - Angularjs:为什么 1 个绑定(bind)有 3 个观察者?

javascript - 更新自定义指令 Controller 中的 $scope 时,Angular.js 双向绑定(bind)不起作用?

javascript - 如何区分 Javascript 中的关联数组和常规数组?

javascript - 调用一个获取 API 并呈现返回值的函数,Promises 出现问题