Using script to build table

I’m trying to build a table in a script, and am having trouble getting it to work exactly as I want it to, and I’m not certain what I’m doing wrong. I’m trying to display invoice items, and the number of rows per item is conditional.

I’ve played around with the last line of the script, .html versus .after. (.after) formats my lines correctly, but messes up paging, while (.html) pages correctly but doesn’t include the tags for any row after the first one in each item.

Can you help me with the correct/best way to do this?

Table:

        Line 
        Item#/PO/Description 
        Ordered 
        Shipped 
        
            Unit Price
        
        
            Ext. Price

Script using Selector: #Items tbody tr

results.each(function(index) {
var result = “”;
var thisItem = record.tables.InvoiceItem[index];
var cls = “”;
var format = new TextFormatter(locale);

if (thisItem != null){
    if (index % 2 == 0){cls = "odd"; query("#firstrow").attr("class","odd");} 
    else {cls = "even"; query("#firstrow").attr("class","even");}
    
    var custItem = thisItem.fields["custItem"];
    var item = thisItem.fields["item"];
    var lineItem;
    var secondLineItem = "";
    if (custItem == "" || custItem == item)
    {
        lineItem = item ;
    }
    else 
    {
        lineItem = "C/I: " + custItem;
        secondLineItem = item;
    }

    //Add the unit of measure to the first item line
    lineItem = lineItem + ' U/M: ' + thisItem.fields["UOM"] + '&nbsp&nbsp&nbsp';

//.toFixed(2).replace(/(\d)(?=(\d\d\d)+(?!\d))/g, “$1,”)
var extPrice = format.currency(thisItem.fields[“Price”] * thisItem.fields[“QtyShipped”]);
var qtyOrdered = format.grouped(thisItem.fields[“QtyOrdered”]);
var qtyShipped = format.grouped(thisItem.fields[“QtyShipped”]);
var extTotal = extTotal + extPrice;

 //This is the first row, item or cust item, U/M, and ordered/shipped/prices
    result += '';

        result += '' + thisItem.fields["lineNum"] + '';
        result += '' + lineItem + '';
        result += '' + qtyOrdered + '';
        result += '' + qtyShipped + '';
        result += '' + thisItem.fields["Price"] + '';
        result += '' + extPrice + '';
    result += '';

//This is the second row, item if necessary, description
    result += '';
        result += '';
        result += '';
        if (secondLineItem != ""){ result += secondLineItem + "&nbsp&nbsp-&nbsp&nbsp";}
        result += thisItem.fields["itemDescription"] + '';
    result += '';

//Then if necessary, the cust PO, HTS, revision go here
    if (thisItem.fields["CustPO"] != "" || thisItem.fields["HTSCode"] != "" || thisItem.fields["Revision"])
    {
        result += '';
            result += '';
            result += '';
            if (thisItem.fields["Revision"] != ""){result += ' Rev:' + thisItem.fields["Revision"];}                
            if (thisItem.fields["CustPO"] != ""){result += ' ' + thisItem.fields["CustPO"];}
            if (thisItem.fields["HTSCode"] != ""){result += ' HTS:' + thisItem.fields["HTSCode"];}                
        result += '';
    }
    
//Finally loop through the line notes, if there are any, and create a new line for each
    if(thisItem.tables.LineNotes != null) {
        var Notes = thisItem.tables.LineNotes;
        var noteslen = Notes.length;
        //Loop through as many notes as there are
        for (var n=0;n';
                result += ''; //This is the first column - blank
                result += '' + thisNote.fields["Notes"];
            result += '';
        }
    }
    

}
    result += '';
    result += ' ';

this.html(result);

});

Hello Rick,

This is pretty difficult to troubleshoot without being able to see the document and the data. Here’s a theory however:

.after() will add all your string code after the existing TR, which means that empty TR will always be there. This might mess up columns because that TR doesn’t actually have any cell definitions inside of it.

.html() adds your code inside the TR, which of course is absolutely not what you want, since your have a bunch of <tr> tags within a single <tr>.

