javascriptlivecycle

Adding new instance of row, unable to update cell raw values


I've stumped myself on what I thought was a pretty simple JavaScript function. The function takes 2 parameters. The first is a listbox (oField), and the second is a table (nTable).

The listbox is populated by the user, and each time this listbox's values are changed, I want the function below to be called, to recreate nTable, the original header, and rows for each value in the list box.

function table_build(oField, nTable){
    //clear table when selection changes
    nTable._HeaderRow.setInstances(1);
    //add new selection to table
    for(i=0;i<oField.length;i++){
        nTable._HeaderRow.addInstance(true);
        nTable.HeaderRow[i+1].Cell1.rawValue = oField.getDisplayItem(i);    
    }   
}

The issue that I am having is with the following line:

nTable.HeaderRow[i+1].Cell1.rawValue = oField.getDisplayItem(i); 

I am able to update the value of the header row using:

nTable.HeaderRow.Cell1.rawValue = oField.getDisplayItem(i); 

Which is weird, because I would have expected it to require HeaderRow[0] after the new instance is added.

What is wrong here?


Solution

  • I have worked out a solution. The addInstance() function returns the new row instance. So I have updated my table_build() function to assign that value to a variable(nRow), then use nRow to reference the row.

    var nRow = nTable._HeaderRow.addInstance(true);
    nRow.Cell1.rawValue = oField.getDisplayItem(i);
    

    Hope this helps anyone else with a similar query :)