【问题标题】:Angularjs: Check / Uncheck checkboxes of categories and list itemsAngularjs:选中/取消选中类别和列表项的复选框
【发布时间】:2019-06-12 11:06:16
【问题描述】:

我列出了我想要具有选中/取消选中功能的类别和列表项。单击类别的位置还应检查所有子复选框。我有一个列表(JSON 中的特权),其中某些项目具有相同的类别,如下所示;

我在 plunkr 中有示例代码 - https://next.plnkr.co/edit/au1XPEjj7MuP77gc

我有如下 JSON 代码。

var list = [{
      "uuid": "0023",
      "title": "AM_FULL_ACCESS",
      "displayName": "Application monitoring Full access",
      "status": "ACTIVE",
      "type": "ADMIN",
      "category": "Application Monitoring"
    },
    {
      "uuid": "0025",
      "title": "CM_FULL_ACCESS",
      "displayName": "Client management Full access",
      "status": "ACTIVE",
      "type": "ADMIN",
      "category": "Client Management"
    },
    {
      "uuid": "0031",
      "title": "COMPLIANCE_FULL_ACCESS",
      "displayName": "Compliance Full access",
      "status": "ACTIVE",
      "type": "ADMIN",
      "category": "Compliance"
    },
    {
      "uuid": "0054",
      "title": "FAILED_APPLICATION_ACCESS",
      "displayName": "Failed application access",
      "status": "ACTIVE",
      "type": "ADMIN",
      "category": "Compliance"
    },
    {
      "uuid": "0068",
      "title": "FUND_TRANSFER",
      "displayName": "Fund Transfer",
      "status": "ACTIVE",
      "type": "ADMIN",
      "category": "Funds"
    },
    {
      "uuid": "0067",
      "title": "FUND_LOADING",
      "displayName": "Fund Loading",
      "status": "ACTIVE",
      "type": "ADMIN",
      "category": "Funds"
    },
    {
      "uuid": "0066",
      "title": "FUND_LOADING_TRANSFER",
      "displayName": "Fund Loading and transfer",
      "status": "ACTIVE",
      "type": "ADMIN",
      "category": "Funds"
    }
];

为了将每个列表项置于特定类别下,我循环了 JSON 并使用 ng-if 指令对其进行过滤,以显示特定列表项是否具有相同的类别。

此处查看(HTML 代码)

<label>
    <input type="checkbox" id="" ng-model="IsAllChecked" ng-change="CheckUncheckAll()"> 
    <span class="ml-1">Application Monitoring</span>
</label>
<ul class="list-unstyled">
    <li ng-repeat="pr in privilegesList track by $index" ng-if="pr.category === 'Application Monitoring'">
        <label class="control-label">
            <input id="pr.title" type="checkbox" ng-model="pr.isActive" ng-change="CheckUncheckHeader()">
            <span>{{pr.displayName}}</span>
        </label>
    </li>
</ul>

<label>
    <input type="checkbox" id="" ng-model="IsAllChecked" ng-change="CheckUncheckAll()"> 
    <span class="ml-1">Client Management</span>
</label>
<ul class="list-unstyled">
    <li ng-repeat="pr in privilegesList track by $index" ng-if="pr.category === 'Client Management'">
        <label class="control-label">
            <input id="pr.title" type="checkbox" ng-model="pr.isActive" ng-change="CheckUncheckHeader()">
            <span>{{pr.displayName}}</span>
        </label>
    </li>
</ul>

<label>
    <input type="checkbox" id="" ng-model="IsAllChecked" ng-change="CheckUncheckAll()"> 
    <span class="ml-1">Compliance</span>
</label>
<ul class="list-unstyled">
    <li ng-repeat="pr in privilegesList track by $index" ng-if="pr.category === 'Compliance'">
        <label class="control-label">
            <input id="pr.title" type="checkbox" ng-model="pr.isActive" ng-change="CheckUncheckHeader()">
            <span>{{pr.displayName}}</span>
        </label>
    </li>
</ul>

<label>
    <input type="checkbox" id="" ng-model="IsAllChecked" ng-change="CheckUncheckAll()"> 
    <span class="ml-1">Funds</span>
</label>
<ul class="list-unstyled">
    <li ng-repeat="pr in privilegesList track by $index" ng-if="pr.category === 'Funds'">
        <label class="control-label">
            <input id="pr.title" type="checkbox" ng-model="pr.isActive" ng-change="CheckUncheckHeader()">
            <span>{{pr.displayName}}</span>
        </label>
    </li>
</ul>

