5

In jquery Datatables is it possible to define columns with a server-side script? I need something like this enter image description here

The columns with dates have to be loaded from server. Then number of columns can vary.

5
  • 1
    Why dont you get the complete data using JSON and then create a HTML table that you can show it to the user Commented Dec 29, 2011 at 8:07
  • I don't quite understand what you mean by "create HTML table". Create with Javascript ? Commented Dec 29, 2011 at 8:10
  • when you get the JSON then you can parse it using JSON.parse and then you can iterate the objects to create a HTML table using JQuery Commented Dec 29, 2011 at 8:19
  • see the link zachhunter.com/2010/04/json-objects-to-html-table Commented Dec 29, 2011 at 8:21
  • 1
    Will this work in pair with datatables? Right now I load rows with "sAjaxSource" property. Commented Dec 29, 2011 at 8:27

2 Answers 2

5

I think I have found what you were looking for

I will paste some code + post a link to a similar Q' in which you will get much more info...

$.ajax( {
    "url": 'whatever.php',
    "success": function ( json ) {
        json.bDestroy = true;
        $('#example').dataTable( json );
     },
    "dataType": "json"
} );

where json is something like this

{

"aaData": [
[ "2010-07-27 10:43:08", "..."], [ "2010-06-28 17:54:33", "..."],
[ "2010-06-28 16:09:06", "..."], [ "2010-06-09 19:15:00", "..."]
] ,

 "aaSorting": [
  [ 1, "desc" ]
 ],

 "aoColumns": [
   { "sTitle": "Title1" },
   { "sTitle": "Title2" }
 ]

}

here a link to the original thread

Column definition via JSON array (ajax)

Sign up to request clarification or add additional context in comments.

Comments

5

To expand on what Kamal Deep Singh was saying:

You could dynamically create the table on the fly, then apply datatables to it to get datatables' functionality.

// up in the html
<table id="myDatatable" class="... whatever you need..."></table>

and then:

// in the javascript, where you would ordinarily initialize the datatable
var newTable = '<thead><tr>'; // start building a new table contents

// then call the data using .ajax()
$.ajax( {
    url: "http://my.data.source.com",
    data: {}, // data, if any, to send to server
    success: function(data) {
        // below use the first row to grab all the column names and set them in <th>s
        $.each(data[0], function(key, value) {
            newTable += "<th>" + key + "</th>";
        });
        newTable += "</tr></thead><tbody>";                  

        // then load the data into the table
        $.each(data, function(key, row) {
             newTable += "<tr>";
              $.each(row, function(key, fieldValue) {
                   newTable += "<td>" + fieldValue + "</td>";
              });
             newTable += "</tr>";
        });
       newTable += '<tbody>';

       $('#myDatatable').html(newTable); // replace the guts of the datatable's table placeholder with the stuff we just created. 
    }
 });

 // Now that our table has been created, Datatables-ize it
 $('#myDatatable').dataTable(); 

Note you can put settings in that .dataTable() as normal, however, not 'sAjaxSource' or any of the associated data-getting functions -- this is applying datatables to an already existing table, one we created on the fly.

Ok, so it's kind of a hacky way of doing it, but it should work.

There isn't currently a built in method of doing this with datatables dynamically. See here: https://github.com/DataTables/DataTables/issues/273

1 Comment

Thanks, but I was looking for a more convenient way, similar to this "sAjaxSource": "scripts/server_processing.php"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.