/    Sign up×
Community /Pin to ProfileBookmark

Update a HTML row from JSON?

I am searching for a way to update a row based on JSON values. Anybody a clue?
Do I need to add keys in the `<td>` in order to update a certain cell?

https://jsfiddle.net/v0q4La2y/13/

to post a comment
JavaScript

11 Comments(s)

Copy linkTweet thisAlerts:
@SempervivumFeb 16.2022 — @sibert#1642598

Unfortunately I do not completely understand what you intend to do:

The button at the bottom of the page is outside the table. You want to look up the row in the table based on the id in the JSON and then change the content of the first cell to the name in the JSON? Correct?
Copy linkTweet thisAlerts:
@sibertauthorFeb 17.2022 — > @Sempervivum#1642613 You want to look up the row in the table based on the id in the JSON and then change the content of the first cell to the name in the JSON? Correct?

I select a row in a table and edit the record in a form. The response output from the form is JSON. The table can consists of several columns, so the routine that replaces the edited row may be dynamic.
Copy linkTweet thisAlerts:
@SempervivumFeb 17.2022 — I extended your fiddle a bit, check if this helps you:

https://jsfiddle.net/Sempervivum/3dLyjn6z/3/

Modifications at the bottom.
Copy linkTweet thisAlerts:
@sibertauthorFeb 17.2022 — Thank you! Do you mean that every &lt;td&gt; must have a class in order to target them?

``<i>
</i>&lt;tr data-id=${element.user_id}&gt;
&lt;td class="task_subject"&gt;subject 1&lt;/td&gt;
&lt;td class="task_desc"&gt;Description&lt;/td&gt;
&lt;td class="task_status"&gt;2&lt;/td&gt;
&lt;/tr&gt;<i>
</i>
`</CODE>
Or is it better to delete a modified row and dynamically create a new and replace this row?<br/>
In order to avoid hardcoding each <C>
<td>` for maybe 20 columns?
Copy linkTweet thisAlerts:
@SempervivumFeb 17.2022 — Not necessarrily. Alternatively you can address the cells by their index:

`const cell = document.querySelector('table tr[data-id="' + data.user_id + '"] td:nth-of-type(0)');`

https://developer.mozilla.org/en-US/docs/Web/CSS/:nth-of-type

Doesn't need classes but there is some risk: When inserting columns into the table, the index has to be modified.
Copy linkTweet thisAlerts:
@sibertauthorFeb 17.2022 — > @Sempervivum#1642628

>Can it be sure that JSON deliver the columns always in the same order?


The sequence of the columns is defined by this Javascript or the template string:
``<i>
</i> data.forEach(element =&gt; table.insertAdjacentHTML('beforebegin',

<tr data-id="${element.user_id}">
<td class="task-subject">${element.user_name}</td>
<td class="edit"></td>
<td class="more"></td>
</tr>

));<i>
</i>
``

The sequence of the key value pairs in the JSON doesn't matter.
Copy linkTweet thisAlerts:
@sibertauthorFeb 17.2022 — In my dreams I am hoping for the same dynamic code as populating a form, but for a table row:

``<i>
</i>function fillform(jsondata) {
const {
elements
} = document.querySelector('form')
for (const [key, value] of Object.entries(data)) {
const field = elements.namedItem(key)
if (field) field.value = value
}
}<i>
</i>
`</CODE>

Pseudo code:

<CODE>
`<i>
</i>function filltablerow(jsondata) {
const {
elements
} = document.querySelector('table tr $[id]')
for (const [key, value] of Object.entries(data)) {
const field = elements.namedItem(key)
if (field) field.value = value
}
}<i>
</i>
``


Possible?
Copy linkTweet thisAlerts:
@SempervivumFeb 17.2022 — Yes, this can be done:

https://jsfiddle.net/Sempervivum/qhdm4r0u/4/

Core sections in javascript:
``<i>
</i> function filltable(data) {
html = '';
data.forEach(element =&gt; {
// add opening tr tag, user ID in data attribute
// in order to locate the row easily later on:
html +=
<tr data-user-id="${element.user_id}">;
// for each key in current record from data:
for (let key in element) {
// add td tag, current key in data attribute:
const val = element[key];
html +=
<td data-key="${key}">${val}</td>;
}
// add edit and more button, close tr tag:
html +=
<td class="edit"></td><td class="more"></td></tr>;
});
tBody.innerHTML = html;
}<i>
</i>
`</CODE>
<CODE>
`<i>
</i> function update() {
// provide row for this user:
const row = document.querySelector('table tr[data-user-id="' + data.user_id + '"]');
// for each key in current record from data:
for (let key in data) {
// update content of td element:
const val = data[key];
row.querySelector('td[data-key="' + key + '"]').innerHTML = val;
}
}<i>
</i>
``
Copy linkTweet thisAlerts:
@sibertauthorFeb 19.2022 — Thank you indeed. This solution works perfect as long as the JSON corresponds to the list columns. But how do I do if the list columns are fewer than the JSON keys? The iteration is aborted when no match is found.

https://jsfiddle.net/u1v0Lkq9/
Copy linkTweet thisAlerts:
@SempervivumFeb 19.2022 — @sibert#1642672

This can be catched easily by checking if the key exists in the table row:
``<i>
</i> function update() {
// provide row for this user:
const row = document.querySelector('table tr[data-user-id="' + data.user_id + '"]');
// for each key in current record from data:
for (let key in data) {
// update content of td element:
const val = data[key],
cell =row.querySelector('td[data-key="' + key + '"]') ;
if (cell) cell.innerHTML = val;
}
}<i>
</i>
``
Copy linkTweet thisAlerts:
@sibertauthorFeb 19.2022 — > @Sempervivum#1642673 by checking if the key exists in the table cell

Yes, the iteration will continue, and the cell is updated. Thank you!

https://jsfiddle.net/jv28b503/3/
×

Success!

Help @sibert spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...