简体   繁体   English

选择全部复选框以选择嵌套角度ng重复复选框?

[英]Select All checkbox to select nested angular ng-repeat checkboxes?

I have a series of nested ng-repeat checkboxes, where it's a structure like: 我有一系列嵌套的ng-repeat复选框,它的结构如下:

  • Checkbox 复选框
    • country 国家
    • country 国家
    • select all 全选
  • Checkbox 复选框
    • country 国家
    • country 国家
    • select all 全选

The "select all" in each group should select all "country" checkboxes in that group. 每个组中的“全选”应选择该组中的所有“国家/地区”复选框。

You can see it in action here: http://plnkr.co/edit/rBIv9lfOr5MASPia7ORS?p=preview 你可以在这里看到它: http//plnkr.co/edit/rBIv9lfOr5MASPia7ORS?p=preview

The inner checkboxes are generated by an array in the main json: 内部复选框由主json中的数组生成:

  $scope.carriers = 
  [{
    "name": "UPS",
    "selected": false,
    "hasPaymentOptions": false,
    "isSelectable": true,
    "value": "",
    "countries": [
        "USA","UK","Canada","Germany","Australia"
     ]
   },etc

And are in a nested ng-repeat: 并且是嵌套的ng-repeat:

<!-- main list of carrier checkboxes -->
    <div ng-repeat="c in partnerent.carriers.carriers" class="spacer-top-md spacer-bottom-md">
      <div class="carrier spacer-bottom-sm">
        <div class="checkbox-inline">
          <label>
            <input type="checkbox" name="{{c.name | slug}}" ng-model="c.selected"> {{c.name}}
          </label>
        </div>
        <!-- inner checkboxes for countries -->
        <ul class="entitlement-countries list-inline">
          <li ng-repeat="ec in c.countries">
            <div class="checkbox-inline">
              <label>
                <input type="checkbox" name="{{ec | slug}}"> {{ec}}
              </label>
            </div>
            </li>
            <li ng-if="c.countries.length > 0">
              <div class="checkbox-inline">
                <label>
                  <input type="checkbox" name="{{ec | slug}}_selectall"> Select all
                </label>
              </div>
            </li>
        </ul>

It all works, but my question is how to have those "select all" checkboxes select their group of countries? 这一切都有效,但我的问题是如何让那些“全选”复选框选择他们的国家组? Most of the solutions and directives I've found involve the model and are generally for one group of checkboxes, where I have several inside a ng-repeat. 我发现的大多数解决方案和指令都涉及模型,并且通常用于一组复选框,其中我有几个复选框内。

Use ng-checked directive to select all the checkboxes. 使用ng-checked指令选择所有复选框。

<ul class="entitlement-countries list-inline">
  <li ng-repeat="ec in c.countries">
    <div class="checkbox-inline">
      <label>
        <input type="checkbox" name="{{ec | slug}}" ng-checked="c.countries.selectAll"> {{ec}}
      </label>
    </div>
    </li>
    <li ng-if="c.countries.length > 0">
      <div class="checkbox-inline">
        <label>
          <input type="checkbox" name="{{ec | slug}}_selectall" ng-model="c.countries.selectAll"> Select all
        </label>
      </div>
    </li>
</ul>

Here is the updated plnkr: http://plnkr.co/edit/o2NOBmFYZMtfkBu4lTrs?p=preview 这是更新的plnkr: http ://plnkr.co/edit/o2NOBmFYZMtfkBu4lTrs?p = preview

Plunker of my answer: http://plnkr.co/edit/d4cV1VP5iPj58ePGvZNR 我回答的问题: http ://plnkr.co/edit/d4cV1VP5iPj58ePGvZNR

Note: I didn't change all of the carriers, just the first one in the Plunker 注意:我没有改变所有的载体,只是Plunker中的第一个载体

How I would approach this would be to have some sort of ng-change method on the selectAll & the child checkboxes: 我如何处理这个问题的方法是在selectAll和子复选框上使用某种ng-change方法:

<ul class="entitlement-countries list-inline">
    <li ng-repeat="ec in c.countries">
        <div class="checkbox-inline">
            <label>
                <input type="checkbox" name="{{ec.name | slug}}" ng-model="ec.selected" ng-change="checkAllCountriesSelected(c)"> {{ec}}
            </label>
        </div>
    </li>
    <li ng-if="c.countries.length > 0">
        <div class="checkbox-inline">
            <label>
                <input type="checkbox" name="{{c.name | slug}}_selectall" ng-model="c.selectAll" ng-change="checkAllCountriesSelected(c)"> Select all
            </label>
        </div>
    </li>
</ul>

And the data model would change slightly to: 数据模型会略有变化:

$scope.carriers = 
  [{
    "name": "UPS",
    "selected": false,
    "selectAll": false,
    "hasPaymentOptions": false,
    "isSelectable": true,
    "value": "",
    "countries": [
        { name: "USA", selected: false },
        { name: "UK", selected: false },
        { name: "Canada", selected: false },
        { name: "Germany", selected: false },
        { name: "Australia", selected: false }
     ]
   },etc

And I'd add the change methods: 我会添加更改方法:

$scope.checkCountryToggled = function checkCountryToggled (carrier) {
    var carrierCountriesSelected = getSelectedCountries(carrier);

    if (carrierCountriesSelected.length === carrier.countries.length) {
        carrier.selectAll = true;
    } else {
        carrier.selectAll = false;
    }
};

function getSelectedCountries (carrier) {
    var selectedCountries = filterFilter(carrier.countries, function (country) {
        return country.selected;
    });

    return selectedCountries || [];
}

$scope.checkSelectAllToggled = function (carrier) {
    if (carrier.selectAll) {
         selectAllCountries(carrier);
    }
};

function selectAllCountries (carrier) {
    angular.forEach(carrier.countries, function (country) {
        country.selected = true;
    });
}        

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM