当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - Computed property with HTML LightSwitch

问题描述:

I followed the tutorial here:

Computed Properties With the LightSwitch HTML Client (lightswitchhelpwebsite.com)

Here is the tutorial code:

myapp.AddEditFlowerShopOrder.NumberOfDetails_postRender = function (element, contentItem) {

function updateTotal() {

// Compute the total for the Order

contentItem.screen.TotalOfOrders =

TotalOrders(contentItem.screen.FlowerShopOrderDetail);

}

// Set a dataBind to update the value when the collection changes

contentItem.dataBind("screen.FlowerShopOrderDetail.count", updateTotal)

};

// Function to compute the total for the Order

function TotalOrders(OrderDetails) {

// Start with 0

var TotalAmountOfOrders = 0;

// Get the data for the collection passed

var OrderDetail = OrderDetails.data;

// Loop through each row

OrderDetail.forEach(function (order) {

// Add each row to TotalAmountOfOrders

TotalAmountOfOrders = TotalAmountOfOrders +

(order.Quantity * order.FlowerShopProduct.Price);

});

// Return TotalAmountOfOrders

return TotalAmountOfOrders;

}

Here is MY code:

myapp.AddEditOrder.Order_Subtotal_postRender = function (element, contentItem) {

//Write code here.

function subtotalDetails() {

contentItem.screen.Order.Subtotal =

TotalOrderDetails(contentItem.screen.OrderDetails);

}

subtotalDetails();

//contentItem.dataBind("screen.OrderDetails.count", subtotalDetails(contentItem));

};

function TotalOrderDetails(details) {

var subtotal = 0;

var detail = details.data;

detail.forEach(function (order) { subtotal = subtotal + order.Total; });

return subtotal;

}

I could not get the dataBind to bind, it says it can't return the call. So I commented it out and called it just once, when it renders to get the rest of the code working. I have no idea how the .data member works, but if I change the detail.forEach into a for, it's not iterating through a collection of ProductDetails' ProductDetail objects.

Just to clarify, I have these tables: Order >- OrderDetails. My goal is to bind the Order.Subtotal = sum of Order.OrderDetail(all items).Total.

网友答案:

The differences I see in your code versus the demo is that the demo uses a Screen property to hold the Subtotal(screen.TotalOfOrders), while you're apparently trying to change a Subtotal field in your Order record (screen.Order.Subtotal). Remember that to access a field in a Parent table from a Child record, you need to use screen.data.fieldnameyouwant instead of screen.ParentTable.fieldname. I know the second way seems like the logical thing to do, but Lightswitch doesn't see it that way.

What I would try is to make a screen property called DetailTotal like the demo does, then wait until the screen's validation event fires and update the parent record subtotal there. That way, nothing actually gets written to the parent until you're leaving the screen anyway.

myapp.AddEditOrder.beforeApplyChanges = function (screen) {
    // Write code here.
   var targetOrder = screen.findContentItem("Order_Subtotal");
   targetOrder.data.Subtotal = screen.DetailTotal;
};
分享给朋友:
您可能感兴趣的文章:
随机阅读: