search

Home  >  Q&A  >  body text

angular.js - angularjs如何检测多个是否被选中,并收集其所在行数据的id

我知道jquery可以操作dom去检查,然后获取。但是angularjs刚学,不怎么会。如下:
这是在thead的

<td><input type="checkbox" ng-model="selectAll">

这是在tbody的:

<td><input ng-checked="selectAll" value="{{ x.id }}" name="beDel" type="checkbox"/></td>

这是图示:

我的问题是:怎么判断这些checkbox的选中,并且把选中的value值放到一个集合中去?

精通angularjs教一下。

阿神阿神2745 days ago715

reply all(2)I'll reply

  • 某草草

    某草草2017-05-15 17:03:07

    I just have experience, so let me think about it differently:
    1: The select all button is a checkbox, and the other sub-buttons are lists of multiple checkboxes;
    2: The checkbox of the sub-button is also bound to the checked value of the object when repeating (the default is / Does not exist/i.e. flase/i.e. not selected)
    3: Add two extension methods to the array

    (1):// 设置元素及子元素的选择状态(可关联自身所有子级)
        Array.prototype.setCheck = function (value, children) {
          var set_check = function (main) {
            for (var i = 0; i < main.length; i++) {
              main[i].checked = value;
              if (children && main[i][children]) {
                set_check(main[i][children]);
              }
            }
          };
          set_check(this);
        };
        
     (2):// 将元素中所有级别子元素中被选中的元素的id打包为一个数组
        Array.prototype.checked = function (children, id_key) {
        
          var ids = [];
          var find_check;
        
          if (arguments.length < 2) {
            find_check = function (main) {
              for (var i = 0; i < main.length; i++) {
                if (main[i].checked) {
                  ids.push(main[i].id);
                };
                if (!!children && main[i][children]) {
                  find_check(main[i][children]);
                }
              }
            };
          } else {
            find_check = function (main) {
              for (var i = 0; i < main.length; i++) {
                if (main[i].checked) {
                  ids.push(main[i][id_key]);
                };
                if (!!children && main[i][children]) {
                  find_check(main[i][children]);
                }
              }
            };
          };
        
          find_check(this);
          return ids;
        };

    4: Bind the extension event (1) to the select all button, and use the extension event (2) to get the id of the selected element

    Demo:

    // js
    $scope.list = [
        {
            id: 1,
            children: [...]
        }, {
            id: 2,
            children: [...]
        },
        ...
    ];
    
    < !-- html/check-all -- >
    <label>
        <input class="check-all" 
               type="checkbox"
               ng-model="check_all"
               ng-change="list.setCheck(check_all, 'children')">
        <span></span>
    </label>
    
    < !-- html/check-item -- >
    <li class="checkbox" ng-repeat="item in list">
         <label>
            <input type="checkbox" 
                   name="item[{{ $index }}]" 
                   ng-model="item.checked"
                   ng-change="item.children.setCheck(item.checked, 'children')"> 
                   < !-- 如果是只有一级数据的话,此处无需再绑定自身子级 -- >
         </label>
    </li>
    

    Get the selected value:

    // 一级数据
    $scope.checked_ids = $scope.list.checked(); // return [1,32,4,...]
    // 多级别数据
    $scope.checked_ids = $scope.list.checked('children'); // return [1,32,4,...]
    
    // 如果需要转字符串
    $scope.checked_ids = $scope.list.checked('children').join(','); // return "1,32,4"

    reply
    0
  • 習慣沉默

    習慣沉默2017-05-15 17:03:07

    http://stackoverflow.com/questions/12648466/how-can-i-get-angular-js-checkboxes-with-select-unselect-all-functionality-and-i

    http://stackoverflow.com/questions/14514461/how-to-bind-to-list-of-checkbox-values-with-angularjs

    reply
    0
  • Cancelreply