locked
Refreshing detail total sums from the parent screen RRS feed

  • Question

  • This has definitely been seen in quite a few posts. Not sure there has been a definitive answer.

    In my case I have a claim header that contains the total of all the parts and costs in two files underneath it.

    I have implemented the example of a variable on a screen with a post_render method that is bound to  the screen.HWSRWCDs.count. It seems to work fine unless the only thing that has changed is the quantity or cost of the item.

    Adding a cost will always trigger the change.

    Another solution suggested adding a setInterval to the contentItem databind but that errors when the collection has been closed.

    Anyone have a great idea on how to see the changes? I have likewise tried the suggestion Josh had to use another contentItem property but have struck out so far. Ideally I would like to use the state of the HWSRWCDs collection but have not found code to support that.

    Thanks for any help!

    Konrad U

    myapp.AddEditWarrantyClaims.TotalCosts_postRender = function (element, contentItem) {
        // Total Claim Costs
        function updateTotal() {
            // Compute the total for the Claim        contentItem.screen.TotalCosts =
                TotalDetailCost(contentItem.screen.HWSRWCDs);
        }
        // Set a dataBind to update the value when the collection changes
        //Set up a databind on screen.HWSRWCDs.count
           contentItem.dataBind("screen.HWSRWCDs.count", updateTotal);
    };
    // Function to compute the total for the Order 
    function TotalDetailCost(HWSRWCDs) {
        // Start with 0
        var TotCst = 0;
        // Get the data for the collection passed
        var HWSRWCD = HWSRWCDs.data;
        // Loop through each row
        HWSRWCD.forEach(function (HWSRWCD) {
            // Add each row to HWSRWCD
            TotCst = +(TotCst) +
                +(HWSRWCD.BTPTOT);
        });
        // Return TotCst
        return TotCst.toFixed(2);


    
    

    • Edited by GA_Radster Friday, September 11, 2015 12:06 AM
    Friday, September 11, 2015 12:03 AM

Answers

  • AddEditQuote has a table of line items and Quantity and Price are editable:

    Assuming you have the line items (including Quantity and Price properties) on the Claims screen, even if they are in a read-only tile list, it should still recalc after you return from AddEdit Child screen. 

    HTH,

    Josh


    • Edited by joshbooker Tuesday, September 22, 2015 4:48 PM
    • Marked as answer by Angie Xu Tuesday, October 6, 2015 8:14 AM
    Tuesday, September 22, 2015 4:43 PM

All replies

  • Hi GA_Radster,

    Do you mean you want to refresh the total sum on parent screen after you modify and save the quantity and cost in child screen? Which version of Visual Studio are you using? In my opinion, there has a new feature of API support for refresh data on screens in VS2013. There are two methods are consisted in refresh() API, refresh() and refresh(navigationPropertyNames). About these methods and how to use them, please refer to following article.
    http://lightswitchhelpwebsite.com/Blog/tabid/61/EntryId/1208/New-API-For-Refreshing-Data-in-LightSwitch-in-Visual-Studio-2013.aspx
     
    Best Regards,
    Weiwei

    • Proposed as answer by Angie Xu Monday, September 21, 2015 1:38 AM
    Monday, September 14, 2015 9:45 AM
    Moderator
  • Hey Konrad,

    Try an event listener CollectionChange.  I believe this will catch property changes of entities in the collection.  binding to count won't do that and it also doesn't fire if you're using it to calc a sum on a filtered list and the count doesn't change from one filter to another. 

    onCollection Change event handles this well.  (Air code not tested)

        screen.HWSRWCDs.addEventListener("collectionchange", function () {
            updateTotal;
        });

    more:

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/950a1977-0b14-4c8a-aef8-e51ace3b0a25/how-get-total-sum-property-of-filtered-table?forum=lightswitch

    HTH,

    Josh


    • Edited by joshbooker Tuesday, September 15, 2015 4:49 PM
    Tuesday, September 15, 2015 4:48 PM
  • Josh,

    Thanks for the suggestion! I am assuming that I need to create and dispose of it with the standard functions. Should be easy to test.

    Konrad U.

    Wednesday, September 16, 2015 11:21 PM
  • function onCostChange() {
            // Compute the total for the Order
             screen.TotalCosts = TotalDetailCost(screen.HWSRWCDs);
        }
        screen.HWSRWCDs.addEventListener("collectionchange", onCostChange);
        // Reclaim Cost Change
        screen.findContentItem("Details").handleViewDispose(function () {
        screen.HWSRWCDs.removeEventListener("collectionchange", onCostChange);
        });

    Josh,

    Tried the event listener and it works just like the count method. Apparently the events are only add or delete. See anything wrong here? Any other suggestions?

    Monday, September 21, 2015 11:53 PM
  • Hey Konrad,

    It appears you're correct.  Changing properties of items in a collection doesn't raise collectionchange event.  There may be another event that gets raised, but who knows(?)

    I suppose you'll have to recalc your total when the line item property changes.

    Here is an example of doing this:

    myapp.AddEditQuote.created = function (screen) { // calculate total from quantity * price screen.TotalDetailCost = function (data) { var total = 0; data.forEach(function (data) { total += (data.Quantity * data.Price); }); return total.toFixed(2); }; screen.onCostChange = function () { setTimeout(function () { //set Total property screen.Total = screen.TotalDetailCost(screen.QuoteLineItems.data); }, 1); }; screen.QuoteLineItems.addEventListener("collectionchange", function () { screen.onCostChange(); }); }; myapp.AddEditQuote.Quantity_postRender = function (element, contentItem) { // recalc when quantity is changed

    contentItem.dataBind("value", function () { contentItem.screen.onCostChange(); }); }; myapp.AddEditQuote.Price_postRender = function (element, contentItem) { // recalc when price is changed contentItem.dataBind("value", function () { contentItem.screen.onCostChange(); }); };

    This is AddEditQuote screen with a table of QuoteLineItems collection having Quantity and Price properties. 

    The functions are methods of screen so they will be accessible from anywhere (render, postreneder, created, etc.).

    You should be able to translate this into your entities fairly easily.

    HTH,

    Josh


    • Edited by joshbooker Tuesday, September 22, 2015 3:55 PM
    Tuesday, September 22, 2015 3:54 PM
  • Josh,

    Nice method but I see the quantity and price binds are on the main quote screen. My items and costs are on separate screens. Not sure the bind will work when the total is on the AddEditClaim screen and the quantity and price are on the AddEditItem or AddEditCost screen correct?

    Maybe you are binding to the display variable on the read only tile before the after/before the edit step?

    Thanks!

    Konrad U

    Tuesday, September 22, 2015 4:28 PM
  • AddEditQuote has a table of line items and Quantity and Price are editable:

    Assuming you have the line items (including Quantity and Price properties) on the Claims screen, even if they are in a read-only tile list, it should still recalc after you return from AddEdit Child screen. 

    HTH,

    Josh


    • Edited by joshbooker Tuesday, September 22, 2015 4:48 PM
    • Marked as answer by Angie Xu Tuesday, October 6, 2015 8:14 AM
    Tuesday, September 22, 2015 4:43 PM
  • Josh,

    Thanks! I am working with that. Unfortunately my items and costs have about 30 fields to them so they require a separate entry screen but I do have the tile for each one that updates based on the underlying changes.

    Seems like it should work, thanks again.

    Konrad U

    Tuesday, September 22, 2015 4:55 PM
  • Is the total a screen property or an entity. If its an entity why not do the calc in the updating event serverside and use a entity detail .refresh if needed. I always do my entity calc serverside and screen property in the client

    Sven Elm


    • Edited by Sven Elm Tuesday, September 22, 2015 6:43 PM
    Tuesday, September 22, 2015 6:41 PM