I use AjaxCFC currently and have written a few custom JS functions for the DWRUtil.js library. I thought I would share these in case they are useful to anyone else. I found that I needed to these particular tasks quite a lot.
Just drop these into the util.js file
- selectPop()
Populate a select list with an object of arrays returned by AjaxCFC - selectOption()
looks for a specific option value in a select list, if it is there, sets it to SELECTED - getSelected()
get the currently selected item in a SELECT list. if multiple options selected, return as an array - getSelectedText()
As above but gets the text of selected options
/* DWRUtil.selectPop
added by Russ Michaels
Populate a select list with an object of arrays returned by AjaxCFC
e.g.
r.id[0] = 1
r.name[0] = 'Russ'
arguments:-
list : name of select list to populate
obj : object containing array
text : item to be used for option text
value : item to be used for option value
defaultTop : default value to be inserted at top of list
overwrite : boolean, whetehr or not to overwrite existing list
Synjtax:-
DWRUtil.selectPop('customers', r, 'id','name',['select one'], [true])
*/
DWRUtil.selectPop = function(list, obj, text, value, defaultTop, overwrite) {
if(overwrite) $(list).options.length = 0;
//insert a default top item in the list such as **please select**, its value will be 0
if(defaultTop)
$(list).options[0] = new Option(defaultTop, 0);
for(x=0;x<obj[text].length;x++)
{
$(list).options[$(list).options.length] = new Option(obj[text][x], obj[value][x]);
}
};
selectOption = function (list,searchstr)
{
//looks for a specific option value in a select list, if it is there, sets it to SELECTED
for(a=0;a<list.length;a++)
{
if(list.options[a].value == searchstr)
{
list.options[a].selected = true;
}
}
}
getSelected = function (ele)
{
//get the currently selected item in a SELECT list. if multiple options selected, return as an array
element = $(ele);
selectedArray = new Array();
for(x=0;x<element.options.length;x++)
{
if(element.options[x].selected)
selectedArray[selectedArray.length] = element.options[x].value;
}
if(selectedArray.length == 1)
return selectedArray[0];
else
return selectedArray;
}
getSelectedText = function (ele)
{
//gets the text of selected options
element = $(ele);
selectedArray = new Array();
for(x=0;x<element.options.length;x++)
{
if(element.options[x].selected)
selectedArray[selectedArray.length] = element.options[x].text;
}
if(selectedArray.length == 1)
return selectedArray[0];
else
return selectedArray;
}
Sep 30, 2008 at 3:13 PM thanks for this, this is exactly what i was looking for. I was just about to write a function that did the exact same thing when I found this.