AJAX

Updating the HTML DOM

27-Jan-2009 09:46:46

The parseMessages() function handles the incoming XML data. In doing so, it relies on several ancillary functions, such as appendComposer(), getElementY(), and clearTable(). You must also introduce new elements to the index page, such as a second HTML table which serves as the auto-complete box, and ID's for elements so they can be referenced in javascript.js. Finally, you create new variables corresponding to ID's for elements in index.php, initialize them in the init() function that you previously implemented, and add some functionality that is needed each time index.php is loaded.

Note: The functions and elements that you create in the following steps work interdependently. It is recommended that you work through this section, then examine the code once it is all in place.

 

  1. Open index.php in the editor and type in the below code for the second row of the HTML table you previously created.
    <tr>
    <td id="auto-row" colspan="2">
    <table id="complete-table" />
    <td/>

    </tr>
    The second row of the table contains another HTML table. This table represents the auto-complete box that will be used to populate composer names.
  2. Open javascript.js in the editor and the following three variables to the top of the file.
    var completeField;
    var completeTable;
    var autoRow;
  3. Add the following lines (in bold) to the init() function.
    function init() {
    completeField = document.getElementById("complete-field");
    completeTable = document.getElementById("complete-table");
    autoRow = document.getElementById("auto-row");
    completeTable.style.top = getElementY(autoRow) + "px";
    clearTable();

    }
    One purpose of init() is to make elements inside index.php accessible to other functions that will modify the index page's DOM.
  4. Add appendComposer() to javascript.js.
    function appendComposer(firstName,lastName,composerId) {

    var row;
    var cell;
    var linkElement;

    if (isIE) {
    completeTable.style.display = 'block';
    row = completeTable.insertRow(completeTable.rows.length);
    cell = row.insertCell(0);
    } else {
    completeTable.style.display = 'table';
    row = document.createElement("tr");
    cell = document.createElement("td");
    row.appendChild(cell);
    completeTable.appendChild(row);
    }

    cell.className = "popupCell";

    linkElement = document.createElement("a");
    linkElement.className = "popupItem";
    linkElement.setAttribute("href", "autocomplete.php?action=lookup&id=" + composerId);
    linkElement.appendChild(document.createTextNode(firstName + " " + lastName));
    cell.appendChild(linkElement);
    }
    This function creates a new table row, inserts a link to a composer into it using the data passed to the function via its three parameters, and inserts the row into the index page's complete-table element.
  5. Add clearTable() to javascript.js.
    function clearTable() {
    if (completeTable) {
    completeTable.style.display = 'none';
    for (loop = completeTable.childNodes.length -1; loop >= 0 ; loop--) {
    completeTable.removeChild(completeTable.childNodes[loop]);
    }
    }
    }
    This function sets the display of the complete-table element to 'none', (i.e., makes it invisible), and it removes any existing composer name entries that were created. Note that this function is called in init() in order to set the table as invisible by default.
  6. Add getElementY() to javascript.js.
    function getElementY(element){

    var targetTop = 0;

    if (element.offsetParent) {
    while (element.offsetParent) {
    targetTop += element.offsetTop;
    element = element.offsetParent;
    }
    } else if (element.y) {
    targetTop += element.y;
    }
    return targetTop;
    }
    This function is applied to find the vertical position of the parent element. This is necessary because the actual position of the element, when it is displayed, is often dependent on browser type and version. Note that the complete-table element, when displayed containing composer names, is shifted to the lower right of the table in which it exists. The correct height positioning is determined by getElementY().
  7. Modify the doCompletion() function so that any composer entries that display in the auto-complete box are removed if the text field is empty (changes in bold).
    function doCompletion() {
    if (completeField.value == "") {
    clearTable();
    } else {

    var url = "autocomplete.php?action=complete&id=" + escape(completeField.value);
    req = initRequest(url);
    req.open("GET", url, true);
    req.onreadystatechange = callback;
    req.send(null);
    }
    }
  8. Add parseMessages() to javascript.js.
    function parseMessages(responseXML) {
    clearTable();
    var composers = responseXML.getElementsByTagName("composers")[0];
    if (composers.childNodes.length > 0) {
    completeTable.setAttribute("bordercolor", "black");
    completeTable.setAttribute("border", "1");
    } else {
    clearTable();
    }

    for (loop = 0; loop < composers.childNodes.length; loop++) {
    var composer = composers.childNodes[loop];
    var firstName = composer.getElementsByTagName("firstName")[0];
    var lastName = composer.getElementsByTagName("lastName")[0];
    var composerId = composer.getElementsByTagName("id")[0];
    appendComposer(firstName.childNodes[0].nodeValue,
    lastName.childNodes[0].nodeValue,
    composerId.childNodes[0].nodeValue);
    }
    }

The parseMessages() function receives as a parameter an object representation of the XML document returned by the autocomplete.php file. The function programmatically traverses the XML document, extracting the firstName, lastName, and id of each entry, then passes this data to appendComposer(). This results in a dynamic update to the contents of the complete-table element. For example, an entry that is generated and inserted into complete-table might look as follows:

<tr>
<td class="popupCell">
<a class="popupItem" href="autocomplete?action=lookup&id=12">Antonin Dvorak</a>
</td>
</tr>

The dynamic update to the complete-table element represents the final step of the process flow of communication that takes place during communication using Ajax.


 Reply Comment
 
  fbhblxpg ( 10-Dec-2009 23:04:48 )

<a href="http://guxtjsfx.com">mntaeanh</a>  [URL=http://jbtfwjkf.com]dvfubitw[/URL]  narjdjjw http://npyaizdc.com acifxoxf kspnmkvd

 
Your name
Website http://
Comment
   
Image verification code
Retype image code here
   
   

Index : AJAX