Delete documents over the Web using Ajax and JavaScript
SearchDomino.com member Rishikesh Sahi provides step-by-step instructions on how to delete documents over the Web using Ajax and JavaScript -- and without having to refresh the current open window.
The following step-by-step instructions explain how to delete document(s) over the Web using Ajax -- without having to refresh the current open window.
- Write your own table tag inside a view that you're going to show on the Web. This will give you the freedom to have complete control over the feel and look.
- In the first column, write the following code for showing a checkbox:
"<tr id="+@Text(@DocumentUniqueID)+"> <td width=10%><input id="$$SelectDoc_"+ @Text(@DocNumber) +"" type=checkbox value=" + @Text(@DocumentUniqueID) + "></td>"
- Now, make more columns with what you need and put them into the <td> as follows:
"<td align=left>" + @Text(urfield) + "</td>"
- Check the view property "Treat view contents as HTML."
- Embed this view into a form inside the <table> tag as follows:
<ur delete button goes here> <table border=0 cellspacing=2 cellpadding=2 width=100%> <ur embedded view come here> </table>
- On the "Delete" button, click the call function called "getChkBoxValue()."
- Put the code below in your JavaScript:
var strUIDs=""; var Sep=""; var req; var count=0; function getChkBoxValue() { var msg; msg= "Are you sure you want to delete the selected document ? "; var agree=confirm(msg); if (agree) { 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 & & chkboxes.item(i).value !="0") { strUIDs = strUIDs +Sep+chkboxes.item(i).value ; document.getElementById(chkboxes.item(i).value). style.display='none'; Sep="~@~"; } } } url="http://137.55.222.154/Rishi/CustomerDetails.nsf/ deleteDocument?OpenAgent&processUNID="+strUIDs; loadXMLDoc(url); } else { return false } } function loadXMLDoc(url) { req = false; if(window.XMLHttpRequest) { try { req = new XMLHttpRequest(); } catch(e) { req = false; } } else if(window.ActiveXObject) { try { req = new ActiveXObject("Msxml2.XMLHTTP"); } catch(e) { try { req = new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) { req = false; } } } if(req) { req.onreadystatechange = processReqChange; req.open("GET", url, true); req.send(""); } } function processReqChange() { if (req.readyState == 4) { if (req.status == 200) { xmlStr=req.responseText alert(xmlStr); } else { alert("There was a problem retrieving the XML data:n" + req.statusText); } } } function toggleChkBox() { if (document.forms[0].chkAll.checked) { selectAll(); } else { deselectAll(); } } function selectAll() { d = document.forms[0]; for (i = 0; i < d.elements.length; i++) { if (d.elements[i].type == "checkbox") { d.elements[i].checked = true; } } } function deselectAll() { d = document.forms[0]; for (i = 0; i < d.elements.length; i++) { if (d.elements[i].type == "checkbox") { d.elements[i].checked = false; } } } var j=1; function HideBorder() { var chkboxes = document.getElementsByTagName("input"); for(i=0;i<chkboxes.length;i++) { if(chkboxes.item(i).attributes[ "type"].value == "checkbox") { if (chkboxes.item(i).id .indexOf("$$SelectDoc_") ==0) { document .getElementById("$$SelectDoc_"+j) .style.borderRight='0'; document .getElementById("$$SelectDoc_"+j) .style.borderLeft='0'; document .getElementById("$$SelectDoc_"+j) .style.borderTop='0'; j++; } } } }
***In the above JavaScript, there is: an XMLHTTp call for the agent to delete the document; the function to get selected document UID; the function to remove ugly borders from the checkbox; and the last function called "toggleChkBox" -- which is used to select and deselect all checkboxes from one single checkbox , per Yahoo style.
- The next step is to write an agent called "deleteDocument" and paste the below code :
On Error Goto errhandler Print "Content-Type:text/plain" Dim session As New NotesSession Dim currDb As NotesDatabase Dim coll As NotesDocumentCollection Dim strEmpId As String Dim doc As NotesDocument Dim currDoc As NotesDocument Dim tmpdoc As NotesDocument Dim view As NotesView Set currDb= session.CurrentDatabase Set currDoc=session.DocumentContext Set view=currDb.GetView("DeleteDocument") strRowDocUID= Strrightback(currDoc.QUERY_STRING(0),"=") strDocUID=Split(strRowDocUID,"~@~") For i=0 To Ubound(strDocUID) Set tmpdoc= currDb.GetDocumentByUNID(strDocUID(i)) Call tmpdoc.replaceitemvalue("Delete","Yes") Call tmpdoc.save(True,False) Next Print i & " document(s) successfully deleted" Exit Sub errhandler: Print "Error" & Str(Err) & ": " & Error$ Exit Sub
- Now, write the following selection formula to the view you are showing on the Web.
SELECT Form="frmCustomerDetails" & Delete !="Yes"
- The last step is to write one agent to delete those documents whose flag is Delete="Yes."
The method used above is only secure if the agent "deleteDocument" is flagged as "Run as Web User." Otherwise, anyone with read access could delete documents in the database.
—Markus K.
******************************************
In my opinion, just because you can accomplish this with Ajax, doesn't necessarily mean you should. With something like deleting documents, that could be misused. I think this tip adds a layer of unneeded complexity. The function already exists using various standard methods over the Web, such as URL commands, etc. I think you should wait for a reload if they are deleting multiple documents.
—Jason H.
Do you have comments on this tip? Let us know.
This tip was submitted to the SearchDomino.com tip library by member Rishikesh Sahi. Please let others know how useful it is via the rating scale below. Do you have a useful Lotus Notes, Domino, Workplace or WebSphere tip or code snippet to share? Submit it to our monthly tip contest and you could win a prize.