Change the result of a TD value for a dynamic table image

Asked

Viewed 81 times

1

I’m developing a visual availability map, and I created a dynamic table, so far everything right. I need to get the result of <td> and transform into images.

Example: my <td> returns 900, I need to change this value to an X image; mine <td> returns 100, change this value to image Y. understood?

Follow below my Javascript.

var div = document.getElementById("situacao1");

            //var dataset = DatasetFactory.getDataset("colleague", null, null, null);
            //var dataset = DatasetFactory.getDataset("dsEmpreendimentos", null, constraints, null);
            try {
            var dataset = DatasetFactory.getDataset("dsUnidadeVenda", null, constraints, null);
            div.innerHTML = showDataset(dataset);
            } catch(erro) {
                div.innerHTML = erro;
            }
        }

function showDataset(dataset) {
    var tabela = "<table id=minhaTabela>";

    //Monta o cabeçalho
    tabela += "<tr>";
    for (var i = 0; i < dataset.columns.length; i++) {
        tabela += "<th>" + dataset.columns[i] + "</th>";
    }
    tabela += "</tr>";

    //Monta os registros   
    for (var x = 0; x < dataset.values.length; x++) {
        tabela += "<tr>";
        var row = dataset.values[x];
        for (var y = 0; y < dataset.columns.length; y++) {
            tabela += "<td id=td_sit>" + row[dataset.columns[y]] + "</td>";
        }
        tabela += "</tr>";
    }

    tabela += "</table>";
    return tabela;
}

The return of Javascript in HTML is this.

inserir a descrição da imagem aqui

There’s only one result column.

Below is the return of the Result, of the question. inserir a descrição da imagem aqui

  • Bruno, you want to change the content of <td> of character 900, for a image?

  • Without knowing the structure of the table becomes difficult, because it is not known which column you want to take the values... or would be all columns?

  • only has a column that returns the code.

  • But what is the column? the first, the second, the third....

  • I edited my question and put an image of the JS return in HTML.

2 answers

1


You can do it this way (see comments in the code):

// seleciona as TDs
var cols = document.querySelectorAll("#minhaTabela td");

// percorre as TDs
for(var x = 0; x < cols.length; x++){

   // pega o número da TD
   switch(Number(cols[x].textContent)){
      
      // insere a imagem de acordo com o número dentro da TD
      
      case 100:
      cols[x].innerHTML = '<img src="https://www.cleverfiles.com/howto/wp-content/uploads/2016/08/mini.jpg">';
      break;
      
      case 900:
      cols[x].innerHTML = '<img src="https://tinyjpg.com/images/social/website.jpg">';
      break;

   }
   
}
img{
   width: 100px;
}
<table id=minhaTabela border="1">
   <tr>
      <th>
         col
      </th>
   </tr>
   <tr>
      <td>
         100
      </td>
   </tr>
   <tr>
      <td>
         900
      </td>
   </tr>
   <tr>
      <td>
         100
      </td>
   </tr>
</table>

In your case, put the code inside the try after the div.innerHTML:

try {

   var dataset = DatasetFactory.getDataset("dsUnidadeVenda", null, constraints, null);
   div.innerHTML = showDataset(dataset);


   // seleciona as TDs
   var cols = document.querySelectorAll("#minhaTabela td");

   // percorre as TDs
   for(var x = 0; x < cols.length; x++){

      // pega o número da TD
      switch(Number(cols[x].textContent)){

         // insere a imagem de acordo com o número dentro da TD

         case 100:
         cols[x].innerHTML = '<img src="https://www.cleverfiles.com/howto/wp-content/uploads/2016/08/mini.jpg">';
         break;

         case 900:
         cols[x].innerHTML = '<img src="https://tinyjpg.com/images/social/website.jpg">';
         break;

      }

   }

} catch(erro) {
   div.innerHTML = erro;
}

Using an object

You can also create an object and keep the path of the images according to the number. I think it makes it easier to organize and dry the code more:

var imgs_ = {
   "100": "https://www.cleverfiles.com/howto/wp-content/uploads/2016/08/mini.jpg",
   "900": "https://tinyjpg.com/images/social/website.jpg"
}

// seleciona as TDs
var cols = document.querySelectorAll("#minhaTabela td");

// percorre as TDs
for(var x = 0; x < cols.length; x++){

   // insere a imagem de acordo com o número
   cols[x].innerHTML = '<img src="'+imgs_[Number(cols[x].textContent)]+'">';

}
img{
   width: 100px;
}
<table id=minhaTabela border="1">
   <tr>
      <th>
         col
      </th>
   </tr>
   <tr>
      <td>
         100
      </td>
   </tr>
   <tr>
      <td>
         900
      </td>
   </tr>
   <tr>
      <td>
         100
      </td>
   </tr>
</table>

  • Sam, thanks for the answers, but I didn’t understand one thing, should I mount the tebela in HTML? Because I assemble my java script like this. <div name="situacao1" id="situacao1">

  • No. The table I put above was just an example. I hope I will update the answer to get better

  • I updated the answer. It should work, then you just change the src of the images you want to put

  • Guy was right, thank you very much, that’s what I needed.

  • I will add an even simpler form to the answer.. Just a moment

  • If the answer solved, be sure to mark ... Té more!

  • this other leaner I replaced in place of the previous one and it did not work

  • I put the code to work in the answer. If you run see that works too :)

  • found error had left catch inside var img=, now I sure thank you....

  • Sam, sorry to bother you again, but Voce was right I need to add two more columns, as I do with this code you gave me, as I do to select the third column, where the code will be.

  • Just change it up: var cols = document.querySelectorAll("#minhaTabela td:nth-child(3)");

  • Just takes away a doubt this is jquery? this function td:Nth-Child(3)?

  • There is no jQuery there. The :Nth-Child(3) is a CSS selector, which selects the specified 3rd element.

  • guy got top I’ll post, the print. Really worth....

Show 9 more comments

0

Good morning, my friend. I’m not sure I understand your question. Can you give me more details? If your question is about how to change the tag "src" attribute <img> you can do this using jquery yourself. Assign an id to the tag <img> and then do the following:

$("#id_tag_imagem").attr("src", "caminho_arquivo_imagem.jpg");

If this does not help you, comment here giving us more details about your question so that it is easier to help you.

Hugs.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.