2016-08-18 2 views
0

2つのコンボボックスがあり、stationTypeまたはstation(ステーションが選択されている場合はstationTypeをゼロに設定し、その逆も同様です)を選択します。次のコードの結果は、それらの1つが変更されたときに両方をゼロに設定します。私の選択を維持し、他のボックスだけをリセットする方法はありますか?ノックアウトを使用して要素または要素タイプのいずれかを選択

<select class="form-control input-sm" data-bind="options: lookups.stationTypes, optionsText: 'Name', optionsValue: 'Id', value: ProductPlanItem().StationTypeId"></select> 

<select class="form-control input-sm" data-bind="options: lookups.stations, optionsText: 'Name', optionsValue: 'Id', value: ProductPlanItem().StationId"></select> 

ProductPlanItemはこのようなものです:あなたは、このためのreadwrite機能をko.computedを使用することができます

function ProductPlanItem() { 
     var me = this 
     me.StationTypeId = ko.observable() 
     me.StationId = ko.observable() 
     me.StationTypeId.subscribe(function() { 
      me.StationId(0) 
     }) 
     me.StationId.subscribe(function() { 
      me.StationTypeId(0) 
     }) 
    } 

答えて

0

。あなたの現在の設定の問題は、最初のサブスクライブでStationIdを設定すると、2番目のサブスクライブがトリガーされるということです。

ここでの例では、あなたの基本的な行動だ:

function ViewModel() { 
 
    this.first = ko.observable(1); 
 
    this.second = ko.observable(2); 
 

 
    this.oddsAndZero = [0, 1, 3, 5, 7, 9]; 
 
    this.evensAndZero = [0, 2, 4, 6, 8, 10]; 
 

 
    this.firstSelection = ko.computed({ 
 
    read: this.first, 
 
    write: function(newValue) { 
 
     this.first(newValue); 
 
     this.second(0); 
 
    }, 
 
    owner: this 
 
    }); 
 
    
 
    this.secondSelection = ko.computed({ 
 
    read: this.second, 
 
    write: function(newValue) { 
 
     this.second(newValue); 
 
     this.first(0); 
 
    }, 
 
    owner: this 
 
    }); 
 

 
} 
 

 
ko.applyBindings(new ViewModel());
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script> 
 

 

 
<select data-bind="options: oddsAndZero, value: firstSelection"></select> 
 
<select data-bind="options: evensAndZero, value: secondSelection"></select>

+0

まさに、私が計算さを使用することを忘れ – user3222589

関連する問題