使用AJAX提取数据并显示在数据表中,但是未显示在表中。 好的,我的jquery数据表代码是
<script>
$(document).ready(function() {
$.ajax({
url:"data/companylist-ajax.php",
success:function(data) {
var myobj = JSON.parse(data);
$('#companylist').DataTable(
"data": myobj,
"columns": [
{ "data": "name" },
{ "data": "regoffice_city" },
{ "data": "regoffice_country" },
{ "data": "is_customer" },
{ "data": "is_supplier" }
]
)
}
} );
} );
</script>
我用于JSON数据的PHP代码是
$query = mysqli_query($dbc,"SELECT * FROM `comp_companies` LIMIT 0, 500");
$result = mysqli_fetch_assoc($query);
do { $newdata = json_encode($result); echo $newdata; } while($result = mysqli_fetch_assoc($query));
页面上显示的数据是
{"company_id":"1","company_sector":"0","company_type":"TEST-COMP","individual":"0","name":"Test Ltd","regoffice_1":"18 Alma Road","regoffice_2":"","regoffice_3":"","regoffice_city":"Hinckley","regoffice_county":"Leicestershire","regoffice_country":"United Kingdom","regoffice_postcode":"LE10 1PN","is_customer":"1","customer_approved":"0","is_supplier":"0","supplier_approved":"0","registration_no":"123"}
当我直接访问页面时会显示数据,但不会显示在数据表中
不知道我是否做错了事
答案 0 :(得分:0)
您可以像这样使用数据表的ajax选项
$('#companylist').DataTable({
dom: "Bfrtip",
ajax: {
url: "data/companylist-ajax.php",
type: 'POST'
},
aLengthMenu: [[10, 25, 50], [10, 25, 50]],
bPaginate:true,
scrollX: "100%",
order: [[ 0, "desc" ]],
scrollCollapse: true,
columns: [
{ "data": "name" },
{ "data": "regoffice_city" },
{ "data": "regoffice_country" },
{ "data": "is_customer" },
{ "data": "is_supplier" }
]
});
服务器的json响应应该是这样
{
"data": [
[
"Abc",
"xyz",
"abc",
"yes",
"no"
],
[
"Pqr",
"xyz",
"abc",
"no",
"yes"
],
}