angular.js 实现单选和全选,将userId返给后台

时间:2021-11-10 19:37:04

html:

<input type="checkbox" ng-model="z.checked" ng-click="checkd()"/> // 单选框

<input type="checkbox" id="z.select_all" ng-model="select_all" ng-click="all()" />
<label for="select_all">全选</label>  // 全选框

js:

// 单选
  vm.checkd = function() {
    vm.choseArr = [];
    angular.forEach(vm.dataset, function (x) {
      var index = vm.choseArr.indexOf(x.userId);
      if (x.checked && index === -1) {
        vm.choseArr.push(x.userId);
      } else if (!x.checked && index !== -1) {
        vm.choseArr.splice(index, 1);
      };
      })

      if (vm.dataset.length === vm.choseArr.length) {
        vm.select_all = true;
      } else {
        vm.select_all = false;
      }
    };

// 全选
    vm.all = function() {
      if (vm.select_all) {
        vm.choseArr = [];
        angular.forEach(vm.dataset, function (x) {
          x.checked = true;
          vm.choseArr.push(x.userId);
        })
      } else {
        angular.forEach(vm.dataset, function (x) {
          x.checked = false;
          vm.choseArr = [];
        })
      }
    };