Multiple lookups using ajax

Hi,Scenario:–I have four fields(combo box) say, A,B,C,B for example in a web application.

I have to get values using a view in the same database using

AJAX dbcolumn and dblookup.

Field A–dbcolumn.

Field B–dblookup(based up on the value choosen in A field).

Field C–dblookup(based up on the value choosen in B field).

Field D–dblookup(based up on the value choosen in C field).

Problem:-- How to perform muliple dblookups using AJAX.

Subject: Multiple lookups using ajax

  1. Load your first combo box A normally with @dbcolumn.2. Then onChange() of A write the ajax script and populate combo B.

  2. Then onChange() of B write the ajax script and populate combo C.

  3. Then onChange() of C write the ajax script and populate combo D.

  4. When not a new doc and in edit mode you need to make sure to write up a script onLoad() of the form to again get the values for B, C, D and repopulate them and also make sure to select the value previously selected.

HTH

Sai

Subject: RE: Multiple lookups using ajax

At first thank you for your help,but I am facing the following problem.

Error(s):-- If I call the dblookup function in the onchange event of “B” combo box(which is inheriting the values based upon"A"combo box using dblookup) then the code fails.

Problem:-- What are the changes that has to be made to use the following code for multiple dblookups i.e for fields “C”, “D”

Code:–Following is the code that i have used to get the

Dbcolumn and dblookup for A and B fields.

var req;

var attr;

var xmlkey;

var resultColXML= new Array()

var resultlkpXML= new Array()

var ObjCombo1;

var ObjCombo2;

var lkpKey;

/*---------------------------

Dbcolumn function start here

-------------------*/

function dbColumn(server,path,view,column,ObjCombo1)

