Jacques Bronkhorst Jacques Bronkhorst - 1 year ago 93
jQuery Question

Nested Table With KnockoutJS Observable array's(Parent & Child Table)

I am trying to build a Nested Editable gridview with KnockoutJS, and I have no idea where or what to do with it.

I have started the process by obtaining JSON data from my server and using the Mapping Model to map the required JSON info. I even have the Parent grid with all the required Values.
Now Based on the selection of the parent grid it should Pass 2 values to the Child then the Child JSON method should appear. And because it's knockout obviously it should be completely observable.

Here is the Code that I have done so far:

var ProductViewmodel;

function bindProductModel(Products) {
var self = this;
self.items = ko.mapping.fromJS([]);
ProductViewmodel = ko.mapping.fromJS(Products.d, self.items);
console.log(ProductViewmodel);
ko.applyBindings(ProductViewmodel);
}

function bindModel(data) {
var self = this;
self.Locations = ko.mapping.fromJS([]);
viewModel = ko.mapping.fromJS(data.d, self.Locations);
console.log(viewModel);
ko.applyBindings(viewModel);
}

$(document).ready(function () {
bindProductModel(data);
$('#child').click(function () {
bindModel(data2);
$('#childtable').show();
});
});


The JSON that get's returend From Server:

var data = {
"d": [{
"__type": "Warehouse.Tracntrace.Members_Only.StockMovement.ProductStagingMethod",
"ProductSKUID": 2,
"ProductSKUName": "Decoder 1131",
"WarehouseID": 1,
"WarehouseName": "SoftwareDevelopmentTest",
"SystemAreaName": "Staging",
"Qty": 5
}]
};

var data2 = {
"d": [{
"__type": "Warehouse.Tracntrace.Members_Only.StockMovement.StockReturnMethod",
"LotID": 3,
"LotName": "TestLot3",
"AreaID": 11,
"AreaName": "TestArea2L3",
"BinID": 20,
"BinName": "Area10Bin1"
}, {
"__type": "Warehouse.Tracntrace.Members_Only.StockMovement.StockReturnMethod",
"LotID": 4,
"LotName": "TestLot4",
"AreaID": 15,
"AreaName": "TestArea2L4",
"BinID": 24,
"BinName": "Area14Bin1"
}, {
"__type": "Warehouse.Tracntrace.Members_Only.StockMovement.StockReturnMethod",
"LotID": 2,
"LotName": "TestLot2",
"AreaID": 8,
"AreaName": "TestArea3L2",
"BinID": 18,
"BinName": "Area8Bin2"
}]
};


And Finally my Simple Table:

<table border="1" cellpadding="0" cellspacing="0">
<tbody data-bind="foreach: items">
<tr>
<td>
<input type="button" name="ShowmetheChild" value="ShowmetheChild" id="child" /></td>
<td data-bind="text: ProductSKUID"></td>
<td data-bind="text: ProductSKUName"></td>
<td data-bind="text: WarehouseID"></td>
<td data-bind="text: WarehouseName"></td>
<td data-bind="text: SystemAreaName"></td>
<td data-bind="text: Qty"></td>
<div id="childtable" style="display: none";>
<table>
<tbody data-bind="foreach: Locations>
<tr>
<td data-bind="text: LotName"></td>
<td data-bind="text: AreaName"></td>
<td data-bind="text: BinName"></td>
<td><input type="text" name="Qty" data-bind="text: 0" /></td>
</tr>
</tbody>
</table>
</div>
</tr>

</tbody>


So In the Parent Table the Qty table should be the Observable (It should decrease with each QTY entered in the Child Table.)

Any advice would be greatly appreciated.

Answer Source

There are several problems that appear with the JavaScript.

The biggest problem was caused by your adding the location data after clicking on the ShowMeBUtton. It's not easy to make Knockout work properly in this situation. So, I modified code so that the location data is added to the view model before doing the ko.applyBinding.

Simplified JavaScript is below.

function bindProductModel(Products) {
    var self = this;
    self.items = ko.mapping.fromJS([]);
    ProductViewmodel = ko.mapping.fromJS(Products.d, self.items);
}

function bindModel(vm, data) {
    vm.Locations = ko.mapping.fromJS(data.d);
}

$(document).ready(function() {
    bindProductModel(data);
    bindModel(ProductViewmodel()[0], data2);
    ko.applyBindings(ProductViewmodel);
    $('#child').click(function() {
        $('#childtable').show();
    });
});‚Äč

I've put together a working fiddle at: http://jsfiddle.net/photo_tom/p6dW6/23/

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download