Algorithmically Generated Data
Algorithmically Generated Data
ferisj
Posts: 2Questions: 1Answers: 0
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?
This discussion has been closed.
Answers
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?
I'll address your three concerns.
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.
Hi,
The
data()
method can't be used as a setter I'm afraid - just a getter. Userows.add()
to add rows:table.rows.add( generated ).draw();
would do it in this case.Allan