简体   繁体   English

如何将 jquery stringyfy 数据附加到具有 4 个用户的可编辑(tr 和 td)数据的表中,例如循环

[英]How to append jquery stringyfy data to table with editable (tr and td) data of 4 users one by one like loop

I am getting json.stringyfy data and want to append all json.stringyfy data to table,with each tr editable but I don't know that much part of jquery so unable to get any idea can anyone tell me what should do to achieve this?我正在获取 json.stringyfy 数据并希望将所有 json.stringyfy 数据附加到表中,每个 tr 都可编辑但我不知道 jquery 的大部分内容,因此无法获得任何想法谁能告诉我应该怎么做才能实现这一目标?

Json data got in response得到响应的 Json 数据

[{"id":"1","name":"abc"},     user  id 1 data
{"id":"2","name":"def"},      user  id 2 data
{"id":"3","name":"xyz"},      user id 3 data
{"id":"4","name":"aaa"}]      user id 4 data

Ajax call with success function to handle stringyfy data带有成功函数的 Ajax 调用来处理 stringyf 数据

$.ajax({
            url: "<?= base_url('Test_controller/show_user') ?>",
            type: 'POST',
            data: {
            },
            success: function (response) {
                var myJSON = JSON.stringify(response);
// code Here  to append data in table one by one with content editable tr

            }
        });

Html table html表

<table class="show_data">
     <tr>
          <th>User id</th>
          <th>User name</th>
     </tr>
</table>

Just take your data and append it to the table.只需获取您的数据并将其附加到表格中即可。 Loop through each entry in your data and (if using jquery) do .append().循环遍历数据中的每个条目并(如果使用 jquery)执行 .append()。 I just create a simple variable with your json but you can do this with your myJSON variable inside of the success function.我只是用你的 json 创建一个简单的变量,但你可以用成功函数内的 myJSON 变量来做到这一点。

 var data = [{"id":"1","name":"abc"}, {"id":"2","name":"def"}, {"id":"3","name":"xyz"}, {"id":"4","name":"aaa"}] data.forEach(user => { $('.show_data').append(`<tr><td>${user.id}</td><td>${user.name}</td></tr>`); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <table class="show_data"> <tr> <th>User id</th> <th>User name</th> </tr> </table>

Edit编辑

You are getting that in error because you are using JSON.stringify which makes your json a string.您之所以出错,是因为您使用的是 JSON.stringify,它使您的 json 成为字符串。 You can't iterate on a string.你不能迭代一个字符串。 Just use response instead of JSON.stringify(response);只需使用 response 而不是 JSON.stringify(response);

You may use jQuery.each() and build elements on the fly:您可以使用jQuery.each()并动态构建元素:

 var myJSON = [{"id": "1", "name": "abc"}, {"id": "2", "name": "def"}, {"id": "3", "name": "xyz"}, {"id": "4", "name": "aaa"}]; $.each(myJSON, function (idx, ele) { $('.show_data tbody').append( $('<tr/>') // create a TR .append($('<td/>', {text: ele.id})) // append the first TD .append($('<td/>', {text: ele.name})) // append the second TD ); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <table class="show_data"> <thead> <th>User id</th> <th>User name</th> </thead> <tbody> </tbody> </table>

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

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