2

I'm trying to build a dynamic table in javascript with innerHTML.

When the web app runs, it only prints the console.log, but doesn't build a table.

I tried two ways:

First:

    success: function (data, status, jqXHR) {


        $.each(data, function (index, dati) {

            console.log(dati)


            var html = "<table width=\'450\' bgcolor=\'white\' border=\'2\' bordercolor=\'black\' cellpadding=\'10\' cellspacing=\'1\'>\n" +
                "   <tr style=min-width:850px>\n" +
                "        <td>dati.codiceCOmmessa </td>\n" +
                "        <td>dati.commessaMainSub</td>\n" +
                "        <td>dati.settoreCliente</td>\n" +
                "        <td>dati.nomeCliente</td>\n" +
                "        <td>dati.titoloQuals</td>\n" +
                "        <td>dati.keyWordsTopic</td>\n" +
                "        <td>dati.keyWordsActivities</td>\n" +
                "        <td>dati.anno</td>\n" +
                "        <td>dati.dataInizio</td>\n" +
                "        <td>dati.dataFine</td>\n" +
                "        <td>dati.referente</td>\n" +
                "        <td>dati.referenteDoc</td>\n" +
                "        <td>dati.sviluppatore</td>\n" +
                "        <td>dati.path</td>\n" +
                "    </tr>\n" +
                "</table>"

            html.innerHTML;
        })
    },

Second:

(with body.innerHTML or node.innerHTML, the app makes a mistake) :

    success: function (data, status, jqXHR) {


        $.each(data, function (index, dati) {

            console.log(dati)

            innerHTML = "<table width=\'450\' bgcolor=\'white\' border=\'2\' bordercolor=\'black\' cellpadding=\'10\' cellspacing=\'1\'>\n" +
                "   <tr style=min-width:850px>\n" +
                "        <td>dati.codiceCOmmessa </td>\n" +
                "        <td>dati.commessaMainSub</td>\n" +
                "        <td>dati.settoreCliente</td>\n" +
                "        <td>dati.nomeCliente</td>\n" +
                "        <td>dati.titoloQuals</td>\n" +
                "        <td>dati.keyWordsTopic</td>\n" +
                "        <td>dati.keyWordsActivities</td>\n" +
                "        <td>dati.anno</td>\n" +
                "        <td>dati.dataInizio</td>\n" +
                "        <td>dati.dataFine</td>\n" +
                "        <td>dati.referente</td>\n" +
                "        <td>dati.referenteDoc</td>\n" +
                "        <td>dati.sviluppatore</td>\n" +
                "        <td>dati.path</td>\n" +
                "    </tr>\n" +
                "</table>"


        })
    }

Can somebody help tell me what I'm doing wrong?

5
  • dati.keyWordsTopic are variables, try it like " <td>"+ dati.keyWordsTopic+"</td>\n" + Commented Aug 1, 2017 at 13:36
  • You need to write the string you have created somewhere, meaning that you need to call the .innerHTML of an element to actually write this on the page. Commented Aug 1, 2017 at 13:37
  • all dati are variables Commented Aug 1, 2017 at 13:38
  • You've added "<td>dati.titoloQuals</td>" the element values are variables... You need: "<td>" + dati.titoloQuals + "</td>". Commented Aug 1, 2017 at 13:39
  • html is a string so what do you expect to happen in the line html.innerHTML; ? Commented Aug 1, 2017 at 13:39

3 Answers 3

2

At first, loops can make things easier (and string literals too), so may simply display all object values:

var html = 
`<table width='450' bgcolor='white' border='2'bordercolor='black' cellpadding='10' cellspacing='1'>
     <tr style='min-width:850px'>
         ${
            Object.values(dati)
            .map(
               value => `<td>${value}</td>`
            ).join("\n")
          }
       </tr>
  </table>`;

Or if you dont like the literals, same works with concatenation too:

var html =     
  "<table width='450' bgcolor='white' border='2'bordercolor='black' cellpadding='10' cellspacing='1'><tr style='min-width:850px'>"
+ Object.values(dati)
    .map(
         value => "<td>"+value+"</td>"
     ).join("\n")
+ "</tr></table>";

And you.may want to do sth with html :

document.body.innerHTML += html;

A small demo

Sign up to request clarification or add additional context in comments.

4 Comments

Always writing beautiful code, can never get away from you haha :P
@tez is that sarcasm ? !/
Haha yes... A huge compliment. I just know when your around I wont post an answer. (+1)
@tez wingfield the same happens to me when t.j. crowder, bergi or pointy are around.... ;)
0

You need to use an existing element's .innerHTML method to write the html produced by your function to the document. Here's a very simple example with no real data.

var success = function(data, status, jqXHR) {


  $.each(data, function(index, dati) {

    console.log(dati)


    var html = "<table width=\'450\' bgcolor=\'white\' border=\'2\' bordercolor=\'black\' cellpadding=\'10\' cellspacing=\'1\'>\n" +
      "   <tr style=min-width:850px>\n" +
      "        <td>dati.codiceCOmmessa </td>\n" +
      "        <td>dati.commessaMainSub</td>\n" +
      "        <td>dati.settoreCliente</td>\n" +
      "        <td>dati.nomeCliente</td>\n" +
      "        <td>dati.titoloQuals</td>\n" +
      "        <td>dati.keyWordsTopic</td>\n" +
      "        <td>dati.keyWordsActivities</td>\n" +
      "        <td>dati.anno</td>\n" +
      "        <td>dati.dataInizio</td>\n" +
      "        <td>dati.dataFine</td>\n" +
      "        <td>dati.referente</td>\n" +
      "        <td>dati.referenteDoc</td>\n" +
      "        <td>dati.sviluppatore</td>\n" +
      "        <td>dati.path</td>\n" +
      "    </tr>\n" +
      "</table>"


    document.getElementById('wrapper').innerHTML = html;
  })
};

success([0, 1], null, null);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="wrapper"></div>

2 Comments

You may need to enlighten OP of the variables interpreted as strings?
@TezWingfield Maybe I do, but I think this is besides the scope of the question. Plus, there are plenty of other, far better answers elsewhere for that, so I would rather he did check them, instead.
0

First you need to append the dati.variables to the string and not have them as part of the string itself like:

innerHTML = "<table width=\'450\' bgcolor=\'white\' border=\'2\' bordercolor=\'black\' cellpadding=\'10\' cellspacing=\'1\'>\n" +
                "   <tr style=min-width:850px>\n" +
                "        <td>" + dati.codiceCOmmessa + "</td>\n" +
                "        <td>" + dati.commessaMainSub + "</td>\n"

Then you need to append the html you created to the page, depending on where you want to add the table. This will append it to the body:

$(innerHTML).appendTo($('body'));

or set the body to your html:

$('body').html(innerHTML);

2 Comments

@Jonasw duh! fixed it (i doubt he wants to add it to body anyways, just something to use for an example)
@snape no problem. I would still prefer $(document.body).append(innerHTML) anyways

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.