如何在组合框中连接敲除值

时间:2013-11-26 11:02:09

标签: javascript knockout.js combobox

这是javascript

var ViewModel = function () {
    var self = this;
    self.vehicles = ko.observableArray([{
        Id: 1,
        Brand: "Volkswagen",
        Type: "Golf"
    }, {
        Id: 2,
        Brand: "Volkswagen",
        Type: "Sharan"
    }, {
        Id: 3,
        Brand: "BMW",
        Type: "118i"
    }, {
        Id: 2,
        Brand: "BMW",
        Type: "525D"
    }]);

    self.brands = ko.computed(function(){
        var list = ko.utils.arrayMap(self.vehicles(), function(item){
            return item.Brand;
        });

        return ko.utils.arrayGetDistinctValues(list);
    });
};

ko.applyBindings(new ViewModel());

$("select").multiselect();

这是带淘汰赛的Html

<select data-bind="foreach: brands" multiple="multiple" >
    <optgroup data-bind="attr: {label: $data}, foreach: $parent.vehicles">
    <!-- ko if: Brand == $parent -->
        <option data-bind="text: Type"></option>
    <!-- /ko -->
    </optgroup>
</select>

如何连结{Brand}/{Id} + {type} .

http://jsfiddle.net/ruchan/ARF29/2/

1 个答案:

答案 0 :(得分:1)

您可以在绑定中使用JavaScript字符串连接运算符+

<option data-bind="text: Brand + '/' + Id + ' ' + Type"></option>

演示JSFiddle