Expand my Community achievements bar.

Removing instances of dynamically generated subform instances

Avatar

Former Community Member
I am survey document. I have a button that when click loops through the questions and if it finds a certain value, it adds an instance to my "results" subform with the answer from the question.



function CreateSOD(parentSubform)

{

var allChildElements;

var intNumElements;

var currentElement;

var j;

var counter = 1;



//Get all the child nodes of the parent element

allChildElements = parentSubform.nodes;



//Total number of element in the object

intNumElements = allChildElements.length;



//Loop through all the child elements

for(j=0; j < intNumElements; j++)

{

currentElement = allChildElements.item(j);



if(allChildElements.item(j).className == "subform")

{

if(currentElement.name == "Section")

{

// grab all of the "not met" tags

if(currentElement.ddlChoice.rawValue == "2")

{

// add a new instance

TopmostSubform.SOD.SODSection.instanceManager.addInstance();



//set the text values

TopmostSubform.SOD.SODSection.all.item(counter).Reg.rawValue = currentElement.Reg.caption.value.text.value;

TopmostSubform.SOD.SODSection.all.item(counter).RegText.rawValue = currentElement.RegText.rawValue;

TopmostSubform.SOD.SODSection.all.item(counter).TextField.rawValue = currentElement.TextField.rawValue;



//debug info

console.println(currentElement.name + "[" + counter + "]");



counter++;

}

}

CreateSOD(currentElement);

}

}

}



The problem I am having, is that if the user clicks the button again, the index of the results subform changes and the values get all messed up.



I've looked everwhere, but I need to remove all instances of the results subform, then repoulate it using the script above. I've tried using setInstances(), removeInstances() etc and I seem to be missing something.



Anyone know how to "reset" the instances before I do the looping and adding?



Many Many Thanks!
1 Reply

Avatar

Former Community Member
I figured it out, for anyone interested here's the way to do it:



var allCount = TopmostSubform.SOD.SODSection.all.length;



if(allCount > 0)

{



for(var i=allCount-1; i >= 0; i--)

{

TopmostSubform.SOD.SODSection.instanceManager.removeInstance(0);

}

}