根据先前的选择筛选选项。

huangapple go评论79阅读模式
英文:

Filter select options based on previous selections

问题

以下是您提供的代码的翻译部分:

我在AngularJS中有一系列具有预定义值列表的动态选择选项。

基本上,当有人在第一个选择下拉菜单中选择一个值时,我想将其从其余的下拉菜单中移除。

<body ng-app="myNgApp">
  <div ng-controller="myController">
    <input type="button" value="Add group" ng-if="availableGroups < allGroups.length" ng-click="addGroup()" />
    <input type="button" value="Remove group" ng-click="removeGroup()" />

    <div ng-repeat="group in [].constructor(availableGroups) track by $index">
      <select ng-model="selectedGroups_[$index]" ng-options="col for col in allGroups"></select>
    </div>
  </div>
</body>
var ngApp = angular.module('myNgApp', []);

ngApp.controller('myController', function($scope) {
  $scope.allGroups = ['group 1', 'group 2', 'group 3', 'group 4', 'group 5'];
  $scope.availableGroups = 0;

  $scope.addGroup = function() {
    $scope.availableGroups += 1;
  }

  $scope.removeGroup = function($index) {
    $scope.availableGroups -= 1;
  }
});

您可以在这里找到JSFiddle链接

我尝试创建一个名为selectedOptions的数组,并使用ng-change将值推送或移除到该数组中。然后,使用它来过滤所有的select元素。然而,当我将值推送到数组中时,显然它会从已选择的那个中移除。

英文:

I have a dynamic number of select options in AngularJS with a predefined list of values.

Essentially, when someone choose one value on the 1st select dropdown, I want to remove it from the rest of the dropdown lists.

&lt;body ng-app=&quot;myNgApp&quot;&gt;
  &lt;div ng-controller=&quot;myController&quot;&gt;
    &lt;input type=&quot;button&quot; value=&quot;Add group&quot; ng-if=&quot;availableGroups &lt; allGroups.length&quot; ng-click=&quot;addGroup()&quot; /&gt;
    &lt;input type=&quot;button&quot; value=&quot;Remove group&quot; ng-click=&quot;removeGroup()&quot; /&gt;

    &lt;div ng-repeat=&quot;group in [].constructor(availableGroups) track by $index&quot;&gt;
      &lt;select ng-model=&quot;selectedGroups_[$index]&quot; ng-options=&quot;col for col in allGroups&quot;&gt;&lt;/select&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/body&gt;


var ngApp = angular.module(&#39;myNgApp&#39;, []);

ngApp.controller(&#39;myController&#39;, function($scope) {
  $scope.allGroups = [&#39;group 1&#39;, &#39;group 2&#39;, &#39;group 3&#39;, &#39;group 4&#39;, &#39;group 5&#39;];
  $scope.availableGroups = 0;

  $scope.addGroup = function() {
    $scope.availableGroups += 1;
  }

  $scope.removeGroup = function($index) {
    $scope.availableGroups -= 1;
  }
  

});

You can find the JSFiddle here

What I tried to do is to create an array selectedOptions and with an ng-change, push or remove the value to that array. Then use it to filter all select elements. However, the moment I push the value to the array, it is obviously removed from the one that got selected.

答案1

得分: 1

I think while removing also your array needs to be updated right? by replacing the removed selected option, so I have implemented that too here you can check this code or else I will attach my jsfiddle link here jsFiddle

var ngApp = angular.module('myNgApp', []);

ngApp.controller('myController', function($scope) {
  $scope.allGroups = ['group 1', 'group 2', 'group 3', 'group 4', 'group 5'];
  $scope.availableGroups = 0;
  $scope.selectedOptionsList = [];

  $scope.addGroup = function() {
    $scope.availableGroups += 1;
  };

  $scope.removeGroup = function() {
    $scope.availableGroups -= 1;
    $scope.selectedOptionsList.pop();
  };

  $scope.neededSelectableOptions = function(index) {
    var selectedValues = $scope.selectedOptionsList.filter(function(value, i) {
      return i !== index && value !== null && value !== undefined;
    });

    return $scope.allGroups.filter(function(group) {
      return !selectedValues.includes(group);
    });
  };
});
<body ng-app="myNgApp">
  <div ng-controller="myController">
    <input type="button" value="Add group" ng-if="availableGroups < allGroups.length" ng-click="addGroup()" />
    <input type="button" value="Remove group" ng-click="removeGroup()" />

    <div ng-repeat="group in [].constructor(availableGroups) track by $index">
      <select ng-model="selectedOptionsList[$index]" ng-options="col for col in neededSelectableOptions($index)"></select>
    </div>
  </div>
</body>
英文:

I think while removing also your array needs to be updated right? by replacing the removed selected option, so I have implemented that too here you can check this code or else I will attach my jsfiddle link here jsFiddle

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

var ngApp = angular.module(&#39;myNgApp&#39;, []);

ngApp.controller(&#39;myController&#39;, function($scope) {
  $scope.allGroups = [&#39;group 1&#39;, &#39;group 2&#39;, &#39;group 3&#39;, &#39;group 4&#39;, &#39;group 5&#39;];
  $scope.availableGroups = 0;
  $scope.selectedOptionsList = [];

  $scope.addGroup = function() {
    $scope.availableGroups += 1;
  };

  $scope.removeGroup = function() {
    $scope.availableGroups -= 1;
    $scope.selectedOptionsList.pop();
  };

  $scope.neededSelectableOptions = function(index) {
    var selectedValues = $scope.selectedOptionsList.filter(function(value, i) {
      return i !== index &amp;&amp; value !== null &amp;&amp; value !== undefined;
    });

    return $scope.allGroups.filter(function(group) {
      return !selectedValues.includes(group);
    });
  };
});

<!-- language: lang-html -->

&lt;body ng-app=&quot;myNgApp&quot;&gt;
  &lt;div ng-controller=&quot;myController&quot;&gt;
    &lt;input type=&quot;button&quot; value=&quot;Add group&quot; ng-if=&quot;availableGroups &lt; allGroups.length&quot; ng-click=&quot;addGroup()&quot; /&gt;
    &lt;input type=&quot;button&quot; value=&quot;Remove group&quot; ng-click=&quot;removeGroup()&quot; /&gt;

    &lt;div ng-repeat=&quot;group in [].constructor(availableGroups) track by $index&quot;&gt;
      &lt;select ng-model=&quot;selectedOptionsList[$index]&quot; ng-options=&quot;col for col in neededSelectableOptions($index)&quot;&gt;&lt;/select&gt;
    &lt;/div&gt;
  &lt;/div&gt;


&lt;/body&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月27日 19:48:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76564545.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定