IE + XML + javascript = 0

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • milkyway

    IE + XML + javascript = 0

    Hello,

    Just new to all of this. The code below is supposed to load an XML file
    for use in a table. It doesn't work because of one command:

    table.insertRow command:

    I have seen the specs saying that it should be a -1 or the length of
    the table. I have tried.

    table.insertRow (-1) => error: Invalid Argument
    table.insertRow (table.rows.len gth-1) => error: Invalid Argument
    table.insertRow (0) => error: Invalid Argument

    I have incorporated ALL of the code below because I am at the end of my
    rope on this.

    Thanks to ANYONE who will help me with this.

    Regards.

    <html>

    <head>
    <meta name="GENERATOR " content="Micros oft FrontPage 5.0">
    <meta name="ProgId" content="FrontP age.Editor.Docu ment">
    <meta http-equiv="Content-Type" content="text/html;
    charset=windows-1252">
    <title>Table Samples</title>


    <script>
    function findTable(id)
    {
    id = "#" + id;
    var tables = document.getEle mentsByTagName( "table");
    var i;
    for (i=0; i < tables.length; i++) {
    var table = tables[i];
    if (table.getAttri bute("datasrc") == id)
    return table;
    }
    return null;
    }


    function addRowDOM (tableID) {
    // pass every cell content as a futher arg
    alert("here");
    var table =
    document.all ? document.all[tableID] :
    document.getEle mentById(tableI D);
    alert("here 2");
    if (arguments.leng th > 1) {
    alert(table.row s.length);
    var row = table.insertRow (table.rows.len gth-1);
    alert("after var");
    if (document.all) {
    alert("second if");
    for (var i = 1; i < arguments.lengt h; i++) {
    alert("in the for loop");
    var cell = row.insertCell( i - 1);
    cell.innerHTML = arguments[i];
    }
    }
    else if (document.getEl ementById) {
    alert("in else");
    for (var i = arguments.lengt h - 1; i >= 1; i--) {
    var cell = row.insertCell( arguments.lengt h - 1 - i);
    cell.appendChil d(document.crea teTextNode(argu ments[i]));
    }
    }
    }
    }


    function getDataFieldNam es(table)
    {
    var array = new Array();
    var divs = table.getElemen tsByTagName("di v");
    var i;
    for (i=0; i < divs.length; i++) {
    var div = divs[i];
    var datafld = div.getAttribut e("datafld");
    if (datafld != null && datafld != "") {
    array[i] = datafld;
    }
    }

    if (array.length < 1) {
    return null;
    }

    return array;
    }


    function getSrcFieldValu es(xml, fieldNames)
    {
    var srcs = new Array();
    var i;
    for (i=0; i < fieldNames.leng th; i++) {
    var field = fieldNames[i];
    srcs[field] = xml.getElements ByTagName(field );
    }
    return srcs;
    }


    function fillTable(table , srcs)
    {
    var trtemplate =
    table.firstChil d.removeChild(t able.firstChild .firstChild);
    var tdtemplate = trtemplate.remo veChild(trtempl ate.firstChild) ;

    while (trtemplate.fir stChild) {
    trtemplate.remo veChild(trtempl ate.firstChild) ;
    }

    table = table.firstChil d;

    var cols = 0;
    for (field in srcs) {
    srcs[cols++] = field;
    }

    var row;
    for (row = 0; row < srcs[srcs[0]].length; row++) {
    var tr = trtemplate.clon eNode(true);
    var col;
    for (col=0; col < cols; col++ ) {
    var td = tdtemplate.clon eNode(true);
    var div = td.firstChild;
    div.setAttribut e("datafld", srcs[col]);
    var textNode =
    document.create TextNode(srcs[srcs[col]][row].firstChild.nod eValue);
    div.appendChild (textNode);
    tr.appendChild( td);
    }
    table.appendChi ld(tr);
    }
    }


    function loadTableData()
    {
    if (!document.all) {
    var i;
    var xmlNodes = document.getEle mentsByTagName( "xml");
    for (i=0; i< xmlNodes.length ; i++) {
    var xml = xmlNodes[i];
    var id = xml.getAttribut e("id");
    var table = findTable(id);
    var fields = getDataFieldNam es(table);
    var srcs = getSrcFieldValu es(xml, fields);
    fillTable(table , srcs);
    }
    }
    }


    function highlightRow (element, color1, color2) {
    var chk = element.checked ;
    while (element.tagNam e.toUpperCase() != 'TR' && element != null)
    element = document.all ? element.parentE lement :
    element.parentN ode;

    if (element && chk) {
    element.bgColor = color1;
    } else {
    element.bgColor = color2;
    }
    }


    </script>

    </head>

    <body onload="loadTab leData();">


    <xml id="xmldso_list ">
    <CATALOG>
    <CD>
    <TITLE>Empire Burlesque</TITLE>
    <ARTIST>Bob Dylan</ARTIST>
    <COUNTRY>USA</COUNTRY>
    <COMPANY>Columb ia</COMPANY>
    <PRICE>10.90</PRICE>
    <YEAR>1985</YEAR>
    </CD>
    <CD>
    <TITLE>Hide your heart</TITLE>
    <ARTIST>Bonni e Tyler</ARTIST>
    <COUNTRY>UK</COUNTRY>
    <COMPANY>CBS Records</COMPANY>
    <PRICE>9.90</PRICE>
    <YEAR>1988</YEAR>
    </CD>
    <CD>
    <TITLE>Greate st Hits</TITLE>
    <ARTIST>Dolly Parton</ARTIST>
    <COUNTRY>USA</COUNTRY>
    <COMPANY>RCA</COMPANY>
    <PRICE>9.90</PRICE>
    <YEAR>1982</YEAR>
    </CD>
    <CD>
    <TITLE>Still got the blues</TITLE>
    <ARTIST>Gary Moore</ARTIST>
    <COUNTRY>UK</COUNTRY>
    <COMPANY>Virg in records</COMPANY>
    <PRICE>10.20</PRICE>
    <YEAR>1990</YEAR>
    </CD>
    </CATALOG>
    </xml>





    <h1>Table Samples</h1>

    <p>This sample shows how to mimic XML Data Islands in Mozilla with
    tables. This sample
    code is not meant for production use as it is very fragile as it stands
    here (very
    primitive browser capability test, requires certain template table
    layout and so on).</p>

    <h2>Names</h2>


    <table border=1 bgcolor=yellow>
    <tr align=left><th> Title: </th>
    <td><input type=text></td></tr>

    <tr align=left><th> Artist: </th>
    <td><input type=text></td></tr>

    <tr align=left><th> Year: </th>
    <td><input type=text></td></tr>

    <tr align=left><th> Country:</th>
    <td><input type=text></td></tr>

    <tr align=left><th> Company:</th>
    <td><input type=text></td></tr>

    <tr align=left><th> Price: </th>
    <td><input type=text></td></tr>
    </table>

    <br>

    <input type=BUTTON
    ONCLICK="addRow DOM('table1','C heck','MyTitle' ,'MyArtist','My Year',
    'MyCountry','My Company','MyPri ce')" value="Add" >

    <br>
    <br>


    <table id="table1" datasrc="#xmlds o_list" border=1>
    <thead>
    <tr align=left>
    <th>Choice</th>
    <th>Title</th>
    <th>Artist</th>
    <th>Country</th>
    <th>Company</th>
    <th>Price</th>
    <th>Year</th>
    </tr>
    </thead>
    <tr align=left><td> <input type="checkbox" name="check" onclick
    ="highlightRow( this, 'yellow', 'transparent'); " <div></td>
    <td><div datafld="TITLE" /td>
    <td><div datafld="ARTIST " /td>
    <td><div datafld="COUNTR Y" /td>
    <td><div datafld="COMPAN Y" /td>
    <td align=right><di v datafld="PRICE" /td>
    <td><div datafld="YEAR" /td>
    </tr>
    </table>



    </body>
    </html>

  • sharon

    #2
    Re: IE + XML + javascript = 0

    It might be because your table (table1) is automatically built using
    the datasrc attribute.

    Comment

    • sharon

      #3
      Re: IE + XML + javascript = 0

      It might be because your table (table1) is automatically built using
      the datasrc attribute.

      Comment

      • sharon

        #4
        Re: IE + XML + javascript = 0

        Two ways to solve your problem:
        1. Add the new row to the xml itself (xmldso_list).
        2. In addRowDOM you can set dataSrc to be empty and at the end rebound
        it. add another section to the table (tfoot) and use the insertRow to
        add new rows into the tfoot section.

        Comment

        • milkyway

          #5
          Re: IE + XML + javascript = 0


          sharon wrote:[color=blue]
          > Two ways to solve your problem:
          > 1. Add the new row to the xml itself (xmldso_list).
          > 2. In addRowDOM you can set dataSrc to be empty and at the end[/color]
          rebound[color=blue]
          > it. add another section to the table (tfoot) and use the insertRow to
          > add new rows into the tfoot section.[/color]


          Thank you for responding ;-)

          I have just seen that the structures are populated differently. I guess
          this is what happens when you have 'Frankenstein' like code. Am working
          it now though...

          Wish me luck ;-P

          Comment

          • milkyway

            #6
            Re: IE + XML + javascript = 0

            sharon wrote:[color=blue]
            > Two ways to solve your problem:
            > 1. Add the new row to the xml itself (xmldso_list).
            > 2. In addRowDOM you can set dataSrc to be empty and at the end[/color]
            rebound[color=blue]
            > it. add another section to the table (tfoot) and use the insertRow to
            > add new rows into the tfoot section.[/color]


            Thank you for responding ;-)

            I have just seen that the structures are populated differently. I guess
            this is what happens when you have 'Frankenstein' like code. Am working
            it now though...

            Wish me luck ;-P

            Comment

            Working...