嵌套foreach在knockout.js数据显示,但不显示正确



我想使用knockout在表中显示来自ajax结果的数据。数据显示出来了,但显示不正确。我不知道该怎么弥补。我已经阅读了这里的大部分帖子,但仍然找不到问题所在。

预期的结果

帐户A
标题
第一行
第二行
第三行

显示结果帐户A
标题
第一行第二行第三行

HTML是:

<form>
//detail form here
<button type="button" data-bind="click: $root.getReport">GET DATA</button>
</form>
<table data-bind="foreach: coas" style="width: 100%">
<thead>
<tr><th colspan="5"><span data-bind="text: coaname">COA NAME</span></th></tr>
<tr>
<th style="width: 20%">date</th>
<th style="width: 20%">account</th>
<th style="width: 20%">debet</th>
<th style="width: 20%">credit</th>
<th style="width: 20%">saldo</th>
</tr>
</thead>
<tbody>
<tr data-bind="foreach: coaDetails">
<td data-bind="text: date">### date ###</td>
<td data-bind="text: coa">### account ###</td>
<td data-bind="text: debet">### debet ###</td>
<td data-bind="text: credit">### credit ###</td>
<td data-bind="text: saldo">### saldo ###</td>
</tr>
</tbody>
</table>

JS是:

function AddCoaDetails(date,coa,debet,credit,saldo){
var self= this; 
self.date = ko.observable(date);
self.coa = ko.observable(coa);
if(debet == 0)
self.debet = '';
else
self.debet = ko.observable(addPeriod(debet,","));
if(credit == 0)
self.credit = '';
else
self.credit = ko.observable(addPeriod(credit,","));
if(saldo == 0)
self.saldo = '';
else
self.saldo = ko.observable(addPeriod(saldo,","));
}
function AddCoaModel(coaname,detail){
var self = this;
self.coaname = ko.observable(coaname);
self.coaDetails = ko.observableArray(detail)
}
function LedgerModel(){
var self = this;
self.coas = ko.observableArray([]);
var coas = self.coas;
self.getReport = function(){
$.ajax({ 
//after ajax succeed, will get var listcoa, data sample is attached below
var mappedCoas = listcoa.map(function (i) {
var coadetail = $(i.detail).each(function(key,item) {
return new AddCoaDetails(item.date, item.coa, item.debet, item.credit, item.saldo);
});
return new AddCoaModel(i.namecoa, coadetail);
});
coas(mappedCoas);
}}
ko.applyBindings(new LedgerModel());

样本数据:

[
{
"namecoa": "PIUTANG",
"detail": [
{
"date": "",
"coa": "Saldo Awal",
"debet": 0,
"credit": 0,
"saldo": 0
},
{
"date": "2021-10-12",
"coa": "KAS",
"debet": "1000000.00",
"credit": "0.00",
"saldo": 1000000
},
{
"date": "2021-10-13",
"coa": "KAS",
"debet": "10000.00",
"credit": "0.00",
"saldo": 1010000
}
]
},
{
"namecoa": "OPERATIONAL COST",
"detail": [
{
"date": "",
"coa": "Initial Saldo",
"debet": 0,
"credit": 0,
"saldo": 0
},
{
"date": "2021-10-19",
"coa": "BANK 2",
"debet": "0.00",
"credit": "5000000.00",
"saldo": -5000000
},
{
"date": "2021-10-19",
"coa": "BANK 1",
"debet": "0.00",
"credit": "10000000.00",
"saldo": -15000000
}
]
}
]

知道为什么吗?

我认为你所需要做的就是将foreach从<tr>移动到<tbody>,因为foreach使用了它被声明为模板的元素的子元素来复制。

<table data-bind="foreach: coas" style="width: 100%">
<thead>
<tr><th colspan="5"><span data-bind="text: coaname">COA NAME</span></th></tr>
<tr>
<th style="width: 20%">date</th>
<th style="width: 20%">account</th>
<th style="width: 20%">debet</th>
<th style="width: 20%">credit</th>
<th style="width: 20%">saldo</th>
</tr>
</thead>
<tbody data-bind="foreach: coaDetails">
<tr>
<td data-bind="text: date">### date ###</td>
<td data-bind="text: coa">### account ###</td>
<td data-bind="text: debet">### debet ###</td>
<td data-bind="text: credit">### credit ###</td>
<td data-bind="text: saldo">### saldo ###</td>
</tr>
</tbody>
</table> 

相关内容

  • 没有找到相关文章

最新更新