{

ObjCombo1=A

//Put your first combo name here to get dbcolumn result

var pos=0;

currURL =(document.location.href).toLowerCase();

if (trim(server) == “”)

{

pos = currURL.indexOf(‘://’);

if (pos < 0 )

server = “http://10.10.10.120

// Put your server name here

else

{

pos += 3;

pos = currURL.indexOf(‘/’, pos);

server = currURL.substring(0, pos)

alert(server)

}

}

if( trim(path) == “” )

{

if( pos > 0 )

{

newPos = currURL.indexOf('.nsf',pos);

if (newPos > 0)

{

path = currURL.substring

(pos+1,newPos+4)

}

}

}

if( !isNaN(column) )

column -= 1;

vurl = “/”+trim(path)+“/”+view+“?Readviewentries”

//checking whether browser is Mozila or Netscape

if (window.XMLHttpRequest)

{

req = new XMLHttpRequest();

req.overrideMimeType(‘text/xml’);

req.onreadystatechange =

processReqChange_Col;

req.open(“GET”, vurl , true);

req.send(null);

  } 

//checking whether browser is IE

else if (window.ActiveXObject)

{

req = new ActiveXObject(“Microsoft.XMLHTTP”);

if (req)

{

req.onreadystatechange = processReqChange_Col;

req.open(“GET”, vurl, true);

req.send();

       }

    }

}

/* Function used to check whether XML file loaded completely or not */

function processReqChange_Col()

{

if (req.readyState == 4)

/*Only process if XML file is loaded completely:4=“Complete” */

{

     if (req.status == 200)        

/Only process if everything is ok/

{

response = req.responseXML.

documentElement;

populateColumn(response)

    } 

else 

{

alert(“There was a problem retrieving the XML data:n” + req.statusText);

       }

   }

}

/* Function used to extract value one by one from XML file */

function populateColumn(responseXML)

{

NodeList = responseXML.getElementsByTagName(“viewentry”)

for (var k=0;k<NodeList.length;k+=1)

{

child =

responseXML.getElementsByTagName

(‘text’)[k].firstChild

while (child != null)

{

filterNode =

responseXML.getElementsByTagName(“viewentry”)

var filterItem = filterNode.item(k);

posNumber = filterItem.getAttribute(“position”);

if (( child.nodeType == 3) &&

(posNumber.indexOf(“.”)==-1))

{

resultColXML[k] = trim(child.nodeValue);

       } 

       child = child.nextSibling 

   }   

 }

  var finalresult= new Array()

  var tmpstr="";

  var sep="";

for (var p=0;p<resultColXML.length;p++)

{

if (resultColXML[p] != ‘’ && resultColXML[p] != null )

{

tmpstr =tmpstr+sep+trim(resultColXML[p])

sep="#"

}

}

finalresult = tmpstr.split(“#”)

writeInCombo(finalresult,ObjCombo1)

}

/* Function used to trim string */

function trim(str)

{

return str.replace(/^s+/g, ‘’).replace(/s+$/g, ‘’);

}

/*---------------------------

writing in combo function start here

-------------------*/

function writeInCombo(data,fldCombo)

{

fldCombo.length=0

fldCombo.length +=1

fldCombo[fldCombo.length-1].text = “–Select–”

for(iCount=0;iCount<data.length;iCount++)

{

fldCombo.length +=1

strText=data[iCount]

if (strText !=undefined && strText != null)

{

fldCombo[fldCombo.length-1].text = trim(strText)

}

}

}

/*---------------------------

Dblookup function start here

-------------------*/

function dbLookup(server,path,view,key,column,ProjCode0)

{

lkpKey=key

ObjCombo2=B

//Put your second combo name here to get dblookup result

var pos=0;

currURL = (document.location.href).toLowerCase();

if (trim(server) == “”)

{

pos = currURL.indexOf(‘://’);

if (pos < 0 )

server = “http://10.10.10.120

// Put your server name here

else

{

pos += 3;

pos = currURL.indexOf(‘/’, pos);

server = currURL.substring(0, pos)

alert(server)

}

}

if( trim(path) == “” )

{

if( pos > 0 )

{

newPos = currURL.indexOf('.nsf',pos);

if (newPos > 0)

{

 path = currURL.substring(pos+1,newPos+4)

}

}

}

if( !isNaN(column) )

column -= 1;

vurl = “/”+trim(path)+“/”+view+“?Readviewentries&restricttocategory=”+lkpKey

//checking whether browser is mozila or Netscape

if (window.XMLHttpRequest)

{

req = new XMLHttpRequest();

req.overrideMimeType(‘text/xml’);

req.onreadystatechange = processReqChange_lookup;

req.open(“GET”, vurl , true);

req.send(null);

  } 

    //checking whether browser is IE 

else if (window.ActiveXObject)

{

req = new ActiveXObject(“Microsoft.XMLHTTP”);

if (req)

{

req.onreadystatechange =processReqChange_lookup;

req.open(“GET”, vurl, true);

req.send();

       }

    } 

}

/* Function used to check whether

XML file loaded completely or not */

function processReqChange_lookup()

{

if (req.readyState == 4)             

/*Only process if XML

file is loaded completely:4=“Complete” */

{

     if (req.status == 200)           

/Only process if everything is ok/

{

response = req.responseXML.documentElement;

populatelookup(response)

    } 

else 

{

alert(“There was a problem retrieving the XML data:n” + req.statusText);

      }

   }

}

/* Function used to extract

value one by one from XML file */

function populatelookup(responseXML)

{

NodeList =

responseXML.getElementsByTagName(“viewentry”)

 var tmplkpstr="";

 var sep="";

for(var i=0; i<NodeList.length; i++)

{

tmplkpstr =

tmplkpstr+sep+NodeList[i].getElementsByTagName

(“text”)[0].childNodes[0].nodeValue

sep=“#”

}

finallkpresult = tmplkpstr.split(“#”)

writeInCombo(finallkpresult,ObjCombo2)

}

On load event:–

ObjCombo1=document.forms[0].A;

ObjCombo2=document.forms[0].B;

dbColumn(“http://10.10.10.120”,“xxx.nsf”,“vwView”,1,ObjCombo1)

On change event:–(combo box “A”)

keyVal=ObjCombo1.options

[ObjCombo1.selectedIndex].text;

ObjCombo2.length=0

dbLookup(“http://10.10.10.120”,“LT-New/LTWeb.nsf”,“vwProjects”,keyVal,2,ObjCombo2)