Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 防止用户在多个下拉列表中选择相同的值_Angularjs - Fatal编程技术网

Angularjs 防止用户在多个下拉列表中选择相同的值

Angularjs 防止用户在多个下拉列表中选择相同的值,angularjs,Angularjs,我从API调用加载一个表,表行是动态的,它基于API调用返回的值。我显示的排序顺序和值应该是唯一的,用户不应该选择以前选择的值。我试着按照这个()来做,但我无法做到,因为我的是动态的 我试过这个() editor.controller('EditorController',函数($scope){ $scope.entities=[{name:“pencil”,sortOrder:”},{name:“notepad”,sortOrder:”}, {名称:“书架”,分拣员:} ]; $scope.s

我从API调用加载一个表,表行是动态的,它基于API调用返回的值。我显示的排序顺序和值应该是唯一的,用户不应该选择以前选择的值。我试着按照这个()来做,但我无法做到,因为我的是动态的

我试过这个()

editor.controller('EditorController',函数($scope){
$scope.entities=[{name:“pencil”,sortOrder:”},{name:“notepad”,sortOrder:”},
{名称:“书架”,分拣员:}
];
$scope.sortOrderValues=[1,2,3];
});
{{x.name}
*需要排序顺序
如何防止用户使用angular js在每行中选择相同的排序顺序?

这可能会对您有所帮助

首先,从1到
entities.length生成一个数组(本例为3)。
选择选项时,t触发
选项Selected
功能。此函数将生成初始数组,并计算
实体使用的
排序器。然后从第一个数组中过滤第二个数组

HTML

<div ng-controller="EditorController">
  <table>
    <tr ng-repeat="x in entities">
      <td>{{ x.name }}</td>
      <td><select ng-model="x.sortOrder"
                  ng-options="col for col in sortOrderValues"
                  ng-change="optionSelected()"> 
         </select>
            <span ng-show="!x.sortOrder"> * sort order required</span>  
      </td>

    </tr>
  </table>
</div>

我能想到的最佳解决方案是将sortOrderValues列表的副本放在表中显示的每个实体上。然后,我认为每次在其中一个下拉列表中更改值时,您都必须遍历这些实体,并更新每个下拉列表以获得正确的选项。您可以只选择一个控制选择的主集合。然后,当您选择一个时,将该值添加到集合中。将NG选项更改为NG repeat,并根据集合中的值添加NG disable。您可以根据是否要隐藏或禁用选项来选择ngif。我猜是用户的选择。感谢您的帮助,一旦选择了值,它将从列表中删除,并且选择的值将消失,这不是预期的结果。因此,您希望将已选择的值保留在列表中,但不想选择它们?是的,我将在此处发布它。谢谢你的帮助
<div ng-controller="EditorController">
  <table>
    <tr ng-repeat="x in entities">
      <td>{{ x.name }}</td>
      <td><select ng-model="x.sortOrder"
                  ng-options="col for col in sortOrderValues"
                  ng-change="optionSelected()"> 
         </select>
            <span ng-show="!x.sortOrder"> * sort order required</span>  
      </td>

    </tr>
  </table>
</div>
editor.controller('EditorController', function($scope) {

  $scope.entities = [{name:"pencil",sortOrder:""} ,{name:"notepad",sortOrder:""} ,
  {name:"bookshelf",sortOrder:""}
  ];

  // Genereate all the numbers between 1 and $scope.entities.length
  $scope.sortOrderValues= $scope.entities.map(
    function (item, index) {
      return index + 1;
    }
  );

  // Function executed when you select a sortOrder
  $scope.optionSelected = function () {
    // Genereate all the numbers between 1 and $scope.entities.length
    var allIndexes = $scope.entities
      .map(function (entity, index) { return index + 1; });

    // Get all the sortOrder used
    var usedIndexes = $scope.entities
      .map(function(e) { return e.sortOrder; });

    // Remove from the [1, .., $scope.entities.length] array all the sortOrder used
    $scope.sortOrderValues = allIndexes
      .filter(function (order) {
        return !usedIndexes.find(function(index) { return index === order; });
      });
  }
});