I want to calculate within an if binding from a nested view model.
I know how to do a standard if binding based on my view model:
<input type="text" data-bind="value: SelectedVendor() ? SelectedVendor().VendorPrice : '0'" /></td>
Now I want to do the Following:
- If
QTYonOrder > SelectedVendor().VendorMOQthen I want to display theQTYonOrder - If
QTYonOrder < SelectedVendor().VendorMOQthen I want to display theVendorMOQ
Is it possible to do these calculation within the data-bind IF?
My JSON Reply:
{
"ProductName": "Product123",
"RequiredComponents": "CAP 10% H/Vol",
"StockCode": "142111411",
"RequiredQtyByBom": 4,
"QtyUnassignedInWarehouse": 0,
"QtyAllocatedInWarehouse": 40,
"PCBReference": "442C",
"QtyOnOrder": 26,
"Vendors": [],
"RequireVendor": false
},
{
"ProductName": "Product123",
"RequiredComponents": "Screws",
"StockCode": "Screws",
"RequiredQtyByBom": 1,
"QtyUnassignedInWarehouse": 0,
"QtyAllocatedInWarehouse": 14,
"PCBReference": "Screws",
"QtyOnOrder": 26,
"Vendors": [
{"VendorID": "3",
"VendorName": "ABC Supplier",
"VendorMOQ": 50000,
"VendorItemPrice": 322},
{"VendorID": "4",
"VendorName": "DEF Supplier",
"VendorMOQ": 4,
"VendorItemPrice": 120}
],
"RequireVendor": true
},
{
"ProductName": "Product123",
"RequiredComponents": "14141415",
"StockCode": "151555231",
"RequiredQtyByBom": 1,
"QtyUnassignedInWarehouse": 0,
"QtyAllocatedInWarehouse": 170,
"PCBReference": "1414",
"QtyOnOrder": 26,
"Vendors": [],
"RequireVendor": false
}
My HTML:
<table class="table table-bordered">
<thead>
<tr>
<td>Stock Code</td>
<td>Qty Required</td>
<td>Vendor</td>
<td>Vendor Price p/Unit</td>
<td>MOQ</td>
<td>Qty To Order</td>
<td>Value</td>
</tr>
</thead>
<tbody data-bind="foreach: CheckStock">
<tr data-bind="if: RequireVendor">
<td data-bind="text: StockCode"></td>
<td data-bind="text: (RequiredQtyByBom * QtyOnOrder)">
</td>
<td>
<select data-bind="options: Vendors, optionsText: 'VendorName', optionsCaption: 'Choose a Vendor...', value: SelectedVendor" class="form-control"></select>
</td>
<td>
<input type="text" data-bind="value: SelectedVendor() ? SelectedVendor().VendorPrice : '0'" /></td>
<td data-bind="text: SelectedVendor() ? SelectedVendor().VendorMOQ : '0'"></td>
<td>TODO CALC</td>
<td>TODO CALC</td>
</tr>
</tbody>
</table>
computed observablein yourviewModelfor this.