vo1d
vo1d

Reputation: 3213

Table from JSON

I've a php script which loads basically just one csv via:

 echo json_encode(file(csvfile.csv, FILE_IGNORE_NEW_LINES));

The output is something like this:

["foo,bar,baz","foo, foo,bar","bla,bla,blubb"] 

In my basic html file I load it in this way:

  <script>
  $(document).ready(function(){ 
    $.getJSON("myphpscript.php", function(data) {
      filltable(data);
    })
});

function filltable(jqxhr){
    var table_obj = $('table');
    $.each(jqxhr, function(index, item){
         table_obj.append($('<tr id="'+item.id+'"><td>'+item.data+'</td></tr>'));
    }).insertAfter('.table');
}    
  </script>

which fills my <table id="table"></table> just with undefined.

My goal is it to get a table with 1 column containing:

foo,bar,baz
foo, foo,bar
bla,bla,blubb

After specify a delimiter my js-function should explode each line and more columns. This is my first attempt with javascript and json. Thank you very much

Edit: Fixed function call (Thanks VisioN) . object.length is now ok, but the table is still undefinied

Upvotes: 1

Views: 161

Answers (2)

charlietfl
charlietfl

Reputation: 171679

Your data structure doesn't match the jQuery object parsing schema

You are trying to parse an object that would look like:

 [{"id":"foo","data":"foo,bar,baz"}]

For one column You just want

 table_obj.append($('<tr id="'+item+'</td></tr>'));

Upvotes: 1

xception
xception

Reputation: 4287

I think you might want to use

table_obj.append($('<tr id="'+index+'"><td>'+item+'</td></tr>'));

instead of

table_obj.append($('<tr id="'+item.id+'"><td>'+item.data+'</td></tr>'));

Upvotes: 1

Related Questions