2017-11-22 7 views
1

와 OptionText에서 multilpe 텍스트를 표시합니다 :어떻게이 코드가 Knockout.js

<select name="test" id="test" class="" 
    data-bind=" 
    options: myArray, 
    value: idSelected, 
    optionsText: 'name', 
    optionsValue: 'id', 
    optionsCaption: 'All'> 
</select> 

결과 :

text 1 
text 2 
text 3 
... 

내가 가진 ID와 이름 CONCAT합니다 '-'.

1 - Text 1 
2 - text 2 
3 - text 3 
... 

답변

0

당신은 computed 속성을 만들고 options에 그 바인딩 할 수 있습니다 :이 원하는.

var viewModel = function() { 
 
    var self = this; 
 
    self.idSelected = ko.observable(); 
 
    self.myArray = ko.observableArray([{ 
 
    name: "Text 1", 
 
    id: 1 
 
    }, { 
 
    name: "Text 2", 
 
    id: 2 
 
    }]); 
 
    
 
    // bind this to the options 
 
    self.computedArray = ko.computed(() => { 
 
    return self.myArray().map(function(item) { 
 
     return { 
 
     name: item.id + ' - ' + item.name, 
 
     id: item.id 
 
     } 
 
    }); 
 
    }) 
 
} 
 

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

 
<select name="test" id="test" class="" data-bind=" 
 
        options: computedArray, 
 
        value: idSelected, 
 
        optionsText: 'name', 
 
        optionsValue: 'id', 
 
        optionsCaption: 'All'"> 
 
</select>
(당신은 녹아웃을 사용하는 경우 또한 pureComputed을 사용할 수 있습니다 3.2 이상)

: 여기

는 작업 조각의