简体   繁体   English

来自Ajax调用的JSON数据

[英]JSON Data from Ajax Call

I have JSOn data coming from my Function, i do not need all the data and had to create some data too based upon the data i am getting, There are many code outlying which specify how can i loop over json data and display it 我有来自函数的JSOn数据,我不需要所有数据,也必须根据要获取的数据创建一些数据,其中有很多代码偏僻,这些代码指定了如何循环遍历json数据并显示它

$.ajax({url: 'page.php',
        data: {id: 1},
        dataType: 'json',
        type:'POST',
        success: function (json) {
            div.html('<table align="left" width="70%" class="table table-striped borderless p-l-xl">'+
                    '<tr>'+
                        '<td>d1</td><td>d2</td><td>Action</td></tr>'+
                        '<tr><td>'+json.d1+'</td><td>'+json.d2+'</td>'+
                        '<td><a href="javascript:;" class="t" data-id="'+ json.id +'" data-sid="'+json.sid+'">Delete</a></td>'+
                    '</tr>'+
                '</table>').removeClass('loading');
            } 
    });

Tried using this code 尝试使用此代码

How to iterate JSON object with jQuery 如何使用jQuery迭代JSON对象

but i am confused how i feed my own href with custom data and separate headers 但是我很困惑如何用自定义数据和单独的标题填充自己的href

Did a Small Update 做了一个小更新

$.each(json,function(index,item){
                $('<table><tr>').append(
                    $("<td>").text(item.d1),
                    $("<td>").text(item.d2),
                    $("<td>").html('<a href="javascript:;" class="d" data-sid="'+ json.id+'" data-id="'+json.id+'">Delete</a></td>')
                ).appendTo(div);
        });

now it works and create a seperate for each record, amd missing headers 现在它可以工作并为每个记录创建一个单独的文件,并且缺少标题

while i want that all the rows should be under one TABLE Tag and have headers 而我希望所有行都应在一个TABLE标记下并具有标题

Following your update, it's a little clearer. 更新之后,它会变得更加清晰。

You say 你说

"while i want that all the rows should be under one TABLE Tag and have headers" “虽然我希望所有行都应在一个TABLE标记下并具有标题”

...so the simple solution to that is to create the table first before you start the loop, and add the headers at that time as well. ...因此,一种简单的解决方案是在开始循环之前首先创建表,并在此时添加标头。 Then each time in the loop, just append a new row to the existing table, instead of creating a whole new table. 然后,每次循环时,只需将新行追加到现有表中即可,而不是创建整个新表。

This logic isn't too complicated once you think about it like that 一想到这种逻辑就不会太复杂

Here is a demo which illustrates what I mean, using some dummy data instead of the response from the AJAX. 这是一个演示了我的意思的演示,它使用一些虚拟数据而不是来自AJAX的响应。

 //dummy test data var json = [{ "id": 1, "d1": "Text 1", "d2": "Text 2" }, { "id": 2, "d1": "Text A", "d2": "Text B" }, { "id": 3, "d1": "Text X", "d2": "Text Y" }, ]; var div = $("#div1"); var table = $('<table id="table1"><tr><th>Heading 1</th><th>Heading 2</th><th>Heading 3</th></tr>'); $.each(json, function(index, item) { var row = $("<tr>"); row.append( $("<td>").text(item.d1), $("<td>").text(item.d2), $("<td>").html('<a href="javascript:;" class="d" data-sid="' + json.id + '" data-id="' + json.id + '">Delete</a></td>') ); row.appendTo(table); }); table.appendTo(div); 
 table { border-collapse: collapse; } th, td { border: 1px solid #cccccc; padding: 5px; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="div1"></div> 

I believe this is what you are trying to achieve. 我相信这是您要实现的目标。 (without jQuery other than the $.get()). (没有$ .get()以外的jQuery)。

  1. Given some data (here are fake blog posts) 给定一些数据(这是虚假的博客文章)
  2. Modify the data is some way 修改数据是某种方式
  3. Place the modified data into a table 将修改后的数据放入表中
  4. Provide an "Action" column with a link to delete the row 在“操作”列中提供删除行的链接

 $.get('https://jsonplaceholder.typicode.com/posts', transformData); function transformData(posts) { let tableData = posts.map(post => { return { id: post.id, title: post.title, added: 'added' } }); makeTable(tableData); } function deleterow(el) { alert('Deleting row with id: ' + el.dataset.id); } function makeTable(data) { var table = document.createElement("table"); table.style.border = "1px"; var headerRow = document.createElement("thead"); headerRow.style.backgroundColor = "#ccc"; Object.keys(data[0]).forEach(key => { let headerCol = document.createElement("td"); headerCol.textContent = key; headerRow.appendChild(headerCol); }); let actionCol = document.createElement('td'); actionCol.textContent = 'Action'; headerRow.appendChild(actionCol); table.appendChild(headerRow); data.forEach(item => { let row = document.createElement("tr"); Object.keys(item).forEach(key => { let col = document.createElement("td"); col.textContent = item[key]; row.appendChild(col); }); let action = document.createElement("a"); action.href = 'javascript:void(0);'; action.textContent = 'Delete'; action.setAttribute("onclick", 'deleterow(this)'); action.dataset.id = item.id; action.dataset.title = item.title; action.dataset.added = item.added; row.appendChild(action); table.appendChild(row); }); document.body.appendChild(table); } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> 

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

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