Lost in using AJAX

Hi,

Just last week, I was seeking advice on how to go about selecting multiple documents in a view and thereafter grab the info in these documents to be created in a new document. I got response and the steps to go about it and I found myself stumped on using AJAX and web enable Domino applications in general.

I created a column in view as a html checkbox with the key as name and value is the document unique id. How do I proceed here in coding the action button so that it will know to pick up the selected documents’ unique ID to be passed in a LS call to the AJAX handler?

Is there any useful reference that I can read up on to learn how to code a domino web application in terms of data exchange and presentation?

Thank you in advance.

Subject: Lost in using AJAX

Hi,

One way to keep track of selected documents while checking a box is having a javascript function on the onChange event of the checkbox that stores in a javascript array the unique id of the selected documents (and remove the entry from the array when the user uncheck the box).

Then you have to handle the Ajax stuff…

Subject: RE: Lost in using AJAX

Jen,

If you had probelm in understanding n implementing steps , You should tell .Atleast I can write some pseudo code for u .

Your first issue is to get the selected document handle from web. So, you have couple of ways to achieve the same

a) Enable “Allow selection of document” property of view and use following code to get selected document handle.

var strUIDs=“”;

var Sep=“”;

var count=0;

var chkboxes = document.getElementsByTagName(“input”);

for(i=0;i<chkboxes.length;i++)

{

  if(chkboxes.item(i).attributes["type"].value == "checkbox") 

  {

       if (chkboxes.item(i).checked)

     {

     	      	   strUIDs = strUIDs +Sep+chkboxes.item(i).value  ;

     	      	   Sep="~#~";

                   count=count+1;

      }  

   }

}

if (count>0)

{

	alert(strUIDs);

}

else

{

  alert("Please select atleast one document to delete");

}

b)You can also write your own HTML code to make checkbox in first column of the view, like :

“<input id="$$SelectDoc_”+@Text(@DocNumber) +“" type=checkbox value=” + @Text(@DocumentUniqueID) + “>”

c)Last way , same as first Enable “Selection of document” property of the view , you can notice domino will generate checkbox with name “$$SelectDoc”. Just u need to take handle of this field and just loop through all the selected checkbox and handle as I have written code above.

Now next , you can see in above code i got all selected check box value into variable “strUIDs”. Now write AJAX function and pass same variable into AJAX. Eg:

	var sURL = './YourAgent?OpenAgent';

	var sReturn = postCommand(sURL, strUIDs);

You “postcommand” function as follows :

function postCommand(sURL, sParms){

var objHTTP = initXMLHttpRequest();

var sReturn = "";

try{

	objHTTP.open("POST", sURL, false);

	objHTTP.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

	objHTTP.send(sParms);

	sReturn = objHTTP.responseText;

}catch(e){

	if(typeof alertError == "function"){

		alertError(e);

	}

}

return sReturn;

}

Write an agent which will get all UNID and will fetch details of those UNIDs agent name like “youragent” in above argument.

you agent will look like below:

    Print |Content-type:text|



Set nsThisSession = New NotesSession 

Set ndbThisDatabase = nsThisSession.CurrentDatabase         

Set ndocThisDocument = nsThisSession.DocumentContext

    sParmArgs= ndocThisDocument.Request_Content(0)



    sParmsData=Split(sParmArgs,"~@~")



strUserFirstName= Cstr(sParmsData(0))

strUserMidName = Cstr(sParmsData(1))

strUserLastName = Cstr(sParmsData(2))

strUserCompanyName= Cstr(sParmsData(3))

strUserMailAddress= Cstr(sParmsData(4))

strUserOfficePhoneNumber= Cstr(sParmsData(5))

strUserHTTPPassword = Cstr(sParmsData(6))



strUserFirstName=Evaluate({@URLDecode("Domino";"} & strUserFirstName & {")})

strUserMidName=Evaluate({@URLDecode("Domino";"} & strUserMidName & {")})

strUserLastName=Evaluate({@URLDecode("Domino";"} & strUserLastName & {")})

strUserCompanyName=Evaluate({@URLDecode("Domino";"} & strUserCompanyName & {")})

strUserMailAddress=Evaluate({@URLDecode("Domino";"} & strUserMailAddress & {")})

strUserOfficePhoneNumber=Evaluate({@URLDecode("Domino";"} & strUserOfficePhoneNumber & {")})

above is my code which do something else , but just u need to get UID, which u passed from AJAX .

use getDocumentByUNID method and get the document handle.

after geting document handle , fetch all individual fields value and return those values into the calling function like :

CompanyName$=personDoc.GetItemValue(“CompanyName”)(0)

	MailAddress$=personDoc.GetItemValue("MailAddress")(0)

	OfficePhoneNumber$=personDoc.GetItemValue("OfficePhoneNumber")(0)

	HTTPPassword$=personDoc.GetItemValue("HTTPPassword")(0)

End If



Print 	CompanyName$+"~@~"+MailAddress$+"~@~"+OfficePhoneNumber$+"~@~"+HTTPPassword$

You can see the print statement , In AJAX call it wonot print the data rather just return all the values into the calling function.

So, u can get all field values into “sReturn” variable which we used on the top.

Just split all the values bcos we separated all wid “~@~”.

pass all values into the form and compose using JS.

Hope I gave all the things , u just need to arrange …

Still u can contact me if any issues u have

Cheers

Rishi.

Subject: RE: Lost in using AJAX

Thanks for the tip. Had this been a php based solution, I would have no issues on how to script it out. I seem to have problems even to do a post method submission…

Subject: function getSelectedDocs()

Here is the tiny Javascript function to getSelectedDocs from a HTML style Web view (Check 'Allow Selection of documents):

function getSelectedDocs(){

/*

** This function returns selected document UNIDs from an HTML-syle web view

*/

var iX = 0;

var aUNID = ;

var hForm = window.document.forms[0];

if (hForm == null) return;

for (var iY = 0; iY < hForm.elements.length; iY++) {

 var el = hForm.elements[iY];

 if (el.type == "checkbox" && el.name == "$$SelectDoc" && el.checked) {

   aUNID[iX] = el.value;

   iX++;

 }

}

return aUNID;

}

I’m not author of above code. I copied it over from the template ‘StdR4Events’. You can find similar code in the mail template. You can teach yourself by going through the implementation of it in the mail template.

For AJAX stuff, use Prototype.js or any other javascript library/framework. They’ll make your life easier in long run.

HTH

Adi