Knockout popover绑定清单

时间:2014-02-15 13:27:29

标签: knockout.js

我想使用knockout Js.Please help将数据库中的值绑定到popover复选框列表。提前感谢!

1 个答案:

答案 0 :(得分:0)

var viewModel = function() {
    var self = this;
    self.valuesForCheckBoxList = ko.observableArray([]);
    self.init = function () {
        $.get("... url for get data from database ...", function (data) {
            self.valuesForCheckBoxList(data);
        });
    };
    self.init();
}

var vm = new viewModel();
ko.applyBindings(vm);

如果返回的数据看起来像这样:[{Id: 1, Name: "n1"}, {Id: 2, Name: "n2"}],你可以写下面的html:

<ul data-bind="foreach: valuesForCheckBoxList">
    <li>
        <span data-bind="text: Name"></span>
        <input type="checkbox" data-bind="checked: $root.valuesForCheckBoxList, value: Id" />
    </li>
</ul>

对于您更具体要求的配置,您可以阅读此https://api.jquery.com/jQuery.get/页面 ko.observableArray:http://knockoutjs.com/documentation/observableArrays.html
复选框绑定:http://knockoutjs.com/documentation/checked-binding.html