我想根据 JSON 有效负载中的项目是否需要供应商,将供应商列表嵌套到我从服务器获取的现有 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
}
我正在考虑使用 2 AJAX 调用将值推送到 Observable 数组中。
AJAX 调用 1:为产品创建初始有效负载
MyDataViewModel.SelectedOrderID.subscribe = ko.computed(function () {
$.ajax({
url: "/URLToMethod/GetBomStockByProductID",
data: { OrderID: ko.toJS(MyDataViewModel.SelectedOrderID) },
type: "GET",
contentType: "application/json; charset=utf-8",
dataType: "JSON",
timeout: 10000,
success: function (Result) {
for (var i = 0; i < Result.d.length; i++) {
element = Result.d[i];
MyDataViewModel.CheckStock.push({
ProductName: element.ProductName, RequiredComponents: element.RequiredComponents, StockCode: element.StockCode, RequiredQtyByBom: element.RequiredQtyByBom, QtyUnassignedInWarehouse: element.QtyUnassignedInWarehouse, QtyAllocatedInWarehouse: element.QtyAllocatedInWarehouse, PCBReference: element.PCBReference, QtyOnOrder: element.QtyOnOrder, Vendors: ko.observableArray(), RequireVendor: ko.computed(function () {
if ((element.RequiredQtyByBom * element.QtyOnOrder) > element.QtyAllocatedInWarehouse) {
return true
} else {
return false
}
})
}
);
}
},
error: function (xhr, status) {
alert(status + " - " + xhr.responseText);
}
});
});
AJAX 调用 2:将值推送到供应商 ko.observableArray() 在第一个有效负载中创建
MyDataViewModel.PurchaseReqHasVendorDetails = ko.computed(function () {
var self = MyDataViewModel;
for (var i = 0; i < self.CheckStock().length; i++) {
if (self.CheckStock()[i].RequirePO()) {
$.ajax({
url: "/URLToMethod/GetVendorsByProductName",
data: { ProductName: self.CheckStock()[i].ProductName },
type: "GET",
contentType: "application/json; charset=utf-8",
dataType: "JSON",
timeout: 10000,
success: function (Result) {
for (var i = 0; i < Result.d.length; i++) {
element = Result.d[i];
self.CheckStock()[i].Vendors.push({ VendorID: element.VendorID, VendorName: element.VendorName, VendorMOQ: element.VendorMOQ, VendorPrice: element.VendorPrice });
}
},
error: function (xhr, status) {
alert(status + " - " + xhr.responseText);
}
});
return true;
} else {
return false;
}
}
});
但这不起作用。它创建了初始有效负载,但没有获得第二个有效负载并将其推送到我创建的 observableArray 中。
任何建议将不胜感激