javascript - 加载 json 列表并处理它
问题描述
我需要一个应用程序
- 从示例 JSON 对象加载项目列表(即客户详细信息),将它们显示在列表中
单击每一行/项目会显示一个带有各个详细信息的模块/弹出窗口
POST 编辑/删除条目
我试图在 div 中显示数据,但它不在列表中,除此之外,我需要为包含客户姓名的每一行添加一个 onclick,并在弹出窗口中显示其他数据有人可以帮我解决这个问题吗?请?我是编程新手。
包.json
[
{
"id": "1",
"firstName": "John",
"lastName": "Doe"
},
{
"id": "2",
"firstName": "Mary",
"lastName": "Peterson"
},
{
"id": "3",
"firstName": "George",
"lastName": "Hansen"
}
]
代码
fetch('package.json')
.then(function (response) {
return response.json();
})
.then(function (data) {
appendData(data);
})
.catch(function (err) {
console.log('error: ' + err);
});
function appendData(data) {
var mainContainer = document.getElementById("myData");
for (var i = 0; i < data.length; i++) {
var div = document.createElement("div");
div.innerHTML = data[i].firstName + ' ' + data[i].lastName;
mainContainer.appendChild(div);
}
}
解决方案
在这里,我将数据视为变量。您可以使用
$.getJSON()
从 JSON 文件中获取数据。
var packageJson;
$(document).ready(function(){
// JSON File Data
packageJson = [
{
"id": "1",
"firstName": "John",
"lastName": "Doe"
},
{
"id": "2",
"firstName": "Mary",
"lastName": "Peterson"
},
{
"id": "3",
"firstName": "George",
"lastName": "Hansen"
}
];
// Insert JSON data to html
$.each(packageJson,function(key,value){
$('#nameList').append('<li data-id="'+value.id+'">'+value.firstName+' '+value.lastName+ '</li>');
});
});
$(document).on('click','#nameList li',function(){
$userId = $(this).attr('data-id');
$user = {};
// loop through the json data and fetch the data to the popup
$.each(packageJson,function(key,value){
if(value.id === $userId)
{
$('#userId p').html(value.id);
$('#userFirstName p').html(value.firstName);
$('#userLastName p').html(value.lastName);
$('#myModal').modal('show'); // Show popup
}
});
});
.mainContent{
margin-left:10px;
padding:10px;
}
#myModal .modal-body p{
display:inline-block;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
<div class='mainContent'>
<h3> User List</h3>
<ul id="nameList">
</ul>
</div>
<!-- Modal -->
<div id="myModal" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">User Details</h4>
</div>
<div class="modal-body">
<div id="userId">
<label>User Id : </label>
<p></p>
</div>
<div id="userFirstName">
<label>First Name : </label>
<p>Some text in the modal.</p>
</div>
<div id="userLastName">
<label>First Name : </label>
<p>Some text in the modal.</p>
</div>
</div>
</div>
</div>
</div>
推荐阅读
- javascript - 是否可以将参数传递给 jQuery .on 处理程序?
- javascript - 变量不在其范围内保持值
- html - 嵌套数组中的 TypeScript for 循环
- cuda - 与 CUDA 中的 SM/核心相关的内存分配和索引
- r - 如何更改 R 中堆叠条的颜色?
- java - 如何以简单的方式在 Java 中保存测量数据?
- flutter - Flutter 将多个 FireStore 流与 Rxdart 相结合
- java - 是否可以使方法(处理程序)从 Spring Cloud Stream 中的多个入站通道接收消息?
- node.js - 在 Kubernetes 上使用 Websocket 构建消息应用程序
- r - ucm 预测问题