The 2nd row of a repeating table does not calculate fields via code behind? - InfoPath Dev
in

InfoPath Dev

The 2nd row of a repeating table does not calculate fields via code behind?

Last post 04-20-2010 06:23 PM by Hilary Stoupa. 5 replies.
Page 1 of 1 (6 items)
Sort Posts: Previous Next
  • 04-16-2010 07:16 AM

    The 2nd row of a repeating table does not calculate fields via code behind?

    I am really liking InfoPath 2007.  However, I am still learning.

    Another question...

     

    I have a repeating table that has multiple rows.  The user will insert the first row, fill out a couple of fields and then as they tab through the columns certain fields will be calculated via code behind.  However, when you do the same thing with row 2 (and beyond) the code behind calculated fields do not calculate (no code behind fires)?

     Any ideas?  I am at a loss.  It is probably something small, but I can see it or find it.

     

    Thanks,

    Chris

  • 04-16-2010 08:57 AM In reply to

    Re: The 2nd row of a repeating table does not calculate fields via code behind?

    Hey, Chris:

    What is your code? What fields do you have Changed events on? How do you know the Changed events aren't firing? If you've used absolute rather than relative XPath in your code, you may just be repeatedly recalculating your first row values, so it only looks like the events aren't firing. Have you run your code in debug to see if the event fires or not?

    Hilary Stoupa

  • 04-16-2010 10:39 AM In reply to

    Re: The 2nd row of a repeating table does not calculate fields via code behind?

    I am using JScript as my code behind.

    When I tab from the Part Cost field it is set to fire the On After Change Event.  Which then calculates the Ext Cost field.  This works on the first row, but not the second (or more) row.  I have attached an image that shows the results.  The grey fields are calculated and the preceding field fires the On After Change Event.

    I am not sure about absolute vs. relative.  I am assuming that I am using absolute.

    EXAMPLE:

     function msoxd__tblDetailsInfo_plPartCost_attr::OnAfterChange(eventObj)
    {
    // Write code here to restore the global state.

        if (eventObj.IsUndoRedo)
            {
                // An undo or redo operation has occurred and the DOM is read-only.
                return;
            }

    // A field change has occurred and the DOM is writable. Write code here to respond to the changes.
        handler = 1;

        // Calculate Extended Cost, 13.5% OH, and Duty Cost columns.
        // CALC: Extended Cost Calculation
        var plQtyPer = XDocument.DOM.selectSingleNode("/dfs:myFields/dfs:dataFields/d:tblDetailsInfo/@plQtyPer");
        var plScrapPct = XDocument.DOM.selectSingleNode("/dfs:myFields/dfs:dataFields/d:tblDetailsInfo/@plScrapPct");
        var plPartCost = XDocument.DOM.selectSingleNode("/dfs:myFields/dfs:dataFields/d:tblDetailsInfo/@plPartCost");
        var plExtCost = XDocument.DOM.selectSingleNode("/dfs:myFields/dfs:dataFields/d:tblDetailsInfo/@plExtCost");
        
        // Calculate the new value and set it to 4 decimal places.
        var calcExtCost = (parseFloat(plPartCost.value * (plScrapPct.value * .01)) + parseFloat(plPartCost.value));
        plExtCost.text = calcExtCost.toFixed(4);

        // CALC: 13.5 OH
        var plOHPct = XDocument.DOM.selectSingleNode("/dfs:myFields/dfs:dataFields/d:tblDetailsInfo/@plOHPct");
        var calcOH = (parseFloat(plExtCost.value) * .135);
        plOHPct.text = calcOH.toFixed(4);
        
        // CALC: Duty Cost
        var plDutyCost = XDocument.DOM.selectSingleNode("/dfs:myFields/dfs:dataFields/d:tblDetailsInfo/@plDutyCost");
        var plDutyPct = XDocument.DOM.selectSingleNode("/dfs:myFields/dfs:dataFields/d:tblDetailsInfo/@plDutyPct");
        var calcDutyCost = (parseFloat(plDutyPct.value) * parseFloat(plExtCost.value));
        plDutyCost.text = calcDutyCost.toFixed(4);

        // Set handler back to zero to prevent "infinite loop" error.
        handler = 0;
    }

     

    Thanks in advance for the help.  I have tried to run in debug and set stop points, but I haven't seemed to be able to get it to work.  So I have put in XDocument.UI.Alert(field_name); in to test and then only pop once so I am assuming they are not firing the second time?

     

    Thanks!

     

     


  • 04-17-2010 04:25 AM In reply to

    Re: The 2nd row of a repeating table does not calculate fields via code behind?

    To run your JScript in debug, simply type debugger; on a line before the code you want to be able to step through (thank goodness for MSDN!).

    You are using absolute XPath, so that is going to be one problem you are having. I notice a variable called 'handler' -- and you mention it is to prevent an infinite loop error? What was happening there?

    The OnAfterChange event is going to fire twice when your field changes, so you should modify the first if statement to make sure that the calculation code only runs when the operation is = to Insert. So change this:

     if (eventObj.IsUndoRedo)

    To this:

     if (eventObj.IsUndoRedo || eventObj.Operation != "Insert")

    Then if the operation is Delete (which is going to run when you clear the existing value in the cell, before you fill in a new value), you'll just return and your caclulation code won't run.

    Next, you'll want to use eventObj.Site and relative XPath to get your node values, so that you have the values for the same row as the one with the changed field. For example, instead of this:

     var plQtyPer = XDocument.DOM.selectSingleNode("/dfs:myFields/dfs:dataFields/d:tblDetailsInfo/@plQtyPer");

    Try this:

     var plQtyPer = eventObj.Site.selectSingleNode("../@plQtyPer");

    Since you are running you code when the plPartCost field changes, you'd reference that node simply as eventObj.Site, like this:

    var plPartCost = eventObj.Site;

    Finally, I'm not sure why your code isn't running every time the plPartCost field changes, my guess may be that the 'handler' variable isn't always what you think it is. Type debugger; right above that variable so that you can try stepping through your code.

    Hilary Stoupa

  • 04-20-2010 07:28 AM In reply to

    Re: The 2nd row of a repeating table does not calculate fields via code behind?

     Once again, you are brilliant!  After changing from absolute to relative it worked perfectly.

     

    The handler variable was something I found in these forums.  I was having an issue with something about infinite loop possibilities (can't remember the error) and saw a post where someone mention using that variable.  It worked perfectly.

     

    Now I have to figure out how to get the default NaN out of calculated fields when I insert a new row.  I want the default to be 0.

     

    Thanks for the excellent help!

     

    Chris

  • 04-20-2010 06:23 PM In reply to

    Re: The 2nd row of a repeating table does not calculate fields via code behind?

    The user won't ever enter data in this field, right? Why not just hide it if it is NaN?

    Hilary Stoupa

Page 1 of 1 (6 items)
Copyright © 2003-2019 Qdabra Software. All rights reserved.
View our Terms of Use.