<!DOCTYPE html>
<html>
    <head>
        <script src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
        <link href="https://nightly.datatables.net/css/jquery.dataTables.css" rel="stylesheet" type="text/css" />
        <script src="https://nightly.datatables.net/js/jquery.dataTables.js"></script>
        <meta charset=utf-8 />
        <title>DataTables - JS Bin</title>
    </head>
    <body>
        <div class="container">
            <table id="example" class="display" width="100%">
            </table>
        </div>
    </body>
</html>
 
body {
    font: 90%/1.45em "Helvetica Neue", HelveticaNeue, Verdana, Arial, Helvetica, sans-serif;
    margin: 0;
    padding: 0;
    color: #333;
    background-color: #fff;
}
div.container {
    min-width: 980px;
    margin: 0 auto;
}
 
var columns = [];  // Create an empty array for the columns option
function getDT() {
    $.ajax({
      url: "/ssp/objects.php",  // All of the data from the server is return, used on;y for this example
      success: function (data) {
        
        // Parse the JSON response to a JS object
        data = JSON.parse(data);
        
        console.log(data.data[0]);  
        // Get the object keys from the first row of data
        // This might be different in a real world example
        // where specific column config data is returned
        columnNames = Object.keys(data.data[0]);
        
        // Iterate each of the columnNames to build the columns.data and columns.title optins
        for (var i in columnNames) {
          
          // Push the {data: ..., title: ...} onto array
          columns.push({data: columnNames[i], 
                    title: capitalizeFirstLetter(columnNames[i])});
        }
        
        // Once columns array is built init server side Datatables
        $('#example').DataTable( {
            processing: true,
            serverSide: true,
            ajax: "/ssp/objects.php",
            columns: columns
        } );
      }
    });
}
function capitalizeFirstLetter(string) {
    return string.charAt(0).toUpperCase() + string.slice(1);
}
$(document).ready(function() {
  
  
  getDT();
  
} );
Output

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
anonymouspro
0viewers