What I suggest trying first is to actually remove the <tr data-repeat=“”></tr> that’s in the <tbody> tag and just making your script selector straight-up #Items tbody and use .html() , which will just put your code within the tbody. That might be a better starting point.

However I’m curious as to what you consider to be “messes up paging”, as your expected result may not be what this script would actually do. A script like this will not “Detect” what you consider a line item, and would most definitely be able to split the table to a new page in the middle of your list of HTS/Revision/PO lines, or in the middle of your notes. AKA it won’t split right before your “First row”.

Let me know if you have further questions,
~Evie

Thanks for the quick response, Evie.

I have tried the .html on just the #Items tbody selector, and then it will only display the first line…apparently the <tr data-repeat=“”> must control the data looping?

If I try including a text selector inside the tbody (or even the tr) such as @RowDetail@, it reformats the code so it’s above the table.

When I use .after and include the <tr data-repeat=“”>tag, then each line displays properly, but I get this error, it pages badly, and the master page header doesn’t display.error message

Do you have any examples or samples of documents with nested tables and multiple pages worth of data?

I think I figured it out…I was missing the unique data-breakable tag in the additional ""s.

While I’m here…is there any means of controlling the paging while using this method?

Rick,

Here’s the example that I often send to demonstrate the feature (which won’t help you much since you were 95% of the way there already):

sample-scripted-nested-tables.OL-package

As for controlling the paging… I’m not sure exactly what you’re asking. In other words, what do you mean by “Paging”?

Thank your the example, I’m sure it will still be helpful as I learn new stuff.

Paging…just if there is some way to force a new page if I can tell it will split a multi-line invoic?e item line. I’m not sure how to keep track of which line I’m on, and how to force a new page.

At the moment, it’s not possible to control exactly where this break happens, not because you can’t know what line you’re in (you can just straight up add a counter within your script, but you’ll have to keep a tally on each TR you add), but because you cannot tell the table itself to break or not to break. There is of course mid-to-long-term plans to add this kind of functionality but at the moment Overflow is a relative black box in this regards.

That is, unless there’s a way that I’m not aware of. I will ask around just in case someone with a bit more knowledge of Designer can help out.

~Evie

Thanks Evie

I was able to keep a counter as you mentioned and then just looped and added 's as necessary to force the table to page break where I wanted it to. A bit more complex to write and maintain than I’d hoped, but works nicely.

Hi Evie - I was very excited when I saw your document. This is what I am trying to do myself. Except my data extraction is a little different. Would this work if I have nested detail tables in my data mapper? Due to the size of the data we work with I thought it would be easier to extract the following:

PlanClmStats (this will have 13 records)
ClmStats (this will have 5 records for each plan)

The example you provide is for data in one detail table. If possible, how would I change the coding to use the data from the nested detail tables in the data mapper.

Thank you!!

Amy

Hi Amy,

There’s no limit to how many detail tables you have in a data mapping configuration, so you should find there’s no issue achieving what you want. In fact, multiple tables with nested detail tables work fine too!

Obviously you’ll have to modify the script to fit that new structure. But it’s definitely possible!

~Evie

Thank you !! I am a beginner in javascript. I have tried to take your script from your example and tweak it to fit my needs. I have been unsuccessful. Would you be able to take a look at my file and point me in the right direction. I am not sure what fields to be looking at. I will attach my template, data mapper and pdf of what my table needs to look like. I am thinking my script would be a little more simplified than your example. If not I understand. I might have made more of a mess of things. [http://help.objectiflune.com/files/connect-samples/testingNested Tables.zip](http://help.objectiflune.com/files/connect-samples/testingNested Tables.zip)

Thanks

Amy

Hi - is there anyone who can help me on this? I have tried to adjust the script but I am not having any luck getting it to work. I have several reports I’m working on that I need to use this method for or else I have to re-write all the XML’s.

Thank You!!

ahaddad, please open a new question on Q2A in order to get the appropriate help - only 2 people can see your comment at this time. Also, your file did not correctly attach.