这样做 ng-if 似乎工作正常,但我想用更好的解决方案(如果有的话)来做。我还想为所有类别及其各自的子项目添加检查/取消选中功能。因此,如果我检查任何类别,所有子复选框也将被选中。如果未选中任何/所有项目复选框,则应取消选中父复选框。

我关注的 JS 代码似乎可以用于检查/取消选中所有选项。我可以针对特定类别执行此操作,但我希望拥有针对所有类别及其子复选框执行此操作的脚本。

【问题讨论】:

    标签: javascript angularjs checkbox checkboxlist


    【解决方案1】:

    也许这个脚本有用。

    angular.module('someApp', [])
      .controller('someController', function someController($scope) {
        $scope.getCategories = getCategories;
        $scope.getPrivilegesByCategory = getPrivilegesByCategory;
        $scope.selectCategory = selectCategory;
        $scope.checkCategory = checkCategory;
        $scope.privileges = getCategories().reduce(
          function(acc, category, index) {
            acc.push({
              category: category,
              data: getPrivilegesByCategory(category)
            });
            return acc;
          }, []);
      });
    
    angular.bootstrap(
      document.body, ['someApp']
    );
    
    function getCategories() {
      var categories = privilegesService().reduce(
        function(acc, privilege) {
          if (acc.indexOf(privilege.category) === -1) {
            acc.push(privilege.category);
          }
          return acc;
        }, []);
      return categories;
    }
    
    function getPrivilegesByCategory(category) {
      var privileges = privilegesService().filter(
        function(privilege) {
          return privilege.category === category;
        });
      return privileges;
    }
    
    function selectCategory(item) {
      checkCategory(item);
      item.data.forEach(function(privilege) {
        privilege.isActive = !item.isActive;
      });
    }
    
    function checkCategory(item) {
      var res = item.data.filter(function(privilege) {
        return privilege.isActive !== true;
      });
      item.isActive = res.length === 0;
    }
    
    function privilegesService() {
      return [{
          "uuid": "0023",
          "title": "AM_FULL_ACCESS",
          "displayName": "Application monitoring Full access",
          "status": "ACTIVE",
          "type": "ADMIN",
          "category": "Application Monitoring"
        },
        {
          "uuid": "0025",
          "title": "CM_FULL_ACCESS",
          "displayName": "Client management Full access",
          "status": "ACTIVE",
          "type": "ADMIN",
          "category": "Client Management"
        },
        {
          "uuid": "0031",
          "title": "COMPLIANCE_FULL_ACCESS",
          "displayName": "Compliance Full access",
          "status": "ACTIVE",
          "type": "ADMIN",
          "category": "Compliance"
        },
        {
          "uuid": "0054",
          "title": "FAILED_APPLICATION_ACCESS",
          "displayName": "Failed application access",
          "status": "ACTIVE",
          "type": "ADMIN",
          "category": "Compliance"
        },
        {
          "uuid": "0068",
          "title": "FUND_TRANSFER",
          "displayName": "Fund Transfer",
          "status": "ACTIVE",
          "type": "ADMIN",
          "category": "Funds"
        },
        {
          "uuid": "0067",
          "title": "FUND_LOADING",
          "displayName": "Fund Loading",
          "status": "ACTIVE",
          "type": "ADMIN",
          "category": "Funds"
        },
        {
          "uuid": "0066",
          "title": "FUND_LOADING_TRANSFER",
          "displayName": "Fund Loading and transfer",
          "status": "ACTIVE",
          "type": "ADMIN",
          "category": "Funds"
        }
      ];
    }
    <link href="//maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>
    
    <div ng-controller="someController">
      <div ng-repeat="item in privileges">
        <label class="h5 mb-2">
            <input type="checkbox" ng-click="selectCategory(item)" ng-model="item.isActive"> 
            <span class="ml-1" ng-bind="item.category"></span>
        </label>
        <ul class="list-unstyled ml-4">
          <li ng-repeat="pr in item.data track by $index">
            <label class="control-label">
                    <input type="checkbox" ng-model="pr.isActive" ng-change="checkCategory(item)">
                    <span ng-bind="pr.displayName"></span>
                </label>
          </li>
        </ul>
      </div>
    </div>

    【讨论】:

    • 谢谢,我会实现这个并分享我的经验。
    猜你喜欢
    • 1970-01-01
    • 2014-05-04
    • 1970-01-01
    • 1970-01-01
    • 2015-02-12
    • 2013-11-26
    • 2019-01-14
    • 2017-04-14
    • 1970-01-01
    相关资源
    最近更新 更多