Home > Domino Tips > Developer > Delete documents over the Web using Ajax and JavaScript
Domino Tips:
EMAIL THIS
 TIPS & NEWSLETTERS TOPICS 

JAVASCRIPT

Delete documents over the Web using Ajax and JavaScript


Rishikesh
09.19.2006
Rating: -2.85- (out of 5)


Lotus Notes, Domino, Workplace and WebSphere tips and advice
Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us    Add to Google


VIEW MEMBER FEEDACK TO THIS TIP

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.

  1. 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.

  2. 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>"
  3. Now, make more columns with what you need and put them into the <td> as follows:
    "<td align=left>" + @Text(urfield) + "</td>"
  4. Check the view property "Treat view contents as HTML."

  5. 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>
  6. On the "Delete" button, click the call function called "getChkBoxValue()."

  7. 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.

  8. 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
  9. Now, write the following selection formula to the view you are showing on the Web.
    SELECT Form="frmCustomerDetails" 
    & Delete !="Yes"
  10. The last step is to write one agent to delete those documents whose flag is Delete="Yes."

MEMBER FEEDBACK TO THIS TIP

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.

Rate this Tip
To rate tips, you must be a member of SearchDomino.com.
Register now to start rating these tips. Log in if you are already a member.




Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us    Add to Google


RELATED CONTENT
Ajax for Lotus Notes Domino
Top 10 Lotus Notes Domino programming and development tips of 2007
Ajax for Lotus Notes Domino
Editing fields in a Lotus Notes view with Ajax
Ajax code equivalent of the @DBColumn formula for Lotus Notes
A bevy of Notes/Domino development tips
A smorgasbord of Notes/Domino development tips
Latest Ajax tools from Nexaweb target SOA, Web 2.0
Ajax threats worry researchers
New chapter and verse on Ajax security
App security tools target Ajax vulnerabilities

JavaScript for Lotus Notes Domino
Trap an attachment path via the Domino file upload control field
Converting Lotus Notes views to XML documents using JavaScript
Mimic Lotus Notes Domino application functionality on the Web
Prevent errors on iFramed pages with JavaScript
Top 10 Lotus Notes Domino programming and development tips of 2007
How to add keyboard functionality for Lotus Notes documents
Validate Lotus Notes Domino fields using JavaScript
How to support Flash objects in any Web browser for a Lotus Notes Domino application
How to validate Lotus Notes forms on a Domino server without losing entered data
A bevy of Notes/Domino development tips

JavaScript
Trap an attachment path via the Domino file upload control field
Converting Lotus Notes views to XML documents using JavaScript
Prevent errors on iFramed pages with JavaScript
How to add keyboard functionality for Lotus Notes documents
Validate Lotus Notes Domino fields using JavaScript
How to support Flash objects in any Web browser for a Lotus Notes Domino application
How to validate Lotus Notes forms on a Domino server without losing entered data
Ajax code equivalent of the @DBColumn formula for Lotus Notes
A bevy of Notes/Domino development tips
Loading XML from JavaScript

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary

DISCLAIMER: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.

HomeTopicsITKnowledge ExchangeTipsAsk the ExpertsMultimediaWhite PapersDomino IT Downloads
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides enterprise IT professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective IT purchase decisions and managing their organizations' IT projects - with its network of technology-specific Web sites, events and magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Reprints  |  Site Map




All Rights Reserved, Copyright 1999 - 2008, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts