0

I am using dataTables to populate my tables and I need to insert some static data into the table.

So I now have this:

jQuery('#mytable').DataTable( {
    data: dataSet,
    columns: [
        { "users": "id" },
        { "somethingelse": "valuehere" }
    ]
});

Is it possible to add a variable to valuehere? for example:

var dataSet = [['userid', 'something else'],['userid', 'something else']];
var myVariableHere = "some variable";

jQuery('#mytable').DataTable( {
  data: dataSet,
  columns: [
    { "users": "id" },
    { "somethingelse": myVariableHere }
  ]
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<script src="https://cdn.datatables.net/1.10.12/js/jquery.dataTables.min.js"></script>
<link href="https://cdn.datatables.net/1.10.12/css/jquery.dataTables.min.css" rel="stylesheet"/>
<table id="mytable" class="display" width="100%"></table>

3
  • This should be possible if the myVariableHere has been formatted properly. Commented Nov 15, 2016 at 13:17
  • 3
    did you tried it ? as first look , it seems to work. And if the question is simply "Is it possible to add a variable to valuehere? ", then i think you are the best placed person to tell this Commented Nov 15, 2016 at 13:17
  • var myVariableHere = 'whatever' jQuery('#mytable').DataTable(function (myVariableHere) { ... } should be enough. Commented Nov 15, 2016 at 13:24

1 Answer 1

1

Yes the variable isn't a problem, but your columns need to each have a "title" property.

var dataSet = [['userid', 'something else'],['userid', 'something else']];
var myVariableHere = "some variable";

jQuery('#mytable').DataTable( {
  data: dataSet,
  columns: [
    { title: "id" },
    { title: myVariableHere }
  ]
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<script src="https://cdn.datatables.net/1.10.12/js/jquery.dataTables.min.js"></script>
<link href="https://cdn.datatables.net/1.10.12/css/jquery.dataTables.min.css" rel="stylesheet"/>
<table id="mytable" class="display" width="100%"></table>

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

1 Comment

How do I do it if the data looks like this: { "data": "data.name", "title": "Names" } ?

Your Answer

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