2013-02-27 2 views
0

Knockoutjs 바인딩에서 사용자 정의 데이터 옵션을 지정할 수있는 방법이 있습니까?Knockout.js options custom data-something

<select data-bind="options: filtered, value: value, optionsText: 'Text', optionsValue: 'Value', 'data-something': 'Description' }"></select> 

은이 결과 :

<option value="foo" data-something="description">Text</option> 

회신을 바랍니다.

답변

1

Custom Binding

<select data-bind="createDropDown: filtered"></select> 

그런 다음 당신이 선택에 각 옵션을 추가 수동으로 자신을 배열을 처리 할 수 ​​있으며이 사용자 정의 바인딩 방법, 추가 :이 테스트하지했습니다

ko.bindingHandlers.yourBindingName = 
{ 
    update: function(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) 
    { 
     ko.utils.arrayForEach(valueAccessor(), function (item) 
     { 
      $(element).append('<option value="' + item[value] + '" data-something="' + item[description] + '">' + item[text] + '</option>'); 
     }); 
    } 
}; 

을하지만, 당신은 아이디어를 얻어야합니다.

+0

잘 작동하면 질문자 – nav0611

0

아마도 이것이 당신이 찾고있는 것입니까?

<select data-bind="foreach: filtered"> 
    <option data-bind="text: Text, attr: {value: Value, 'data-something': Description}"></option> 
</select>