[英]With knockout.js how to refresh observable array in a Select element
When the dropdown is loaded the items are populated correctly. 加载下拉列表后,将正确填充项目。 But when I click the Refresh button the Dropdown items are not refreshed. 但是,当我单击“刷新”按钮时,下拉菜单项不会被刷新。 The data binding isn't working on clicking the Refresh button. 单击刷新按钮无法进行数据绑定。 After clicking the Refresh button I am expecting to see 'Mary' and 'Sandra'. 单击“刷新”按钮后,我希望看到“玛丽”和“桑德拉”。
Here's the JavaScript: 这是JavaScript:
$(function () {
function viewModel() {
var self = this;
self.persons = ko.observableArray([]);
self.persons.push({
id: 1,
name: 'John'
});
self.persons.push({
id: 2,
name: 'Paul'
});
self.refreshPersonList = function () {
self.persons = ko.observableArray([]);
self.persons.push({ id: 3, name: 'Mary' });
self.persons.push({ id: 4, name: 'Sandra' });
};
}
ko.applyBindings(new viewModel());
});
And HTML: 和HTML:
<select data-bind="options: $root.persons(), optionsValue: 'id', optionsText: 'name'"></select>
<button value="Refresh" data-bind="event: {click : $root.refreshPersonList() }">Refresh Person List</button>
Here you are creating a new array, breaking existing bindings: 在这里,您正在创建一个新的数组,打破了现有的绑定:
self.persons = ko.observableArray([]);
Try emptying the array instead: 尝试清空数组:
self.refreshPersonList = function () {
self.persons.removeAll(); //Empty array
self.persons.push({ id: 3, name: 'Mary' });
self.persons.push({ id: 4, name: 'Sandra' });
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.