Subject: Ajax dbcommand
Here is an article that I found some time ago, hope it helps:
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,Subject)
{
ObjCombo1=Subject //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.50.3.107” // 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(server)+“/”+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 he 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,Name)
{
lkpKey=key
ObjCombo2=Name //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.50.3.107” // 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(server)+"/"+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 he 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)
}
Steps to use:
-
Make your first column categarized in view (which you wana use for dbcolumn and dblookup).
-
Assume you have to combo on form. So, call function Dbcolumn on OnLoad of the form:
ObjCombo1=document.forms[0].Combo1;
ObjCombo2=document.forms[0].Combo2;
dbColumn(“serverName”,“DbName”,viewname,1,Combo1)
- When you select any option from combo1 then its respective value should come in second combo. So, call dblookup function on OnChange of first combo:
keyVal=ObjCombo1.options[ObjCombo1.selectedIndex].text;
ObjCombo2.length=0
dbLookup(“ServerName”,“DBName”,viewname,keyVal,2,ObjCombo2)
The last parameter in both functions is comboBox name object, which is on your form used to display options.
--------------------------------------------------------------------------------MEMBER FEEDBACK TO THIS TIP
If you run this script on a server using a fully qualified hostname (like http://www.myserver.com) as server it won’t work and will create a security issue in both IE and Firefox, resulting in a Javascript security error.
To have it works with a fully qualified hostname, the two lines
vurl = trim(server)+“/”+trim(path)+“/”+view+“?Readviewentries”
vurl = trim(server)+"/"+trim(path)+"/"+view+"?Readviewentries&restricttocategory="+lkpKey
must be changed to:
vurl = “/”+trim(path)+“/”+view+“?Readviewentries”
vurl = "/"+trim(path)+"/"+view+"?Readviewentries&restricttocategory="+lkpKey