OK, on closer inspection of nuGetLookupId and nuPopulateLookup some clarity is forming; thank you very much for your help in getting me to this point!
Now my template lookup's id contains no prefix and the instance of it I create has the same id which includes a prefix. I do find that setting my instance's
data-nu-target to the template's id as suggested makes it not work; which makes sense since nuPopulateLookup uses the target to get the prefix which only exists in my instance; not the template.
Anyhow; it now kind of works ... but,
I get a 'Uncaught Error: Syntax error' I would like to clean up. The error comes from the fact that my instance has a prefix so in nuGetLookupId the code drops into nuAddSubformRow and the sfid ends up being empty.
I can add a return if sfid is empty in nuAddSubformRow; but I don't know if that is a good solution, and maybe I'm doing something else wrong?
For completeness here is the now cleaned up code for creating another lookup instance (not form or db object) of a template lookup (a real form object in the db).
Code: Select all
//Called like :
createNewLookUpInstance('nuRECORD', 'hiddenAssyCode_id', 'myPrefix_000', 'My Label Text', 150, 230);
// Create a new instance of a lookup widget already on the form (the template lookup).
// Then append it to the form at a new x,y location.
//
// formContainerIdName = I think this is always 'nuRECORD'
// templateId = ID of template lookup which is already a form object in the DB
// prefix = prefix name for the data-nu-prefix attribute, also gets added
// to templateId to create the ID for the new instance
// instancelabel = Label text for this instance of the template
// x,y = New location coordinates
function createNewLookUpInstance(formContainerId, templateId, prefix, instancelabel, x , y) {
// Create clones of all the templates lookup elements
var newInstance = document.getElementById(templateId).cloneNode(true);
var newInstance_label = document.getElementById('label_' + templateId).cloneNode(true);
var newInstance_idcode = document.getElementById(templateId + 'code').cloneNode(true);
var newInstance_idbutton = document.getElementById(templateId + 'button').cloneNode(true);
var newInstance_iddescription = document.getElementById(templateId + 'description').cloneNode(true);
// Prepare for looping through elements
var elems = [newInstance, newInstance_label, newInstance_idcode, newInstance_idbutton, newInstance_iddescription];
var xAdj = [ 0, -109, 0, 76, 95];
// Update id's & positions
var elem = {}; // Current element in the loop
var attrib = {}; // Current element attribute in the loop
for(var ielems = 0; ielems < elems.length; ielems++) {
elem = elems[ielems];
elem.style.left = (x + xAdj[ielems]) + 'px';
elem.style.top = y + 'px';
// Make label, code and description visiible; we assume template is not
if(ielems > 0) {
elem.style.visibility = 'visible';
elem.style.display = '';
}
// Update the label text
if(ielems == 1) {elem.innerText = instancelabel;}
// Add data-nu-prefix property
if(prefix != '') {elem.setAttribute('data-nu-prefix', prefix);}
// Change any reference to the template ID in the attributes to the newInstance ID
for(var iattribs = 0; iattribs < elem.attributes.length; iattribs++) {
attrib = elem.attributes[iattribs];
if (attrib.specified) {
attrib.value = attrib.value.replace(templateId, prefix + templateId);
}
}
}
// Put the new elements in the form
for(ielems = 0; ielems < elems.length; ielems++) {
elem = elems[ielems];
document.getElementById(formContainerId).appendChild(elem);
}
}