Discussions
Categories
Groups
Community Home
Categories
INTERNAL ENABLEMENT
POPULAR
PUBLIC CLOUD
PRIVATE CLOUD
Quick Links
MY LINKS
HELPFUL TIPS
Back to website
Home
Web CMS (TeamSite)
Re-Populate Dynamic Elements
whysguy
Greetings,
I have a DCT that makes heavy use of FormAPI to dynamically display pick-lists, and CGI server calls (via FormAPI) to populate the pick-lists. My problem is that I don't know how to re-populate those dynamic elements once the DCR has been saved. When I reopened a saved DCT, it did not display the fields that were visible, and in one case only populated the pick list with the numeric value in place of the option label.
Any suggestions on what the best approach is?
Thanks
- Billy
Find more posts tagged with
Comments
Migrateduser
This is a really big problem. Whatever logic you use to load the options when the DCR is created should be called when an existing DCR is opened, but it has to leave the selected option selected. This code might provide some help:
/**
* getSelectedValues( value, array )
* Returns true if the specified value appears in the specified array.
**/
function isValueInArray( value, array )
{
var result = false;
for( var i = 0 ; i < array.length ; i++ )
{
if ( value == array
)
{
result = true;
}
}
return( result );
}
/**
* getSelectedValues( item )
* Returns an array of the values of the options selected in the specified
* item.
**/
function getSelectedValues( item )
{
var indexes = item.getValue();
var results = new Array();
if ( indexes == 0 || ( indexes != undefined && indexes != null && indexes != '' ))
{
var options = item.getOptions();
if ( item.getType() == 'radio' )
{
results[0] = options[indexes].value;
}
else
{
for( var i = 0 ; i < indexes.length ; i++ )
{
results
= options[indexes
].value;
}
}
}
return( results );
}
/**
* repopulateOptions( item, values, doSort, doRedraw )
* Repopulates the specified item with the value/labels in the values
* hashtable (object). Sorts and redraws depending on the Booleans
* provided.
**/
function repopulateOptions( item, values, doSort, doRedraw )
{
var chosen = getSelectedValues( item );
var options = new Array();
var i = 0;
for( var prop in values )
{
var selected = isValueInArray( prop, chosen );
options[i++] = new Option( values[prop], prop, selected, selected );
}
item.setOptions( options );
if ( doSort )
{
sortOptionsByLabels( item );
}
if ( doRedraw )
{
IWDatacapture.redraw();
}
return( true );
}
function populateSomething( item )
{
var config = new Object();
//could come from database (but callServer is not synchronous!) so better to populate from an XML file.
config['value1'] = 'label1';
config['value2'] = 'label2';
return( repopulateOptions( item, config, true, false ));
}
/**
* sortOptionsByLabels( item )
* Sorts the options in the specified item by their labels. All options
* must have unique labels!
**/
function sortOptionsByLabels( item )
{
var oldOptions = item.getOptions();
var optProps = new Object();
var keys = new Array();
var j = 0;
for( var i = 0 ; i < oldOptions.length ; i++ )
{
keys[j++] = oldOptions
.text;
optProps[oldOptions
.text] = new Object();
optProps[oldOptions
.text].value = oldOptions
.value;
optProps[oldOptions
.text].defaultSelected = oldOptions
.defaultSelected;
optProps[oldOptions
.text].selected = oldOptions
.selected;
}
keys.sort();
var newOpts = new Array();
for( var i = 0 ; i < keys.length ; i++ )
{
newOpts
= new Option( keys
, optProps[keys
].value, optProps[keys
].defaultSelected, optProps[keys
].selected );
}
item.setOptions( newOpts );
return( true );
}
whysguy
Hi John, thanks for the reply. I have a few questions about the code you posted. Are all of the functions you posted used? If so, which one is the main function? Also, what is this code used for:
config['value1'] = 'label1';
config['value2'] = 'label2';
What should I replace value and label with?
Thanks
- Billy
Migrateduser
I think I only posted functions that I actually use.
That hashtable contains the key/value pairs of the options you want to populate (replace this with whatever values you are using). You can probably just set the labels to be the same as the values.
The main function would be populateSomething.
whysguy
OK John,
I feel like a real dunce. Here is what I am doing - I am getting the data from the database and passing that into the populateSomething function along with the item name. It is populating the drop down, but it is not selecting the item that was selected. Any thoughts?
populateSomething(HighLevelCategoryItem, DocCategories);
function populateSomething( item, itemOptions )
{
var config = new Object();
//could come from database (but callServer is not synchronous!) so better to populate from an XML file.
// config['value1'] = 'label1';
// config['value2'] = 'label2';
for (var i = 0; i < itemOptions.length; i++)
{
//var currentIndex = categories
;
var thisValue = itemOptions
.value;
var thisLabel = itemOptions
.text;
config[thisValue] = thisLabel;
}
return( repopulateOptions( item, itemOptions, false, false ));
//alert (itemOptions.length);
}
Thanks again
Migrateduser
Sorry, I am not following. I don't see where you are retrieving the options from the database. I am pretty sure that code worked, so I would guess that the value in the DCR does not match the value in the database?
Migrateduser
Now I get it, sorry. You are using FormAPI to populate the options (which clears the selected value), then using FormAPI to repopulate them and it is losing track of the selected option. I think whatever function you are using to populate the options from the database should be modified - that is where you should call repopulateOptions. So instead of populating the options, populate the config hashtable directly, then call repopulateOptions. If you send me your .js I can try to fix it.
But it could be much simpler than that - in the code you are using to populate the options, just check the value of the item and in your loop to populate options, just add true, true to the initialization list if there is a match.
Crikey Interwoven this kind of logic should come with the product...
whysguy
Thanks again John. I started to make the changes but I thought I might be off by just a bit and you might be able to put your finger on it. Here is my JS, if you have any ideas I would appreciate it. Also, I agree, this should be part of the application.
Thanks
- Billy
Thanks
- Billy
Migrateduser
Ahah, now it all makes sense and you can totally ignore all of my code. You are using callServer to call a CGI to populate the options. I believe that what you want to do is add a parameter containing the selected value to what you are passing to callServer, then modify your ipl to set that option as selected. Can you post or email the .ipl?
var params = new Object();
params['selected'] = // (get the existing value of the item)
IWDatacapture.callServer( "/iw-bin/USAllianz/dbutilitles/loadInitialData.ipl", params, true );
I know post is better than get but with get it passes the parameters on the URL so it's visible in access_log which can help in debugging.
Then modify the .ipl to check the value of the CGI poaram 'selected', when looping over the DB results compare each to this value, if there is a match then
new Option( text, key, true, true )
instead of false, false.
whysguy
Thanks John. I think that is the route I am going to take. I really appreciate you taking the time to trouble-shoot with me. I will let you know how it goes.
Thanks
- Billy
whysguy
Howdy,
I just thought I would post my solution in case it might be helpful to someone else trying to do the same thing.
rePopulateMultiSelects(StatesItem, statesOption);
function rePopulateMultiSelects( myItem, itemOptions )
{
var newItemOptions = new Array();
var itemValues = new Array();
itemValues = myItem.getOptions();
for (var i = 0; i < itemOptions.length; i++)
{
var thisValue = itemOptions
.value;
var thisLabel = itemOptions
.text;
newItemOptions
= new Option(thisLabel, thisValue, false, false);
for (var j = 0; j < itemValues.length; j++)
{
var itemValue = itemValues.value;
if (thisValue == itemValue)
{
newItemOptions
= new Option(thisLabel, thisValue, true, true);
}
}
}
myItem.setOptions(newItemOptions);
IWDatacapture.redraw();
}
If anyone has questions about this code, please don't hesitate to ask.
Thanks
- Billy