@DBColumn and @DBLookup for all browsers using Ajax
Learn how to use this generic code that can be used to perform @DBColumn and @DBLookup using Ajax in Internet Explorer, Mozilla and Netscape.
This generic code can be used to perform @DBColumn and @DBLookup using Ajax (Asynchronous JavaScript and XML) in all browsers. It's perfectly tested on Mozilla, Netscape and of course Internet Explorer. And it's easy to use without changing any line from the code.
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.lengthk+=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.lengthp++) { 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.lengthiCount++) { 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 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) }
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
Renaud T.
******************************************
To allow this function to lookup multi-value fields from views, simply change the following function from this:
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) ]
to this:
function populatelookup(responseXML) { NodeList = responseXML. getElementsByTagName("viewentry") var tmplkpstr=""; var sep=""; var multiNodeListNbr; for(var i=0; i<NodeList.length; i++) { // is this a multivalue list, then the statement below will equal greater than 1 multiNodeListNbr = NodeList[0]. getElementsByTagName("text").length; if (NodeList[0]. getElementsByTagName("text").length == 1) { tmplkpstr = tmplkpstr+sep+NodeList[i]. getElementsByTagName ("text")[0].childNodes[0].nodeValue; sep="#" ] else { // interate through all the list entrie for (var z = 0; z <multiNodeListNbr; z++) { tmplkpstr = tmplkpstr+sep+ NodeList[0].getElementsByTagName ("textlist")[0].getElementsByTagName ("text").item(z).firstChild.data; sep="#" ] ] ] finallkpresult = tmplkpstr.split("#") writeInCombo(finallkpresult,ObjCombo2) ]
Do you have comments on this tip? Let us know.
This tip was submitted to the SearchDomino.com tip exchange by member Rishikesh Sahi. Please let others know how useful it is via the rating scale at the end of the tip. Do you have a useful Notes/Domino tip or code to share? Submit it to our bimonthly tip contest and you could win a prize and a spot in our Hall of Fame.