Questo
codice generico e' usato per realizzate una DBColumn e DBLookup usando
AJAX (Sinonimo della combinazione Javascript e XML) ed e' funzionante in
tutti i browser. E' stato testato e lavora alla perfezione con Mozilla,
Netscape e Internet Explorer. E' molto semplice da utilizare.
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.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:
1. Make your first column categarized in view (which you
wana use for dbcolumn and dblookup).
2. 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)
3. 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.
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. |
1 Commenti:
L'idea è senza dubbio buona, ma il codice è ridondante. Ci sono grosse quantità di codice che sono ripetute a meno di una riga di codice.
Inoltre non viene gestito alcun feedback per l'utente durante la richiesta al server, e non è gestito un time-out per la richiesta al server che potrebbe perdersi nel nulla senza alcuna risposta all'utente.
Infine una considerazione o meglio una domanda. Ottima l'idea di caricare i valori del secondo combo con AJAX ma perchè popolare anche il primo campo con una richiesta asincrona nell'onload della pagina quando posso farlo direttamente con una dbColumn nei valori del campo. Con il metodo proposto il server mi dà la pagina, poi popolo i valori del primo campo con i valori letti dalla vista mediante richiesta asincrona. Se invece faccio direttamente la @dbColumn nei valori del campo quando mi arriva la pagina il campo è già valorizzato: fine delle operazioni, posso
già selezionare.
Sto lavorando su queste idee quando avrò finito posterò una mia versione del codice.
Ciao