87,993
社区成员
发帖
与我相关
我的任务
分享
<table class="contentable" id="datatable">
<thead>
<tr>
<th width="7%" name="user">用户名</th>
<th width="10%" name="phone">联系方式</th>
<th width="10%" name="email">用户邮箱</th>
<th width="20%" name="address">联系地址</th>
<th width="5%" name="credit">信用</th>
<th width="8%" name="manner">平台态度</th>
<th width="5%" name="userclass">类别</th>
<th width="8%" name="userimg">头像</th>
<th width="15%" name="operate">操作</th>
</tr>
</thead>
</table>// 建立table下的tbody
$("#datatable").append("<tbody></tbody>");
$.ajax({
type: "post",
url: "../php/getuser.php",
async: true,
data: {},
dataType: "json",
success: function(data) {
// 获取数据和条数
console.log(data);
console.log("data的长度" + data.length);
//获取第一行的值,用来获取json字符串的key值
var jsonfirst = data[0].valueOf();
// 获取数据表每一行的内容
var thobj = $("thead").children("tr").children("th");
// 打印data.length行数据,有重复问题
$.each(data, function(index, obj) {
$("#datatable tbody").append("<tr></tr>" );
// 获取后台所传数据keyname
thobj.each(function(){
var thobjname = $(this).attr('name');
for(var key in jsonfirst) {
var keyvalue = key;
if(thobjname == keyvalue ){
console.log(obj[thobjname]);
$("#datatable tbody tr").append("<td>" + obj[thobjname] + "</td>");
}else if(thobjname=="operate"){
console.log("操作");
$('#datatable tbody tr').append("<td><button class='functio editicon'>更改</button><button class='functio deleteicon'>删除</button></td>");
return false;
}else{
}
}
});
});
},
error: function() {
error("获取数据错误");
}
});



