skip to Main Content

I’m trying to convert JSON to a table, but it doesn’t. Everything seems fine, but I cannot see the values ​​in my table.

My code that converts JSON to table:

    $(function() {
          var my_data = '';
          $.each(JSON.parse(sonuc.response) , function(key, item){
          my_data += '<tr>';
          my_data += '<td>'+item.kod+'</td>';
          my_data += '<td>'+item.ev + " - " + item.deplasman+'</td>';
          my_data += '<td>'+item.oran+'</td>';
          my_data += '<td>'+item.tahmin+'</td>';
          my_data += '<td>'+item.tur+'</td>';
          my_data += '<td>'+item.tarih+'</td>';
          my_data += '<td>'+item.tarih+'</td>';
          my_data += '</tr>';
          });
         console.log(my_data)
         $('#maclar_table').append(my_data);    
  });

my_data output:

<tr><td>213</td><td>Galatasaray - Fenerbahçe</td><td>3.2</td><td>MS2</td><td>free</td><td>22.03.2019 / 18:00</td><td>22.03.2019 / 18:00</td></tr><tr><td>213</td><td>Galatasaray - Fenerbahçe</td><td>3.2</td><td>MS2</td><td>free</td><td>22.03.2019 / 18:00</td><td>22.03.2019 / 18:00</td></tr><tr><td>213</td><td>Galatasaray - Fenerbahçe</td><td>3.2</td><td>MS2</td><td>free</td><td>22.03.2019 / 18:00</td><td>22.03.2019 / 18:00</td></tr><tr><td>213</td><td>Galatasaray - Fenerbahçe</td><td>3.2</td><td>MS2</td><td>free</td><td>22.03.2019 / 18:00</td><td>22.03.2019 / 18:00</td></tr><tr><td>213</td><td>Galatasaray - Fenerbahçe</td><td>3.2</td><td>MS2</td><td>free</td><td>22.03.2019 / 18:00</td><td>22.03.2019 / 18:00</td></tr>

my JSON Output:

my JSON OUTPUT

My Table HTML Code:

         <table class="table" name="maclar_table">
                <thead class=" text-primary">
                  <th>
                    Kod
                  </th>
                  <th>
                    Takımlar
                  </th>
                  <th>
                    Oran
                  </th>
                  <th>
                    Tahmin              
                  </th>
                  <th>
                    Tür
                  </th>
                   <th>
                    Tarih
                  </th>

                  <th>
                    Yetkiler
                  </th>
                </thead>
              </table>

2

Answers


  1. You are setting the name to maclar_table, not the id. $(“#maclar_table”) in jQuery gets elements with the ID of maclar_table, not the name. My suggestion for the code on line 1 of your table:

    <table class="table" name="maclar_table" id="maclar_table">
    

    That should do the job.

    Login or Signup to reply.
  2. You can use – https://www.npmjs.com/package/html-tablify

    It is that simple –

    var html_tablify = require('html-tablify');
    var options = {
        data: [{a: 1, b: 2}, {b: 1, a: 2}, {a: 4}]
    };
    var html_data = html_tablify.tablify(options);
    console.log(html_data);
    

    Output (pretty) –

    <table border=1 cellspacing=0 cellpadding=0>
        <thead>
            <tr> <th>a</th> <th>b</th> </tr>
        </thead>
        </tbody>
            <tr> <td>1</td> <td>2</td> </tr>
            <tr> <td>2</td> <td>1</td> </tr>
            <tr> <td>4</td> <td> </td> </tr>
        </tbody>
    </table>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search