我想在if binding
嵌套视图模型中计算。我知道如何if binding
根据我的视图模型做一个标准:
<input type="text" data-bind="value: SelectedVendor() ? SelectedVendor().VendorPrice : '0'" /></td>
现在我想做以下事情:
- 如果
QTYonOrder > SelectedVendor().VendorMOQ
那时我想显示QTYonOrder
- 如果
QTYonOrder < SelectedVendor().VendorMOQ
那时我想显示VendorMOQ
是否可以在数据绑定 IF 中进行这些计算?
我的 JSON 回复:
{
"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
}
我的 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>