我有下一个模型wirt子数组
<!-- ko foreach: {data: userAdminView.viewRoles, as: 'rrole'} -->
<tr>
<td class="userRolesRoleTitle"><b data-bind="text: rrole.role.name"></b><br/><i data-bind="text: rrole.role.description"></i></td>
<td class="userRolesRoleGroups">
<!-- ko foreach: {data: rrole.role.groups, as: 'group'} -->
<div class="usersGroupElement" data-bind="html: group.viewName"></div>
<!-- /ko -->
<a class="btn emb green" data-bind="click: userAdminView.addNewGroup,visible:(rrole.role.isNewGroupAccessible) , attr: { value: rrole }"><i class="icon16 plus"></i>add</a>
</td>
</tr>
<!-- /ko -->
它运行良好,但从业务需要我需要动态更改rrole.role.groups子数组中的数据。我的代码正在工作并执行此操作,我看到在调试模式下数组已更改(添加、删除元素),但仅当 userAdminView.viewRoles数组发生更改时才会更新视图。为什么 knockoutjs 不管理它。我该如何解决?
function userAdminView(user) {
var self = this;
self.viewRoles = ko.observableArray([]);
self.saveNewGroup = function (data, event) {
var datar = self.viewRoles();
$.each(datar, function (index, vrole) {
if (vrole.role.id == self.dialogRole.role.id) {
var line = {
viewName: "<b>" + self.dialogCustomer.name + "</b>",
customer: self.dialogCustomer,
department: self.dialogDepartment,
isEdit: true
};
if (vrole.role.groups == null) {
vrole.role.groups = [];
}
vrole.role.groups.push(line);
}
});
};
}
数据结构:
{
role: {
description: role.description,
id: role.id,
name: role.name,
groups: existingGroups,
isNewGroupAccessible: self.canGrant(role, data.d)
}
};
谢谢。