1

From what I gather normally with Jsons and Datatables, datatables will load each record as a row of data for your table.

I am trying to find a good way to load json data as columns instead of rows using DataTables. But I haven't been seen a good method to accomplish this. Here is the code I have currently which loads rows:

$(document).ready(function() {

var table = $('#jsontable').dataTable();  //Initialize the datatable

    $('#load').on('click',function(){
        var user = $(this).attr('id');
        if(user != '') 
        { 
        $.ajax({
            url: 'process.php?method=fetchdata',
            dataType: 'json',
            success: function(s){
            console.log(s);
                    oTable.fnClearTable();
                        for(var i = 0; i < s.length; i++) {
                         table.fnAddData([
                                    s[i][0],
                                    s[i][1],
                                    s[i][2],
                                    s[i][3],
                                    s[i][4]
                                           ]);                                      
                                        } // End For

            },
            error: function(e){
               console.log(e.responseText); 
            }
            });
        }
    });
});
Gyrocode.com
  • 51,125
  • 13
  • 124
  • 164
user2725919
  • 179
  • 1
  • 12

1 Answers1

0

Probably you can try to think how to convert columns to rows and in which point of App you will do it.

I recommend to do it at the SQL's level.

Check this SQL Server : Columns to Rows or this one SQL query to convert columns into rows

If you want to do it in JavaScript take a look at the Swap rows with columns (transposition) of a matrix in javascript

Community
  • 1
  • 1
DmitryBoyko
  • 32,983
  • 69
  • 281
  • 458