JavaScript
x
46
46
1
function createTable(data_array){
2
const billing_table_body = document.querySelector('#billing_progile_Table > tbody')
3
4
5
6
//we loop through object array and have access to each individual JSON
7
for(var i = 0; i<objarray.length;i++){
8
console.log("data : ",objarray[i].profileName)
9
10
//create row
11
const tr = document.createElement('tr'); //creating the row
12
console.log('creating new row');
13
14
15
16
//append individual tds
17
const td = document.createElement('td')
18
td.textContent = objarray[i].profileName//appends data from the json cell
19
td.className = 'text_td';
20
tr.appendChild(td);
21
22
const td_two = document.createElement('td')
23
td_two.textContent = objarray[i].cardemail
24
td.className = 'text_td';
25
tr.appendChild(td_two);
26
27
const td_three = document.createElement('td')
28
td_two.textContent = objarray[i].cardownername
29
td.className = 'text_td';
30
tr.appendChild(td_three);
31
32
const td_four = document.createElement('td')
33
td_two.textContent = objarray[i].cardnumber
34
td.className = 'text_td';
35
tr.appendChild(td_four);
36
37
38
39
40
41
//append whole row to tr
42
billing_table_body.appendChild(tr);
43
}
44
45
}
46
im trying to append the cells into the table with their data but the table won’t allow me to do it and I need to write it like this because im trying to access specific objects of the json array. any help im new to JAVASCRIPT AND JSON
Advertisement
Answer
Please stop adding row and cells with createElement() method…!
JavaScript
1
16
16
1
const billing_table_body = document.querySelector('#billing_progile_Table > tbody')
2
3
function createRows(data_array)
4
{
5
data_array.forEach(el =>
6
{
7
let newRow = billing_table_body.insertRow()
8
newRow.insertCell().textContent = el.profileName
9
newRow.insertCell().textContent = el.cardemail
10
newRow.insertCell().textContent = el.cardownername
11
newRow.insertCell().textContent = el.cardnumber
12
13
newRow.querySelectorAll('td').forEach(td=>td.className='text_td')
14
})
15
}
16