user6018902 user6018902 - 3 months ago 7
Javascript Question

How to update a data from a table using data-atributes in jQUery

I have this table in html and I need to update a user using the data-userid, I make I function that to this but this function didn't change anything in web page.

Here, I have the html table code for data-userid:

<table id="tblList">
<tbody id="someTest">
<tr data-userid="801992084067"></tr>
<tr data-userid="451207954179"></tr>
<tr data-userid="310896831399"></tr>
<tr data-userid="863939754980"></tr>
<tr data-userid="1123542226482"></tr>
</tbody>
</table>


And here I have update function.

function updateUser(userId, user) {

var foundUser = findUser(userId);


foundUser.username = user.username;
foundUser.level = user.level;
foundUser.registrationStatus = user.registrationStatus;
foundUser.registrationDate = user.registrationDate;


for(var i = 1; i<userId.length; i++){
$("#someTest tr[data-userid = 'userid " + userId[i] + "']").each(function () {
// if (rowId === userId) {

var table = $('#tblList');

var row = "<tr data-userid=" + foundUser.id + ">"
+ " <td>"
+ " <img src='resources/img/edit.png' alt='Edit' class='btnEdit'/>"
+ " <img src='resources/img/delete.png' alt='deleteUser' class='btnDelete'/>"
+ "</td>"
+ " <td>" + foundUser.username + "</td>"
+ " <td>" + foundUser.level + "</td>"
+ " <td>" + foundUser.registrationStatus + "</td>"
+ " <td>" + foundUser.registrationDate + "</td>"
+ "</tr>";
table.append(row);
// }
});
}
hidePopup();
}


My question is the following: What to do in updateUser function because when I change a user, to see that change in browser?

Answer

try this

function updateUser(userId, user) {

        var foundUser = findUser(userId);


        foundUser.username = user.username;
        foundUser.level = user.level;
        foundUser.registrationStatus = user.registrationStatus;
        foundUser.registrationDate = user.registrationDate;

        var row = " <td>"
                        + "     <img src='resources/img/edit.png' alt='Edit' class='btnEdit'/>"
                        + "     <img src='resources/img/delete.png' alt='deleteUser' class='btnDelete'/>"
                        + "</td>"
                        + " <td>" + foundUser.username + "</td>"
                        + " <td>" + foundUser.level + "</td>"
                        + " <td>" + foundUser.registrationStatus + "</td>"
                        + " <td>" + foundUser.registrationDate + "</td>";


        $("#someTest tr[data-userid = '" + userId + "']").html(row)
        hidePopup();
    }