2016-08-18 3 views
0

두 개의 콤보 상자가 있으며 stationType 또는 station (station을 선택하면 stationType을 0으로 설정하고 그 반대도 마찬가지입니다)를 선택하려고합니다. 다음 코드의 결과는 둘 중 하나가 변경되면 둘을 0으로 설정합니다. 내 선택을 유지하고 다른 상자 만 다시 설정하는 방법이 있습니까?knockout을 사용하여 요소 또는 요소 유형을 선택하십시오.

<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 설정은 두 번째 구독을 시작한다는 것입니다.

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

관련 문제