Fill html-table data with jquery fails Uncaught ReferenceError












2















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:



My data



and the desired output:



my 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):










share|improve this question

























  • why do you want to use jquery to manipulate the DOM in angular?

    – L.A
    Nov 14 '18 at 15:18











  • @L.A I edited my question and tried to explain what and why I am trying to do it that way. If you have another possibility feel free to tell me :) (I know you shouldn't ask for these things in a question, still I am open for other ideas)

    – Rüdiger
    Nov 14 '18 at 16:03


















2















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:



My data



and the desired output:



my 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):










share|improve this question

























  • why do you want to use jquery to manipulate the DOM in angular?

    – L.A
    Nov 14 '18 at 15:18











  • @L.A I edited my question and tried to explain what and why I am trying to do it that way. If you have another possibility feel free to tell me :) (I know you shouldn't ask for these things in a question, still I am open for other ideas)

    – Rüdiger
    Nov 14 '18 at 16:03
















2












2








2








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:



My data



and the desired output:



my 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):










share|improve this question
















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:



My data



and the desired output:



my 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):







jquery html angular typescript






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 16:03







Rüdiger

















asked Nov 14 '18 at 12:58









RüdigerRüdiger

219322




219322













  • why do you want to use jquery to manipulate the DOM in angular?

    – L.A
    Nov 14 '18 at 15:18











  • @L.A I edited my question and tried to explain what and why I am trying to do it that way. If you have another possibility feel free to tell me :) (I know you shouldn't ask for these things in a question, still I am open for other ideas)

    – Rüdiger
    Nov 14 '18 at 16:03





















  • why do you want to use jquery to manipulate the DOM in angular?

    – L.A
    Nov 14 '18 at 15:18











  • @L.A I edited my question and tried to explain what and why I am trying to do it that way. If you have another possibility feel free to tell me :) (I know you shouldn't ask for these things in a question, still I am open for other ideas)

    – Rüdiger
    Nov 14 '18 at 16:03



















why do you want to use jquery to manipulate the DOM in angular?

– L.A
Nov 14 '18 at 15:18





why do you want to use jquery to manipulate the DOM in angular?

– L.A
Nov 14 '18 at 15:18













@L.A I edited my question and tried to explain what and why I am trying to do it that way. If you have another possibility feel free to tell me :) (I know you shouldn't ask for these things in a question, still I am open for other ideas)

– Rüdiger
Nov 14 '18 at 16:03







@L.A I edited my question and tried to explain what and why I am trying to do it that way. If you have another possibility feel free to tell me :) (I know you shouldn't ask for these things in a question, still I am open for other ideas)

– Rüdiger
Nov 14 '18 at 16:03














2 Answers
2






active

oldest

votes


















1














If you want something really fast, however, it's best to do without jQuery and reuse as much of the objects you generate as possible.



<table id="dataTable"></table>



const MOCKUP = [{
type: 'thead',
children: [{
type: 'tr',
dataset: {id: 1},
children: [
{type: 'th', textContent: '', colSpan: 1, contentEditable: true},
{type: 'th', textContent: '2018', colSpan: 2, contentEditable: true},
{type: 'th', textContent: '2019', colSpan: 6, contentEditable: true}
]
}, {
type: 'tr',
dataset: {id: 2},
children: [
{type: 'th', textContent: '', colSpan: 1, contentEditable: true},
{type: 'th', textContent: 'December', colSpan: 2, contentEditable: true},
{type: 'th', textContent: 'January', colSpan: 2, contentEditable: true},
{type: 'th', textContent: 'February', colSpan: 2, contentEditable: true},
{type: 'th', textContent: 'March', colSpan: 2, contentEditable: true}
]
}, {
type: 'tr',
dataset: {id: 3},
children: [
{type: 'th', textContent: '', colSpan: 1, contentEditable: true},
{type: 'th', textContent: 'Foo', colSpan: 1, contentEditable: true},
{type: 'th', textContent: 'Boo', colSpan: 1, contentEditable: true},
{type: 'th', textContent: 'Foo', colSpan: 1, contentEditable: true},
{type: 'th', textContent: 'Boo', colSpan: 1, contentEditable: true},
{type: 'th', textContent: 'Foo', colSpan: 1, contentEditable: true},
{type: 'th', textContent: 'Boo', colSpan: 1, contentEditable: true},
{type: 'th', textContent: 'Foo', colSpan: 1, contentEditable: true},
{type: 'th', textContent: 'Boo', colSpan: 1, contentEditable: true}
]
}]
}];

const cache = {};

function assign(destination, source) {
for (const key in source) {
if (source.hasOwnProperty(key)) {
const value = source[key];

if (typeof value === 'object') {
assign(destination[key], value);
} else if (null != destination) {
destination[key] = value;
}
}
}

return destination;
}

function get(data, pathStr) {
const path = pathStr.split('.');
let i = path.length;

while (--i) {
data = data[path[i]];
}

return data;
}

function disassemble(target) {
let element;

while ((element = target.lastElementChild)) {
target.removeChild(element);
disassemble(element);

const type = element.tagName.toLowerCase();
const c = cache[type] || (cache[type] = );

c.push(element);
}
}

function assemble(target, path, data = ) {
const fragment = document.createDocumentFragment();

data.forEach(({type, children, ...config}, i) => {
const element = assign(cache[type] && cache[type].pop() || document.createElement(type), config);
const newPath = `.${i}${path}`;

element.dataset.path = newPath;
assemble(element, `.children${newPath}`, children);

fragment.appendChild(element);
});

target.appendChild(fragment);
}

function render(target, data) {
window.requestAnimationFrame(() => {
disassemble(target);
assemble(target, '', data);
});
}

const table = document.getElementById('dataTable');

table.addEventListener('input', ({target: {dataset, textContent, parentElement}}) => {
// use this to update local data
get(MOCKUP, dataset.path).textContent = textContent;

// easy access to row id (dataset values can only be strings)
parentElement.dataset.id

// raw dataset with all types and deep objects intact
get(MOCKUP, parentElement.dataset.path).dataset.id
});

render(table, MOCKUP);


Not as fast as React, but pretty darn close.






share|improve this answer


























  • So my thought was that, if I would simply generate a String-object that is formatted like an HTML-table and pass it, this might work, but sadly when an entry is changed I cannot determine which entry it was to update it in my database, or am I missing something? But as i write this comment, I see that my solution is not offering this to me either...

    – Rüdiger
    Nov 14 '18 at 18:01











  • @Rüdiger I expanded the example a little bit. Now the table can be edited and there's an easy way to get to the id, or any underlying data you might need to.

    – Eugene Kuzmenko
    Nov 14 '18 at 19:38



















0














I'm not exactly familiar with Angular, but this piece of code seems strange to me



<table #dataTable >
</table>


Maybe you meant



<table id="dataTable">
</table>





share|improve this answer
























  • Thanks for the answer, I found an error! I updated my description

    – Rüdiger
    Nov 14 '18 at 14:04











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53300824%2ffill-html-table-data-with-jquery-fails-uncaught-referenceerror%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














If you want something really fast, however, it's best to do without jQuery and reuse as much of the objects you generate as possible.



<table id="dataTable"></table>



const MOCKUP = [{
type: 'thead',
children: [{
type: 'tr',
dataset: {id: 1},
children: [
{type: 'th', textContent: '', colSpan: 1, contentEditable: true},
{type: 'th', textContent: '2018', colSpan: 2, contentEditable: true},
{type: 'th', textContent: '2019', colSpan: 6, contentEditable: true}
]
}, {
type: 'tr',
dataset: {id: 2},
children: [
{type: 'th', textContent: '', colSpan: 1, contentEditable: true},
{type: 'th', textContent: 'December', colSpan: 2, contentEditable: true},
{type: 'th', textContent: 'January', colSpan: 2, contentEditable: true},
{type: 'th', textContent: 'February', colSpan: 2, contentEditable: true},
{type: 'th', textContent: 'March', colSpan: 2, contentEditable: true}
]
}, {
type: 'tr',
dataset: {id: 3},
children: [
{type: 'th', textContent: '', colSpan: 1, contentEditable: true},
{type: 'th', textContent: 'Foo', colSpan: 1, contentEditable: true},
{type: 'th', textContent: 'Boo', colSpan: 1, contentEditable: true},
{type: 'th', textContent: 'Foo', colSpan: 1, contentEditable: true},
{type: 'th', textContent: 'Boo', colSpan: 1, contentEditable: true},
{type: 'th', textContent: 'Foo', colSpan: 1, contentEditable: true},
{type: 'th', textContent: 'Boo', colSpan: 1, contentEditable: true},
{type: 'th', textContent: 'Foo', colSpan: 1, contentEditable: true},
{type: 'th', textContent: 'Boo', colSpan: 1, contentEditable: true}
]
}]
}];

const cache = {};

function assign(destination, source) {
for (const key in source) {
if (source.hasOwnProperty(key)) {
const value = source[key];

if (typeof value === 'object') {
assign(destination[key], value);
} else if (null != destination) {
destination[key] = value;
}
}
}

return destination;
}

function get(data, pathStr) {
const path = pathStr.split('.');
let i = path.length;

while (--i) {
data = data[path[i]];
}

return data;
}

function disassemble(target) {
let element;

while ((element = target.lastElementChild)) {
target.removeChild(element);
disassemble(element);

const type = element.tagName.toLowerCase();
const c = cache[type] || (cache[type] = );

c.push(element);
}
}

function assemble(target, path, data = ) {
const fragment = document.createDocumentFragment();

data.forEach(({type, children, ...config}, i) => {
const element = assign(cache[type] && cache[type].pop() || document.createElement(type), config);
const newPath = `.${i}${path}`;

element.dataset.path = newPath;
assemble(element, `.children${newPath}`, children);

fragment.appendChild(element);
});

target.appendChild(fragment);
}

function render(target, data) {
window.requestAnimationFrame(() => {
disassemble(target);
assemble(target, '', data);
});
}

const table = document.getElementById('dataTable');

table.addEventListener('input', ({target: {dataset, textContent, parentElement}}) => {
// use this to update local data
get(MOCKUP, dataset.path).textContent = textContent;

// easy access to row id (dataset values can only be strings)
parentElement.dataset.id

// raw dataset with all types and deep objects intact
get(MOCKUP, parentElement.dataset.path).dataset.id
});

render(table, MOCKUP);


Not as fast as React, but pretty darn close.






share|improve this answer


























  • So my thought was that, if I would simply generate a String-object that is formatted like an HTML-table and pass it, this might work, but sadly when an entry is changed I cannot determine which entry it was to update it in my database, or am I missing something? But as i write this comment, I see that my solution is not offering this to me either...

    – Rüdiger
    Nov 14 '18 at 18:01











  • @Rüdiger I expanded the example a little bit. Now the table can be edited and there's an easy way to get to the id, or any underlying data you might need to.

    – Eugene Kuzmenko
    Nov 14 '18 at 19:38
















1














If you want something really fast, however, it's best to do without jQuery and reuse as much of the objects you generate as possible.



<table id="dataTable"></table>



const MOCKUP = [{
type: 'thead',
children: [{
type: 'tr',
dataset: {id: 1},
children: [
{type: 'th', textContent: '', colSpan: 1, contentEditable: true},
{type: 'th', textContent: '2018', colSpan: 2, contentEditable: true},
{type: 'th', textContent: '2019', colSpan: 6, contentEditable: true}
]
}, {
type: 'tr',
dataset: {id: 2},
children: [
{type: 'th', textContent: '', colSpan: 1, contentEditable: true},
{type: 'th', textContent: 'December', colSpan: 2, contentEditable: true},
{type: 'th', textContent: 'January', colSpan: 2, contentEditable: true},
{type: 'th', textContent: 'February', colSpan: 2, contentEditable: true},
{type: 'th', textContent: 'March', colSpan: 2, contentEditable: true}
]
}, {
type: 'tr',
dataset: {id: 3},
children: [
{type: 'th', textContent: '', colSpan: 1, contentEditable: true},
{type: 'th', textContent: 'Foo', colSpan: 1, contentEditable: true},
{type: 'th', textContent: 'Boo', colSpan: 1, contentEditable: true},
{type: 'th', textContent: 'Foo', colSpan: 1, contentEditable: true},
{type: 'th', textContent: 'Boo', colSpan: 1, contentEditable: true},
{type: 'th', textContent: 'Foo', colSpan: 1, contentEditable: true},
{type: 'th', textContent: 'Boo', colSpan: 1, contentEditable: true},
{type: 'th', textContent: 'Foo', colSpan: 1, contentEditable: true},
{type: 'th', textContent: 'Boo', colSpan: 1, contentEditable: true}
]
}]
}];

const cache = {};

function assign(destination, source) {
for (const key in source) {
if (source.hasOwnProperty(key)) {
const value = source[key];

if (typeof value === 'object') {
assign(destination[key], value);
} else if (null != destination) {
destination[key] = value;
}
}
}

return destination;
}

function get(data, pathStr) {
const path = pathStr.split('.');
let i = path.length;

while (--i) {
data = data[path[i]];
}

return data;
}

function disassemble(target) {
let element;

while ((element = target.lastElementChild)) {
target.removeChild(element);
disassemble(element);

const type = element.tagName.toLowerCase();
const c = cache[type] || (cache[type] = );

c.push(element);
}
}

function assemble(target, path, data = ) {
const fragment = document.createDocumentFragment();

data.forEach(({type, children, ...config}, i) => {
const element = assign(cache[type] && cache[type].pop() || document.createElement(type), config);
const newPath = `.${i}${path}`;

element.dataset.path = newPath;
assemble(element, `.children${newPath}`, children);

fragment.appendChild(element);
});

target.appendChild(fragment);
}

function render(target, data) {
window.requestAnimationFrame(() => {
disassemble(target);
assemble(target, '', data);
});
}

const table = document.getElementById('dataTable');

table.addEventListener('input', ({target: {dataset, textContent, parentElement}}) => {
// use this to update local data
get(MOCKUP, dataset.path).textContent = textContent;

// easy access to row id (dataset values can only be strings)
parentElement.dataset.id

// raw dataset with all types and deep objects intact
get(MOCKUP, parentElement.dataset.path).dataset.id
});

render(table, MOCKUP);


Not as fast as React, but pretty darn close.






share|improve this answer


























  • So my thought was that, if I would simply generate a String-object that is formatted like an HTML-table and pass it, this might work, but sadly when an entry is changed I cannot determine which entry it was to update it in my database, or am I missing something? But as i write this comment, I see that my solution is not offering this to me either...

    – Rüdiger
    Nov 14 '18 at 18:01











  • @Rüdiger I expanded the example a little bit. Now the table can be edited and there's an easy way to get to the id, or any underlying data you might need to.

    – Eugene Kuzmenko
    Nov 14 '18 at 19:38














1












1








1







If you want something really fast, however, it's best to do without jQuery and reuse as much of the objects you generate as possible.



<table id="dataTable"></table>



const MOCKUP = [{
type: 'thead',
children: [{
type: 'tr',
dataset: {id: 1},
children: [
{type: 'th', textContent: '', colSpan: 1, contentEditable: true},
{type: 'th', textContent: '2018', colSpan: 2, contentEditable: true},
{type: 'th', textContent: '2019', colSpan: 6, contentEditable: true}
]
}, {
type: 'tr',
dataset: {id: 2},
children: [
{type: 'th', textContent: '', colSpan: 1, contentEditable: true},
{type: 'th', textContent: 'December', colSpan: 2, contentEditable: true},
{type: 'th', textContent: 'January', colSpan: 2, contentEditable: true},
{type: 'th', textContent: 'February', colSpan: 2, contentEditable: true},
{type: 'th', textContent: 'March', colSpan: 2, contentEditable: true}
]
}, {
type: 'tr',
dataset: {id: 3},
children: [
{type: 'th', textContent: '', colSpan: 1, contentEditable: true},
{type: 'th', textContent: 'Foo', colSpan: 1, contentEditable: true},
{type: 'th', textContent: 'Boo', colSpan: 1, contentEditable: true},
{type: 'th', textContent: 'Foo', colSpan: 1, contentEditable: true},
{type: 'th', textContent: 'Boo', colSpan: 1, contentEditable: true},
{type: 'th', textContent: 'Foo', colSpan: 1, contentEditable: true},
{type: 'th', textContent: 'Boo', colSpan: 1, contentEditable: true},
{type: 'th', textContent: 'Foo', colSpan: 1, contentEditable: true},
{type: 'th', textContent: 'Boo', colSpan: 1, contentEditable: true}
]
}]
}];

const cache = {};

function assign(destination, source) {
for (const key in source) {
if (source.hasOwnProperty(key)) {
const value = source[key];

if (typeof value === 'object') {
assign(destination[key], value);
} else if (null != destination) {
destination[key] = value;
}
}
}

return destination;
}

function get(data, pathStr) {
const path = pathStr.split('.');
let i = path.length;

while (--i) {
data = data[path[i]];
}

return data;
}

function disassemble(target) {
let element;

while ((element = target.lastElementChild)) {
target.removeChild(element);
disassemble(element);

const type = element.tagName.toLowerCase();
const c = cache[type] || (cache[type] = );

c.push(element);
}
}

function assemble(target, path, data = ) {
const fragment = document.createDocumentFragment();

data.forEach(({type, children, ...config}, i) => {
const element = assign(cache[type] && cache[type].pop() || document.createElement(type), config);
const newPath = `.${i}${path}`;

element.dataset.path = newPath;
assemble(element, `.children${newPath}`, children);

fragment.appendChild(element);
});

target.appendChild(fragment);
}

function render(target, data) {
window.requestAnimationFrame(() => {
disassemble(target);
assemble(target, '', data);
});
}

const table = document.getElementById('dataTable');

table.addEventListener('input', ({target: {dataset, textContent, parentElement}}) => {
// use this to update local data
get(MOCKUP, dataset.path).textContent = textContent;

// easy access to row id (dataset values can only be strings)
parentElement.dataset.id

// raw dataset with all types and deep objects intact
get(MOCKUP, parentElement.dataset.path).dataset.id
});

render(table, MOCKUP);


Not as fast as React, but pretty darn close.






share|improve this answer















If you want something really fast, however, it's best to do without jQuery and reuse as much of the objects you generate as possible.



<table id="dataTable"></table>



const MOCKUP = [{
type: 'thead',
children: [{
type: 'tr',
dataset: {id: 1},
children: [
{type: 'th', textContent: '', colSpan: 1, contentEditable: true},
{type: 'th', textContent: '2018', colSpan: 2, contentEditable: true},
{type: 'th', textContent: '2019', colSpan: 6, contentEditable: true}
]
}, {
type: 'tr',
dataset: {id: 2},
children: [
{type: 'th', textContent: '', colSpan: 1, contentEditable: true},
{type: 'th', textContent: 'December', colSpan: 2, contentEditable: true},
{type: 'th', textContent: 'January', colSpan: 2, contentEditable: true},
{type: 'th', textContent: 'February', colSpan: 2, contentEditable: true},
{type: 'th', textContent: 'March', colSpan: 2, contentEditable: true}
]
}, {
type: 'tr',
dataset: {id: 3},
children: [
{type: 'th', textContent: '', colSpan: 1, contentEditable: true},
{type: 'th', textContent: 'Foo', colSpan: 1, contentEditable: true},
{type: 'th', textContent: 'Boo', colSpan: 1, contentEditable: true},
{type: 'th', textContent: 'Foo', colSpan: 1, contentEditable: true},
{type: 'th', textContent: 'Boo', colSpan: 1, contentEditable: true},
{type: 'th', textContent: 'Foo', colSpan: 1, contentEditable: true},
{type: 'th', textContent: 'Boo', colSpan: 1, contentEditable: true},
{type: 'th', textContent: 'Foo', colSpan: 1, contentEditable: true},
{type: 'th', textContent: 'Boo', colSpan: 1, contentEditable: true}
]
}]
}];

const cache = {};

function assign(destination, source) {
for (const key in source) {
if (source.hasOwnProperty(key)) {
const value = source[key];

if (typeof value === 'object') {
assign(destination[key], value);
} else if (null != destination) {
destination[key] = value;
}
}
}

return destination;
}

function get(data, pathStr) {
const path = pathStr.split('.');
let i = path.length;

while (--i) {
data = data[path[i]];
}

return data;
}

function disassemble(target) {
let element;

while ((element = target.lastElementChild)) {
target.removeChild(element);
disassemble(element);

const type = element.tagName.toLowerCase();
const c = cache[type] || (cache[type] = );

c.push(element);
}
}

function assemble(target, path, data = ) {
const fragment = document.createDocumentFragment();

data.forEach(({type, children, ...config}, i) => {
const element = assign(cache[type] && cache[type].pop() || document.createElement(type), config);
const newPath = `.${i}${path}`;

element.dataset.path = newPath;
assemble(element, `.children${newPath}`, children);

fragment.appendChild(element);
});

target.appendChild(fragment);
}

function render(target, data) {
window.requestAnimationFrame(() => {
disassemble(target);
assemble(target, '', data);
});
}

const table = document.getElementById('dataTable');

table.addEventListener('input', ({target: {dataset, textContent, parentElement}}) => {
// use this to update local data
get(MOCKUP, dataset.path).textContent = textContent;

// easy access to row id (dataset values can only be strings)
parentElement.dataset.id

// raw dataset with all types and deep objects intact
get(MOCKUP, parentElement.dataset.path).dataset.id
});

render(table, MOCKUP);


Not as fast as React, but pretty darn close.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 14 '18 at 19:33

























answered Nov 14 '18 at 16:50









Eugene KuzmenkoEugene Kuzmenko

74959




74959













  • So my thought was that, if I would simply generate a String-object that is formatted like an HTML-table and pass it, this might work, but sadly when an entry is changed I cannot determine which entry it was to update it in my database, or am I missing something? But as i write this comment, I see that my solution is not offering this to me either...

    – Rüdiger
    Nov 14 '18 at 18:01











  • @Rüdiger I expanded the example a little bit. Now the table can be edited and there's an easy way to get to the id, or any underlying data you might need to.

    – Eugene Kuzmenko
    Nov 14 '18 at 19:38



















  • So my thought was that, if I would simply generate a String-object that is formatted like an HTML-table and pass it, this might work, but sadly when an entry is changed I cannot determine which entry it was to update it in my database, or am I missing something? But as i write this comment, I see that my solution is not offering this to me either...

    – Rüdiger
    Nov 14 '18 at 18:01











  • @Rüdiger I expanded the example a little bit. Now the table can be edited and there's an easy way to get to the id, or any underlying data you might need to.

    – Eugene Kuzmenko
    Nov 14 '18 at 19:38

















So my thought was that, if I would simply generate a String-object that is formatted like an HTML-table and pass it, this might work, but sadly when an entry is changed I cannot determine which entry it was to update it in my database, or am I missing something? But as i write this comment, I see that my solution is not offering this to me either...

– Rüdiger
Nov 14 '18 at 18:01





So my thought was that, if I would simply generate a String-object that is formatted like an HTML-table and pass it, this might work, but sadly when an entry is changed I cannot determine which entry it was to update it in my database, or am I missing something? But as i write this comment, I see that my solution is not offering this to me either...

– Rüdiger
Nov 14 '18 at 18:01













@Rüdiger I expanded the example a little bit. Now the table can be edited and there's an easy way to get to the id, or any underlying data you might need to.

– Eugene Kuzmenko
Nov 14 '18 at 19:38





@Rüdiger I expanded the example a little bit. Now the table can be edited and there's an easy way to get to the id, or any underlying data you might need to.

– Eugene Kuzmenko
Nov 14 '18 at 19:38













0














I'm not exactly familiar with Angular, but this piece of code seems strange to me



<table #dataTable >
</table>


Maybe you meant



<table id="dataTable">
</table>





share|improve this answer
























  • Thanks for the answer, I found an error! I updated my description

    – Rüdiger
    Nov 14 '18 at 14:04
















0














I'm not exactly familiar with Angular, but this piece of code seems strange to me



<table #dataTable >
</table>


Maybe you meant



<table id="dataTable">
</table>





share|improve this answer
























  • Thanks for the answer, I found an error! I updated my description

    – Rüdiger
    Nov 14 '18 at 14:04














0












0








0







I'm not exactly familiar with Angular, but this piece of code seems strange to me



<table #dataTable >
</table>


Maybe you meant



<table id="dataTable">
</table>





share|improve this answer













I'm not exactly familiar with Angular, but this piece of code seems strange to me



<table #dataTable >
</table>


Maybe you meant



<table id="dataTable">
</table>






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 14 '18 at 13:40









Eugene KuzmenkoEugene Kuzmenko

74959




74959













  • Thanks for the answer, I found an error! I updated my description

    – Rüdiger
    Nov 14 '18 at 14:04



















  • Thanks for the answer, I found an error! I updated my description

    – Rüdiger
    Nov 14 '18 at 14:04

















Thanks for the answer, I found an error! I updated my description

– Rüdiger
Nov 14 '18 at 14:04





Thanks for the answer, I found an error! I updated my description

– Rüdiger
Nov 14 '18 at 14:04


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53300824%2ffill-html-table-data-with-jquery-fails-uncaught-referenceerror%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

List item for chat from Array inside array React Native

Thiostrepton

Caerphilly