JSON Array without associative keys

JSON Array without associative keys

maks_fillmaks_fill Posts: 4Questions: 1Answers: 0
edited January 2018 in Free community support

My json array is a 2 dimensional array.
$array[row][column] where the first [row] is a table header row.

I can build a table using for loops, but that is very problematically to use the row-details function.
I pass the json data var dataSet = <?php print_r($text['post_content']);?>; and data: dataSet in the table definition.
But than I dont know how to use it in the function

function format ( d ) {
    // `d` is the original data object for the row
    return '<table cellpadding="5" cellspacing="0" border="0" style="padding-left:50px;">'+
        '<tr>'+
            '<td>Full name:</td>'+
            '<td>'+d.name+'</td>'+
        '</tr>'+
        '<tr>'+
            '<td>Extension number:</td>'+
            '<td>'+d.extn+'</td>'+
        '</tr>'+
        '<tr>'+
            '<td>Extra info:</td>'+
            '<td>And any further details here (images etc)...</td>'+
        '</tr>'+
    '</table>';
}

I cannot put +d.1+ for the first column, that is inconsistent

Could you help me?

Edited by Allan - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.

This question has accepted answers - jump to:

Answers

  • bindridbindrid Posts: 730Questions: 0Answers: 119
    Answer ✓

    I am not sure that I get what you are asking so I assumed that you are trying to create a table from a double dimension table. So here is my solution:

    You can see it work here: http://live.datatables.net/yuhutuni/1/edit

    $(document).ready( function () {
      // get the headers
      var headers = dataset[0];
      // now remove that row so it does not end up in the table
     var displaydata =  dataset.slice(1);
    
     // create column definition
      var cols = [];
      for(var i = 0; i < headers.length; ++i){
        cols.push({title:headers[i]});
      }
      
      var table = $('#example').DataTable({columns:cols, data:displaydata});
    } );
    
  • allanallan Posts: 63,455Questions: 1Answers: 10,465 Site admin
    Answer ✓

    Without the syntax highlighting (which I've now added) there was a lot of context missing in the example.

    In the row details function you would use array notation to access information from an array of data - e.g. d[0].

    Allan

  • maks_fillmaks_fill Posts: 4Questions: 1Answers: 0

    Thank you very much for your responce!
    I will try your sugestion now.

  • maks_fillmaks_fill Posts: 4Questions: 1Answers: 0

    I tried to combine your method and the https://datatables.net/examples/api/row_details.html

    where I've put d[0]

    Could you check my code and tell me what i am doing wrong?
    http://live.datatables.net/madumexe/1/edit

    Thank you very much

  • allanallan Posts: 63,455Questions: 1Answers: 10,465 Site admin

    http://live.datatables.net/madumexe/2/edit

    You didn't have anything enabled to show / hide the details row. I've put the event handler on the whole row, but you can modify that as you need.

    Allan

  • maks_fillmaks_fill Posts: 4Questions: 1Answers: 0

    Tank you vary much for your quick answer.
    I'm noob in JS thats I ask you some more questions:
    In the code
    http://live.datatables.net/roxopatu/1/edit
    I try to push headers and fileds in childer rows. Could you tell me what I am doing wrong?

    function format(cols) {
      return '<table cellpadding="5" cellspacing="0" border="0" style="padding-left:50px;">'
      var headers = dataset[0];
      var d = dataset.slice(1);
      var cols = [];
      for (var i = 5; i < headers.length; ++i) {
            cols.push({
            title: headers[i],
            field: d[i]
          });
      }
        "</table>";
    }
    

    And how can I define "className": 'details-control' in columns option of table definition, if there is already columns: cols ?

    var table = $("#example").DataTable({
        data: displaydata,
        columns: cols
         "columns": [                 // returns error: double definition of clumns arrtibut 
                {
                    "className":      'details-control',
                    "orderable":      false,
                    "data":           null,
                    "defaultContent": ''
                }
      ]
      
      });
    

    Thank you very much in advance.
    BR Maks

  • allanallan Posts: 63,455Questions: 1Answers: 10,465 Site admin

    cols.push({

    This is adding data to the row's data array! You want to output HTML from the format function - not modify the original array.

    The columns array needs to have exactly the same number as entries as the number of columns that will be shown. In the above you only define a single column, so that won't work I'm afraid. You need to define something for each column, even if it is null (i.e. no extra options).

    Allan

This discussion has been closed.