Wednesday, April 15, 2009

move selected data form one list to other (JavaScript)

/**
* This javascript file helps you to transfer data between two lists
* Modified by Nimish.
**/

/*
*** To impelment this you have to create two list box with multilpe select option yes
*** Create a hidden field for storing the value in the destination list seperated by | symbol

//This is the source list box


//This button is used for move all value from source list to destination list


//This button is used to move only selected value from source list to destination list


//This button used to remove the selected values from the source list and move it to destination list
//and store the value in the source list to the hidden field


//This button is used to remove all the values from source list and move it to destination list


//hidden field to store the values in the destination list


//destination list

*/

function addOption(selectbox,text,value )
{
var optn = document.createElement("OPTION");
optn.text = text;
optn.value = value;
selectbox.options.add(optn);
}

function removeOption(listbox,i)
{
listbox.remove(i);
}

/**
* This method remove the value from the list which is alredy there in
* the array passed as second argument of this method.
**/
function removeExisting(sourceobj,fldData)
{
for(k=0;k
for(l=0;l
if(sourceobj[l].value==fldData[k])
{
removeOption(sourceobj,l);
}
}
}
}


function replaceAll(str,pcFrom, pcTo)
{
var i = str.indexOf(pcFrom);
var c = str;
while (i > -1)
{
c = c.replace(pcFrom, pcTo);
i = c.indexOf(pcFrom);
}
return c;
}

function transferOption_list(sourceid,destid)
{
sourceobj=document.getElementById(sourceid);
for(i=sourceobj.options.length-1;i>=0;i--){
if(sourceobj[i].selected){
addOption(document.getElementById(destid),sourceobj[i].text, sourceobj[i].value);
removeOption(sourceobj,i);
}
}
}

/**
* This method transfer the selected values from source list to destination list
* and stores the value in the desination list stored into the hidden field result
**/
function addOption_list(sourceid,destid,resultid){
transferOption_list(sourceid,destid);
fillResultBox(destid,resultid);
}

function removeOption_list(sourceid,destid,resultid){
transferOption_list(sourceid,destid);
fillResultBox(sourceid,resultid);
}

function transfer_all_Option(sourceid,destid){
var sourceobj=document.getElementById(sourceid);
for(i=sourceobj.options.length-1;i>=0;i--) {

addOption(document.getElementById(destid), sourceobj[i].text, sourceobj[i].value);
}
removeAllOptions(sourceobj);
}

/**
* This method move all value from sourcelist to detination list and store resulted value in the
* hidden field separated by "|" symbol
**/
function move_all_Option(sourceid,destid,resultid){
transfer_all_Option(sourceid,destid);
fillResultBox(destid,resultid);
}

/**
* This method remove all value from sourcelist to detination list and store values in the destination
* list into the hidden specified by resultid separated by "|" symbol.
**/
function remove_all_Option(sourceid,destid,resultid){
transfer_all_Option(sourceid,destid);
fillResultBox(sourceid,resultid);
}

function removeAllOptions(selectbox)
{
var i;
for(i=selectbox.options.length-1;i>=0;i--)
{
selectbox.remove(i);
}
}

//No need to call from application.
function fillResultBox(sourceid,resultid)
{
var i;
var result="";
var sourceobj=document.getElementById(sourceid);
var len=sourceobj.options.length;
for(i=0;i
{
result+=sourceobj[i].value;
if(i+1
result+="|";

}
document.getElementById(resultid).value=result;
}


No comments:

Post a Comment

LinkWithin

Related Posts with Thumbnails