javascript - How to add json complex format to html table
问题描述
I used AJAX and get JSON response then I want to map json to table html like
# | TypeID | TypeDesc | CreateBy
1 | 000001 | AAAAAA | Adam
2 | 000002 | BBBBBB | James
This is my json
{
"records": [{
"type_id": 000001,
"type_desc": "AAAAAA ",
"type_createby": "Adam"
}, {
"type_id": 000002,
"type_desc": "BBBBBB",
"type_createby": "James"
}, {
"type_id": 000003,
"type_desc": "CCCCCC",
"type_createby": "Sam"
}]
}
and this is I'm trying
success: function (response) {
$('#table-container').html("");
$.each(response, function (index) {
var tableRow = "";
var row = 1;
tableRow = $('<tr/>');
tableRow.append("<td>" + row + "</td>");
row = row + 1;
tableRow.append("<td>" + response[index].type_id + "</td>");
tableRow.append("<td>" + response[index].type_desc + "</td>");
tableRow.append("<td>" + response[index].type_createby + "</td>");
$('#table-container').append(tableRow);
});
},
My display show the table but the data still show "undefined". There are two questions I have.
1.How to define the correct data ?
2.How to loop to show 5 row and get paging with javascript ?
解决方案
使用“
response.records
”;if (response && response.records) { $.each(response.records, function (index, value) { var tableRow = ""; var row = 1; tableRow = $(''); tableRow.append("" + row + ""); row = row + 1; tableRow.append("" + value.type_id + ""); tableRow.append("" + value.type_desc + ""); tableRow.append("" + value .type_createby + ""); $('#table-container').append(tableRow); }); }
分页的实现方式不同,具体取决于使用的自定义表格控件。
推荐阅读
- ruby-on-rails - 我收到此错误我通常知道如何解决它,但在 SavedHairstylesController#create 中不起作用 Pundit::AuthorizationNotPerformedError
- c - 动态分配整数数组,将它们存储在文件中并在 c 中找到它们的平均值
- sql - 将 SQL 查询简化为更短的查询
- java - 找出 GCP IAM 中服务帐户密钥的最后活动
- symfony - 来自多个实体的多对多映射
- spring-boot - 如何增加 Spring Boot logback-spring.xml 的日志记录天数
- dart - 如何在没有错误的情况下有条件地在 StreamBuilder、构建器函数中重定向?
- datetime - 使用 plone.restapi 插件返回错误查询 plone 通过搜索获取今天的文件
- angular - Angular 7 - 在输入下拉列表中显示“angular-bootstrap-datetimepicker”
- linux - 用 sed 和单引号以及 reg x 替换复杂的字符串