Algorithmically Generated Data

Algorithmically Generated Data

ferisjferisj Posts: 2Questions: 1Answers: 0
edited April 2017 in Free community support

The data I want to show in a Datatable does not come from a server. It comes from JavaScript algorithms I have written. I'm generating JavaScript objects that contain the data I want to display. For example:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<title>Algorithmically Generated Data for DataTables</title>
</head>
<body>
<table id="table">
<thead>
<tr>
<th>First Name</th>
<th>LastName</th>
</tr>
</thead>
</table>
<script src="jquery-1.12.0.min.js" type="text/javascript"></script>
<script src="datatables.min.js" type="text/javascript"></script>
<script type="text/javascript">
var generated = [];
var row0 = {};
row0.firstName = "John";
row0.lastName = "Smith";
generated[0] = row0;
var row1 = {};
row1.firstName = "Geoffery";
row1.lastName = "Corbit";
generated[1] = row1;
var table = $('#table').DataTable( {
columns : [
{ "data" : "firstName" },
{ "data" : "lastName" }
]
} );
table.data(generated);
table.invalidate().draw();
</script>
</body>
</html>

Please notice lines 35 and 36. They indicate what I wished would work, but I have missed such documentation. How do I correctly implement this?

Answers

  • tangerinetangerine Posts: 3,365Questions: 39Answers: 395

    Firstly, DataTables requires jQuery to be referenced.

    Secondly, are you proposing to write your data to a file before handing it to DataTables? If so, will it be written out as JSON, or CSV, or what? If not written to file, then what will be your data source for DataTables' purposes?

    Thirdly, what volumes of data are you envisaging?

  • ferisjferisj Posts: 2Questions: 1Answers: 0

    I'll address your three concerns.

    1. DataTables requires jQuery to be referenced - In my real code I have jQuery properly referenced and I forgot to include it in my contrived example for discussion. I edited my original question to add the missing jQuery reference.
    2. Proposition to write data to a file - I'm not proposing to write the data anywhere. The example is complete in this respect.
    3. Envisioned volumes of data - I have envisioned that there would be no volume of data other than the array of JavaScript objects produced by the algorithm. The example is complete in this respect.

    What I'm trying to do is well represented in my question. It would take more CPU cycles but I could convert my array of objects into a JSON string, if that would help. I would rather not since the DataTable is just going to parse it into an array of objects likely similar to what I started with.

    Take my sample code literally, with no further implied data manipulation or storage required.

  • allanallan Posts: 63,552Questions: 1Answers: 10,477 Site admin

    Hi,

    The data() method can't be used as a setter I'm afraid - just a getter. Use rows.add() to add rows: table.rows.add( generated ).draw(); would do it in this case.

    Allan

This discussion has been closed.