AngularJS-带数字/长度的过滤器复选框

时间:2018-07-13 11:06:09

标签: angularjs filter radio

我有一个可以搜索各种票证的应用程序。 在边栏中,我有一些过滤器,以便用户可以过滤结果(包括复选框和单选按钮,例如eBay / Amazon等)。

因此,它在侧栏中看起来与此类似:

如何购买

直接付款(10)
现金(2)


因此,用户一眼便知道,如果他们单击直接付款,它将返回10个结果。然后我遇到的问题是,当用户单击“直接付款”时,单选按钮将更改为此...。

如何购买

直接付款(10)
现金(0)

如果这是一个复选框,这将是正确的,因为单击直接借记和现金将返回0个结果。但是,在这种情况下(单选按钮),如果用户单击Cash,仍然应该看到2个结果。

这是我的代码...

HTML

<ul class="checkbox-list nga-fast nga-collapse" data-ng-show="ticketing.filterButtons.OperatorBtn">
  <li data-ng-repeat="filter in ticketing.filterButtons.Operator | orderBy: filter" data-ng-if="filter != null">
    <input id="operator{{$index}}" name="operator{{$index}}" type="radio" class="stylized" data-ng-model="ticketing.searchFilters.Operator" data-ng-change="ticketing.update()" value="{{filter}}" />
    <label for="operator{{$index}}">{{filter}} 
      ({{ (ticketing.filteredTickets | filter: {Operator: filter}).length}})
    </label>
  </li>
</ul>

JS

function update() {
  var filtered = vm.all;
  console.log(vm.searchFilters);
  // For each filter in the search filters loop through and delete any that state false, this is so it doesn't explicitly match false and shows everything.
  angular.forEach(vm.searchFilters, function(val, key) {
    // if Key/Property contains 'Allow" and the value is true || if Key/Property doesn't contain 'Allow' and val is false (this is to make sure the oppposite/exclude filter values are deleted as the trues will be falses and vice versa)
    if ((key.indexOf('Allow') !== -1 && val) || (val == false && key.indexOf('Allow') === -1)) {
      // Delete the filter and value
      delete vm.searchFilters[key];
    }
  });

  // Filter results by the filters selected
  filtered = $filter('filter')(filtered, vm.searchFilters);

  // Sort results by selected option
  vm.filteredTickets = $filter('orderBy')(filtered, vm.orderBy);

  console.log(vm.filteredTickets);
  console.log(vm.searchFilters);

  vm.updateGrid();
}

如果您需要更多信息或代码,请告诉我,我会提供。

1 个答案:

答案 0 :(得分:0)

这是一个猜测。在下面的行中可能出了些问题

     ({{ (ticketing.filteredTickets | filter: { Operator:filter}).length}})           

点击Direct Debit";时,您可能仅用“直接付款”数据更新了“ ticketing.filteredTickets”,因此没有“现金”数据