问题是数据绑定不起作用。
我有一个包含声明的observableArray() 。每个索赔都有一个observableArray()费用,这是self.pc_listOfExpenses()中的一项。
这是两个数组的结构:
self.pc_listOfClaims = ([
ID: 34
claimAgainst: "CompanyName"
date: "2010-10-10"
desc: "Description"
**expenses: Object[0]** // <= This one should be an array.
]);
self.pc_listOfExpenses = ko.observableArray([
ID: "34"
**Array** // <= This is the array of "Expense" Objects
]);
注意: *不要担心上面的正确语法。我使用 JSON 填充来自服务的数组。Object[0] 是我在控制台中看到的。*
这个想法是用相应的费用数组映射每个索赔:如果我们有claimID = 34,那么在 self.pc_listOfExpenses() 数组中我们有34=>Array()。
这是淘汰赛代码:
//#region Preview Claims Block
/**************************************************/
/* Preview Claims Block */
/**************************************************/
self.pc_listOfClaims = ko.observableArray([]);
self.pc_showDetailsArr = ko.observableArray([]);
self.pc_listOfExpenses = ko.observableArray([]);
// Get userID-specified Claims using WS
pc_GetSpecifiedClaims(self);
for (var i = 0; i < self.pc_listOfClaims().length; i++) {
self.pc_showDetailsArr()[self.pc_listOfClaims()[i].ID] = ko.observable(false);
}
self.pc_showMoreDetails = function (claim) {
if (typeof claim !== "undefined") {
self.pc_showDetailsArr()[claim.ID](!self.pc_showDetailsArr()[claim.ID]());
pc_GetClaimExpenses(claim.ID);
for (var i = 0; i < self.pc_listOfExpenses()[claim.ID].length; i++) {
self.pc_listOfClaims()[claim_id]["expenses"]().push(self.pc_listOfExpenses()[claim.ID][i]);
}
}
}
//#endregion
这是网络服务:
function pc_GetClaimExpenses(claimID) {
$.ajax({
cache: false,
async: false,
type: 'GET',
url: '/DesktopModules/UltimateExpenses/API/Claims/GetSpecifiedExpensesAsJSONString',
success: function (data) {
self.pc_listOfExpenses()[claimID] = JSON.parse(data);
//console.log(JSON.parse(data));
}
});
}
这是视图:
<table id="claimsDataTable">
<tbody data-bind="foreach: pc_listOfClaims">
<tr>
<td data-bind="text: claimAgainst"></td>
<td data-bind="text: projectName"></td>
<td data-bind="text: taskName"></td>
<td data-bind="text: price"></td>
<td data-bind="text: status"></td>
<td data-bind="text: date"></td>
<td class="actionOptions">
<a href="#" data-bind="click: pc_showMoreDetails">M</a>
</td>
</tr>
<tr>
<td colspan="7" data-bind="visible: pc_showDetailsArr()[$data.ID]">
<!-- This is the part which does not work-->
<div data-bind="foreach: expenses">
<span data-bind="text: ID"></span>
<span data-bind="text: Type"></span>
<span data-bind="text: Price"></span>
</div>
</td>
</tr>
</tbody>
</table>