简体   繁体   English

如何使用angularJS筛选选定的复选框

[英]How to filter the selected checkbox using angularJS

Listed all the array of object as a lists using ng-repeat and added checkbox for each value. 使用ng-repeat将所有对象数组列为列表,并为每个值添加复选框。

Here I want to filter the checkbox and form a new JSON on click Apply Filter based on check/uncheck the value(Checkbox's). 在这里,我要过滤复选框,并在基于“检查/取消选中”值(“复选框”)的“ 应用过滤器”上单击以形成新的JSON。

Actual 实际

I've tried adding the selected and unselected checkbox in the models scope object($scope.models) by concatenating the filter name and its values 我尝试通过串联过滤器名称及其值在模型范围对象($ scope.models)中添加选定和未选定的复选框

(ie) : data1(filter name)+1(value) = data11 (即):data1(过滤器名称)+1(值)= data11

On clicking apply filter looping the existing filters array and compare with the model object and push the matches in the new array. 单击应用过滤器后,循环现有过滤器数组,并与模型对象进行比较,然后将匹配项推入新数组中。

Plunker Plunker

Apply Filter function 应用过滤功能

HTML HTML

<li ng-repeat="(key, filter) in filters">
<a href="" data-target="#{{filter.name}}" data-toggle="collapse" aria-expanded="{{enableShow.indexOf(filter.name) > -1 ? true : false}}">
    {{filter.name}}
</a>
<ul class="collapse list-unstyled" id="{{filter.name}}" ng-class="{show : enableShow.indexOf(filter.name) > -1}">
    <li ng-repeat="v in filter.values">
        <label class="w-100" ng-show="filter.type == 'CheckBox'">
            <span class="ml-3 p-1 d-block">
                {{v.value}}
                <input type="checkbox" ng-model="models[filter.name + v.value]" class="pull-right mt-1 ml-1" style="margin-right: 7%" />
            </span>
        </label>
    </li>
</ul>

JS JS

$scope.applyFilters = function() {
var arr = [];
_.map($scope.filters, function(d) {
    _.map(d.values, function(v) {
        var name = d.name + v.value;
        for (key in $scope.models) {
            if (key == name) {
                arr.push({
                    name: d.name,
                    values: v
                });
            }
        }
    });
});
console.log(arr);
};

Expected 预期

On clicking apply filter want to form a new JSON which contains only the selected values in its respective object. 单击应用过滤器后,要形成一个新的JSON,该JSON仅在其各自的对象中包含选定的值。

{
"results": [
    {
        "name": "data1",
        "type": "CheckBox",
        "values": [
            {
                "value": "1"
            },
            {
                "value": "4"
            }
        ]
    },
    {
        "name": "data2",
        "type": "CheckBox",
        "values": [
            {
                "value": "1"
            }
        ]
    },
    {
        "name": "data5",
        "type": "CheckBox",
        "values": [
            {
                "value": "3"
            }
        ]
    },
    {
        "name": "data6",
        "type": "CheckBox",
        "values": [
            {
                "value": "2"
            }
        ]
    }
]
}

Thanks in advance. 提前致谢。

You could use obj.hasOwnProperty(prop) to contains only the selected values in its respective object. 您可以使用obj.hasOwnProperty(prop)在其各自的对象中仅包含选定的值。

Just change your applyFilters to this: 只需将您的applyFilters更改为此:

 $scope.applyFilters = function() {
    var arr = [];
    _.map($scope.filters, function(d) {
      _.map(d.values, function(v) {
        var name = d.name + v.value;
        for (key in $scope.models) {
           if (key == name && $scope.models.hasOwnProperty(key) && $scope.models[key]) {
            arr.push({
              name: d.name,
              values: v
            });
          }
        }
      });
    });
    console.log(arr);
  };

plunker: https://next.plnkr.co/edit/waIwyXgEkdfPtybq 朋克: https ://next.plnkr.co/edit/waIwyXgEkdfPtybq

Hope it helps! 希望能帮助到你!

Iterate the original data structure with _.map() and transform it to the requested format. 使用_.map()迭代原始数据结构,并将其转换为请求的格式。 Use _.filter() or _.reject() to remove sections without selected values ( plunker ): 使用_.filter()_.reject()删除没有选定值的节( plunker ):

$scope.applyFilters = function() {
  var arr = _($scope.filters)
    .map(function(o, i) {
      var section = _.pick(o, ['name', 'type']);

      section.values = _(o.values)
        .filter(function(v, j) {
          return $scope.models['data' + (i + 1) + (j + 1)];
        })
        .map(_.partialRight(_.pick, 'value'))
        .value();

      return section;
    })
    .reject(function(o) { return _.isEmpty(o.values); })
    .value();

  console.log(arr);
};

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

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