Updating Table rows using jQuery

25,457

Solution 1

I think there may be a couple things you could do

$('#tr_' + objUser.id).find("td").eq(1).html(objUser.id);
$('#tr_' + objUser.id).find("td").eq(2).html(objUser.username);
$('#tr_' + objUser.id).find("td").eq(3).html(objUser.fname);
$('#tr_' + objUser.id).find("td").eq(4).html(objUser.lname);

Could become:

$this = $('#tr_' + objUser.id).find("td");
$this.eq(1).html(objUser.id);
// And so on

You could do an outright replacements if it already exists

if ($('#tr_' + objUser.id).length) {
    var newHtml = '<td>' + .objUser.username + '</td>'
            + '<td>' + objUser.fname + '</td>'
            + '<td>' + objUser.lname + '</td>';
    $('#tr_' + objUser.id).html(newHtml);
}

Solution 2

$(function() {
    var objUser = {"id":2,"username":"j.smith","fname":"john","lname":"smith"};
    var objKeys = ["username", "fname", "lname"];

    $('#tr_' + objUser.id + ' td').each(function(i) {
        $(this).text(objUser[objKeys[i]]);
    });
});

jsFiddle code

Share:
25,457
Florian Mertens
Author by

Florian Mertens

Software Freelancer +44/(0)7594 815 778 florian_mrt [at] hotmail.com Diplomas BSc Information Systems MSc Robotics Competencies Programming: Visual Basic 4.0 5.0 6.0 .Net, C#, .Net 1.1 2.0 3.5, Java, C++, PHP 4 &amp; 5, XHTML, CSS 3.0, Javascript, VBscript, Smarty, jQuery, ActionScript 2 &amp; 3 Operating Systems: WinXP, WinVista, Win2003 Enterprise Server, Win2008 Datacenter Server, Linux Red Hat Fedora Core, Linux Gentoo, Linux Debian, Linux Slackware, Linux Ubuntu Software Studios: MS Office 2010, 3D Studio Max, MatLab, Eclipse, MS Visual Studio 2005, Dreamweaver, Vectorworks 2009, Adobe Studio CS5, Solr, Red5 Management Systems: IIS 5.0 6.0 7.5, Apache, Squirrelmail, Joomla, PHPbb, Drupal, DirectX 9.0, MS SQL Server 2005, Oracle 10g, MySQL 5.0, SQLite, SVN, Git Languages Dutch Writing/fluently speaking. Mother tongue. English Writing/fluently speaking. French Writing/fluently speaking. German Writing/fluently speaking.

Updated on December 12, 2020

Comments

  • Florian Mertens
    Florian Mertens over 3 years

    Suppose you have a html table:

    <table id="data">
        <thead>
            <tr>
                <td>ID</td>
                <td>Username</td>
                <td>First Name</td>
                <td>Last Name</td>
            </tr>
        </thead>
        <tbody>
            <?php foreach($arrData as $arrRecord) { ?>
            <tr id="tr_<?php echo $arrRecord["id"]; ?>">
                <td><?php echo $arrRecord["username"]; ?></td>
                <td><?php echo $arrRecord["fname"]; ?></td>
                <td><?php echo $arrRecord["lname"]; ?></td>
            </tr>
            <?php }?>
        </tbody>
    </table>
    

    And you have a JSON object:

    objUser = {"id":12,"username":"j.smith","fname":"john","lname":"smith"};
    

    And you want to change that record within the corresponding table row (assuming that this table already has a row with id="tr_12"):

    $('#tr_' + objUser.id).find("td").eq(1).html(objUser.id);
    $('#tr_' + objUser.id).find("td").eq(2).html(objUser.username);
    $('#tr_' + objUser.id).find("td").eq(3).html(objUser.fname);
    $('#tr_' + objUser.id).find("td").eq(4).html(objUser.lname);
    

    Is there a faster/cleaner way of updating table rows using jQuery, than this last shown code block?