I would like to use jquery in Angular to create a table based on my data input. I am new to jquery, so this might be some dumb thing I forgot, but I cannot say what it is.
Sadly when I execute ng serve what happens is nothing, just a blank page.
I got the following code:
HTML
<body onLoad="createHeaders('#dataTable')">
<table #dataTable >
</table>
</body>
TS
import { Component, OnInit } from '@angular/core';
import { MOCKUP } from '../Table';
import * as $ from "jquery";
@Component({
selector: 'app-tabular',
templateUrl: './tabular.component.html',
styleUrls: ['./tabular.component.css']
})
export class TabularComponent implements OnInit {
constructor() { }
ngOnInit() {
}
//this is where i want to work in the future, lets just forget about that for the moment
buildHTMLTableCodeFromTree(selector) {
//var header = this.createHeaders(selector, MOCKUP.Head);
$(selector).append("<td>test</td>");
}
createHeaders(selector) {
var headObject = MOCKUP.Head;
console.log("Er ist in der Methode!");
var value;
var colspan;
var entry = "";
for (var j = 0; j < headObject.length; j++) {
entry = entry + "<tr>";
for (var i = 0; i < headObject[j].length; i++) {
value = headObject[j][i].value;
colspan = headObject[j][i].colspan;
entry = entry + "<th colSpan='" + colspan + "'>" + value + "</th>";
}
entry = entry + "</tr>";
}
$("dataTable").append(entry);
}
}
The table Head Mockup:
export const MOCKUP = {
"Table": "tab1",
"Head": [
[
{
"value": "",
"colspan": 1,
},
{
"value": "2018",
"colspan": 2
},
{
"value": "2019",
"colspan": 6
}
],
[
{
"value": "",
"colspan": 1,
},
{
"value": "December",
"colspan": 2
},
{
"value": "January",
"colspan": 2
},
{
"value": "February",
"colspan": 2
},
{
"value": "March",
"colspan": 2
}
],
[
{
"value": "",
"colspan": 1,
},
{
"value": "Foo",
"colspan": 1,
},
{
"value": "Boo",
"colspan": 1,
},
{
"value": "Foo",
"colspan": 1,
},
{
"value": "Boo",
"colspan": 1,
},
{
"value": "Foo",
"colspan": 1,
},
{
"value": "Boo",
"colspan": 1,
},
{
"value": "Foo",
"colspan": 1,
},
{
"value": "Boo",
"colspan": 1,
}
]
],
"Body": [
[
{
"value": "Total",
"entryID": -1
},
{
"value": "10",
"entryID": 33
},
{
"value": "24",
"entryID": 34
},
{
"value": "66",
"entryID": 35
},
{
"value": "0",
"entryID": 36
},
{
"value": "23",
"entryID": 37
},
{
"value": "24",
"entryID": 38
},
{
"value": "21",
"entryID": 39
},
{
"value": "10",
"entryID": 40
}
],
[
{
"value": "Row1",
"entryID": -1
},
{
"value": "10",
"entryID": 1
},
{
"value": "12",
"entryID": 2
},
{
"value": "0",
"entryID": 3
},
{
"value": "0",
"entryID": 4
},
{
"value": "0",
"entryID": 5
},
{
"value": "0",
"entryID": 6
},
{
"value": "0",
"entryID": 7
},
{
"value": "0",
"entryID": 8
}
]
]
}
I wanted it to be a three-rowed header (Year, Month and the last row out of the last values of my HEAD-array). I've tried to do it according to
I also tried to do it like described here (so with $("#dataTable").append(entry); but that does not change the outcome.
Edit: Indeed there is an error, not in the command prompt where ng serve is executed, but in the Chrome-console:
Uncaught ReferenceError: createHeaders is not defined at onload (localhost/:13)
Obviously he did not instantiate the method when executing the HTML-Code.
Edit: Why am I trying with jquery? Note: This might not be necessary for the question itself, it is meant as an explanation
Here's an example of my data:
and the desired output:
Now the output has to be editable and these edits should be saved in my database (on button click, that's why I saved the entryID in my mockup).
For the communication from Backend->Frontend I thought about the above format of the raw data (that's why I save an entryID):

