简体   繁体   English

修改对象后如何更新html中的数据?

[英]How can i update data in html after modify the object?

I have this function that modify my existing object by ID : 我有此功能,通过ID修改我现有的对象:

   var updateUser = function () {
    var id = $("#userId").val();
    var user = findObjectById(id);

    user.username = $('#username').val();
    user.level = $('#level').val();
    user.regstatus = $('#regstatus').val();
    user.regdate = $('#regdate').val();



    $(".table").show();
    $("#newUser").show();
    $("#userForm").hide();
    $("#updateUser").hide();

}; 

How can i replace my curent data from HTML with modified data ? 如何用修改后的数据替换HTML中的当前数据?

This is my function for creating rows : 这是我创建行的功能:

 var buildRow = function (data) {
    var html = '<tr id = ' + data.id + '>' +
        '<td>' + data.username + '</td>' +
        '<td>' + data.level + '</td>' +
        '<td>' + data.regstatus + '</td>' +
        '<td>' + data.regdate + '</td>' +
        '<td>' +
        '<button class="btn btn-info"value="Edit" onclick="userList.editUser(' + data.id + ')">Edit</button>' + ' ' +
        '<button class="btn btn-danger" value="Delete" onclick="userList.deleteRow(' + data.id + ')">Delete</button>' + '</td>' +
        '</tr>';


    $("#users").append(html);

};

PS i want to update in the same place and my id should be the same. PS我想在同一个地方更新,我的ID应该是相同的。

I modified your prototype jsFiddle a bit, so it contains a working example. 我对您的原型jsFiddle进行了一些修改,因此它包含一个有效的示例。

I needed to modify some parts to get it work, for example updating the object I added a function. 我需要修改某些部分才能使其正常工作,例如,更新添加了功能的对象。

updateObjectById = function (id, obj){
        for (var i = 0, l = userData.length; i < l; i++) {
           if (userData[i].id == id) {
            userData[i] = obj;
        }
    }
};

You should be able to work it out I guess, by the given jsFiddle 我猜你应该能够通过给定的jsFiddle解决它

As I understand it you want to update the table when running "updateUser". 据我了解,您想在运行“ updateUser”时更新表。 There are basically three ways to update the data in your case. 基本上,您可以使用三种方法来更新数据。

  1. Rebuild the whole row 重建整行
  2. Pick out specific cells in the table and change the content 选择表中的特定单元格并更改内容
  3. Use a two way databinding framework 使用两种方式的数据绑定框架

In my experience the best solution, and how f.ex. 以我的经验,最好的解决方案以及如何进行外汇交易。 Backbone does it, is just recreating all the HTML when your data changes. Backbone做到了,只是在数据更改时重新创建所有HTML。 two way databinding is even more powerful, but seems overkill in this situation. 两种方式的数据绑定功能甚至更强大,但在这种情况下显得有些过分。

So basically in your updateUser function: 所以基本上在您的updateUser函数中:

var row = buildRow(user);
var $existingRow = $(id);
if ($existingRow.length) {
  $existingRow.replaceWith(row);
} else {
  $('#users').append(row);
}

Now, this of course requires a change to your "buildRow" function, not making it append the row, but return it. 现在,这当然需要更改您的“ buildRow”函数,而不是使其附加行,而是将其返回。

Hope this gets you further... 希望这能使您更进一步...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM