I want to display my data from ajax result in the table using knockout. The data show up, but not displaying right. I don't know how to fix that. I've read most of the post here but still cant find what's wrong.
Expected Result
Account A
Header
Row 1
Row 2
Row 3
Displayed Result
Account A
Header
Row 1 Row 2 Row 3
HTML is:
<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 is:
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());
Sample data:
[
{
"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
}
]
}
]
Any idea why?
I think all you need to do is move the foreach from the <tr>
to the <tbody>
because the foreach uses the the child elements of the element where it is declared as the template to dupilcate.
<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>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.