2017-01-29 18 views
1

複数の選択ボックスのオプションとして優先度(1,2,3)の配列があります。優先度のそれぞれを選択できるのは1回だけです。したがって、1つの選択ボックス '2'が選択されている場合は、他の選択ボックスでは削除または無効にする必要があります。 フィルタhereが見つかりましたが、私のコードで動作するようにはなりませんでした。他の選択に基づくフィルタ選択オプション

HTML:

<div ng-repeat="object in objects"> 
    <md-select ng-model="selectedPriority[$index].priority" ng-change="selectedPriority[$index].objectId = object.id" > 
     <md-option ng-value="priority" ng-repeat="priority in priorities | arrayDiff:priorities:selectedPriority[$index].priority">{{ priority }}</md-option> 
    </md-select> 
</div> 

JS:

.controller('Ctrl', function ($scope, $filter) { 
    $scope.selectedPriority = [ ]; 
    $scope.data = []; 
    $scope.priorities = ['1', '2', '3']; 
    $scope.objects = [{"date": "2017-01-08", "duration": 120}, {"date": "2017-01-07", "duration": 120}] 
}) 

.filter('arrayDiff', function() { 
    return function(array, diff) { 
     var i, item, 
      newArray = [], 
      exception = Array.prototype.slice.call(arguments, 2); 

     for(i = 0; i < array.length; i++) { 
      item = array[i]; 
      if(diff.indexOf(item) < 0 || exception.indexOf(item) >= 0) { 
       newArray.push(item); 
      } 
     } 

     return newArray; 

    }; 
}); 

ここに私のコードのplunkerです: http://plnkr.co/edit/ZXp5kKJaFZcKdur2xueS?p=preview

+1

が良いかもしれません。 –

+0

私のコードでプランナーを追加しました:http://plnkr.co/edit/ZXp5kKJaFZcKdur2xueS?p=preview – maidi

答えて

0

私はdisable when表現を使用していますng-optionsを使用して例を作成しました。

Usage

label disable when disable for value in array track by trackexpr 

秒「無効」trueに評価されている場合、そのオプションを無効にします(あなたは、単にtrueでそれを置き換えることによって、これをテストすることができます)、という表現があります。私は以下の例を作成しました:この例では

HTML

<div ng-app="test" ng-controller="MainCtrl"> 
    <select 
     ng-options="o as o disable when second == o for o in options" 
     ng-model="first" 
     ng-change="change(first)"> 
    </select> 

    <select 
     ng-options="o as o disable when first == o for o in options" 
     ng-model="second" 
     ng-change="change(second)"> 
    </select> 
</div> 

JS

angular.module('test', []) 
.controller('MainCtrl', function($scope) { 
    $scope.options = [1, 2, 3]; 
    $scope.change = function(selection) { 
     console.log(selection); 
    } 
}); 

を、最初のNG-オプションディレクティブは、ngのモデルを追跡します2番めのng-optionsディレクティブ、およびその逆。オプションが他のモデルの値と等しい場合、その値は無効になります。

オブジェクトデータソースで「無効にする」という表現が少し異なります。使用方法についてはdocumentationを参照してください。あなたのコードのplunker/jsfiddleを提供できるかどう

Plunkr

+0

しかし私はあなたの例では、これらの2つのような特定の数の選択ボックスを持っていません。複数のボックスがオブジェクトの数によって生成されており、1つのオプションが選択されている場合はすべてフィルタリングする必要があります。 – maidi

+0

このフィルタは、どのようにさまざまな選択ボックスで実装しますか? – maidi

関連する問題