0

I have the following script which takes the first row (TH) and adds it to a table dynamically which will then be exported to an Excel sheet. For some reason, it keeps adding and stacking on top of the next table row.

Here is the script:

var decode_entities = (function () {
    // Remove HTML Entities
    var element = document.createElement('div');

    function decode_HTML_entities(str) {

        if (str && typeof str === 'string') {

            // Escape HTML before decoding for HTML Entities
            str = escape(str).replace(/%26/g, '&').replace(/%23/g, '#').replace(/%3B/g, ';');

            element.innerHTML = str;
            if (element.innerText) {
                str = element.innerText;
                element.innerText = '';
            } else {
                // Firefox support
                str = element.textContent;
                element.textContent = '';
            }
        }
        return unescape(str);
    }
    return decode_HTML_entities;
})();

function fnExcelReport() {
    var tab_text = "<table border='0'><tr bgcolor='#FFFFFF' class='mainTR'>";
    var textRange;
    var j, p = "", q = "";
    var tab = document.getElementById('BookingResults'); // id of table


    for (j = 0 ; j < tab.rows.length ; j++) {
        for (var y = 0; y < tab.rows[j].cells.length; y++) {
            if (y == tab.rows[j].cells.length - 1) {
                if (j == 0) {
                    q = q + "<TH scope=col class='headerLast vTop'>";
                }
                else {
                    q = q + "<TD class='bodyLast vTop'>";
                }
                q = q + decode_entities(tab.rows[j].cells[y].innerHTML);
                //alert("LAST COL INNER HTML: " + decode_entities(tab.rows[j].cells[y].innerHTML));
                if (j == 0) {
                    q = q + "</TH>";
                }
                else {
                    q = q + "</TD>";
                }
            }
            else {
                if (j == 0) {
                    q = q + "<TH scope=col class='headerNotLast vTop'>";
                }
                else {
                    q = q + "<TD class='bodyNotLast vTop'>";
                }
                q = q + tab.rows[j].cells[y].innerHTML;
                if (j == 0) {
                    q = q + "</TH>";
                }
                else {
                    q = q + "</TD>";
                }
                p = p + tab.rows[j].cells[y].innerHTML;
                //alert("NOT LAST COL INNERHTML: " + p);
            }
        }
        //alert(q);
        //q = "";
        //alert(tab.rows[j].innerHTML);
        //tab_text = tab_text + tab.rows[j].innerHTML + "</tr><tr bgcolor='#FFFFFF' class='mainTR'>";
        tab_text = tab_text + q + "</tr><tr bgcolor='#FFFFFF' class='mainTR'>";
        alert(tab_text);
    }

    tab_text = tab_text + "</table>";
    tab_text = tab_text.replace(/<A[^>]*>|<\/A>/g, "");//remove if u want links in your table
    tab_text = tab_text.replace(/<img[^>]*>/gi, ""); // remove if u want images in your table
    tab_text = tab_text.replace(/<input[^>]*>|<\/input>/gi, ""); // reomves input params

    var ua = window.navigator.userAgent;
    var msie = ua.indexOf("MSIE ");

    if (msie > 0 || !!navigator.userAgent.match(/Trident.*rv\:11\./))      // If Internet Explorer
    {
        txtArea1.document.open("txt/html", "replace");
        txtArea1.document.write(tab_text);
        txtArea1.document.close();
        txtArea1.focus();
        sa = txtArea1.document.execCommand("SaveAs", true, "SpecificGuidelines.xls");
    }
    else {                //other browser not tested on IE 11
        sa = window.open('data:application/vnd.ms-excel,' + encodeURIComponent(tab_text));
    }

    return (sa);
}

Here is the first alert (shows the first row (TH)):

enter image description here

Here is the second alert (The red highlight is repeating, and I am sure on the third run, the first two will repeat and so forth...):

enter image description here

How do I make sure the TH/TD doesn't keep adding up and instead it goes to the next row and append.

So I am looking to modify the script so it is:

<table>
    <tr>
        <th></th> //TH is the header so it appears only once in the table.
    </tr>
    <tr>
        <td></td>
    </tr>
    <tr>
        <td></td>
    </tr>
    ...
</table>

What is happening right now:

<table>
    <tr>
        <th></th>
    </tr>
    <tr>
        <th></th> //brought from the first entry
        <td></td>
    </tr>
    <tr>
        <th></th> //brought from the first entry
        <td></td> //brought from the second entry
        <td></td>
    </tr>
    ...
</table>
SearchForKnowledge
  • 3,663
  • 9
  • 49
  • 122

1 Answers1

0

You need to move <tr bgcolor='#FFFFFF' class='mainTR'> into your loop to open a row for each iteration of the loop and then at end of loop (but inside it) close the <tr>.

Your open tag is outside of the rows loop and the close is after the loop, thus you only create one row.

var tab_text = "<table border='0'>";
for (j = 0 ; j < tab.rows.length ; j++) {
    tab_text+='<tr>'
        /* loop over cells here */

    tab_text+='</tr>';

}
charlietfl
  • 170,828
  • 13
  • 121
  • 150