/    Sign up×
Community /Pin to ProfileBookmark

Replace an edited table row

fetch() is not reliable AFAIK because it is async. I have tried to replace a table row in a reliable way for some days and found this way to replace a table row.

  • 1. Edit record

  • 2. fetch the record with delay

  • 3. update the table
  • “`
    async function update_sql(url, json) {
    let body = get_updatebody(json)
    let obj = JSON.parse(json);
    let id = obj[mod_id]
    await fetch(url, body)
    .then(response => (response.json())) //no return values
    .then(get_newrow(id));
    finish()
    tippy2()
    }

    function get_newrow(id) {
    setTimeout(function() {
    let url = “https://api3.go4webdev.org/” + mod + “/id/” + id;
    fetch(url)
    .then(response => response.json())
    .then(newdata => replace_tablerow(newdata))
    }, 250);
    }

    function replace_tablerow(data) {
    alert(JSON.stringify(data))
    let row = document.querySelector(‘table tr[data-id=”‘ + data[mod_id] + ‘”]’);
    for (let key in data) {
    let val = data[key];
    let cell = row.querySelector(‘td[data-key=”‘ + key + ‘”]’);
    if (cell) cell.innerHTML = val;
    }
    }
    “`

    My question is: Is there a even more reliable way to edit a record and update the table row with new values?

    Theoretically I can edit and send return values, but this only seems to result in `object.Promise` answer. I interpret that this is not reliable at all…

  • 1. Edit record and send return values

  • 2. Replace the table row with returned values
  • to post a comment
    JavaScript

    5 Comments(s)

    Copy linkTweet thisAlerts:
    @SempervivumSep 08.2022 — @sibert#1646873

    I'm fairly sure that fetch or ajax will work reliably when being used properly.

    Using a timeout in order to wait for the server's response will not work reliably as the time is unpredictable. However, AFAIK, this doesn't apply to your code, the timeout causes a delay only.

    I advise against mixing up async/await and a chain of `then`.

    Unfortunately I'm not able to test and modify your code as it's incomplete and my PC is broken currently.
    Copy linkTweet thisAlerts:
    @sibertauthorSep 09.2022 — > @Sempervivum#1646889 I advise against mixing up async/await and a chain of then.

    I found another solution without using "delay". Do you think this is better?

    ``<i>
    </i>async function update_sql(url, json) {
    let body = get_updatebody(json)
    let obj = JSON.parse(json);
    let id = obj[mod_id]
    await fetch(url, body);
    get_newrow(id);
    }

    function get_newrow(id) {
    let url = "https://api3.go4webdev.org/" + mod + "/id/" + id;
    fetch(url)
    .then(response =&gt; response.json())
    .then(newdata =&gt; replace_tablerow(newdata))
    }<i>
    </i>
    ``

    live: https://task.go4webdev.org/usr
    Copy linkTweet thisAlerts:
    @marionlankfordSep 12.2022 — Use jQuery.replaceWith()


    $(document).ready(function() {

    $('#mytable .edit').click( function() {

    var tr = $(this).parent();

    var new_row = '<tr class="new_row"><td>3</td><td>4</td><td>Save</td></tr>';

    tr.replaceWith(new_row);

    });

    });
    Copy linkTweet thisAlerts:
    @sibertauthorSep 12.2022 — > @marionlankford#1646936 Use jQuery

    Thanks for the tip. But my goal is to avoid dependencies :-)
    Copy linkTweet thisAlerts:
    @SempervivumSep 13.2022 — @sibert#1646890

    Yes, IMO this looks better.

    Is your issue fixed by this?

    In your initial posting I spot this code:
    ``<i>
    </i>async function update_sql(url, json) {
    let body = get_updatebody(json)
    let obj = JSON.parse(json);
    let id = obj[mod_id]
    await fetch(url, body)
    .then(response =&gt; (response.json())) //no return values
    .then(get_newrow(id));
    finish()
    tippy2()
    }<i>
    </i>
    `</CODE>
    I suspect that finish() and tippy() rely on the response from the server and should be applied to the new version of the row? If so they should be placed inside the ajax callback instead:
    <CODE>
    `<i>
    </i>function replace_tablerow(data) {
    alert(JSON.stringify(data))
    let row = document.querySelector('table tr[data-id="' + data[mod_id] + '"]');
    for (let key in data) {
    let val = data[key];
    let cell = row.querySelector('td[data-key="' + key + '"]');
    if (cell) {
    cell.innerHTML = val;
    finish();
    tippy();
    }
    }
    }<i>
    </i>
    ``
    ×

    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.18,
    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,
    )...