这是我的Db Table.i想要在模态弹出窗口中的bootstrap数据表中显示表值。
main.html中
<a href="#myModal" id="custId" data-toggle="modal" data-id="special-fy" class="btn btn-primary">Click Here</a>
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h5 class="modal-title"><i class="glyphicon glyphicon-list"></i> Stone Details</h5>
</div>
<div class="modal-body">
<div class="fetched-data">
<table id="example" class="table tab_header table-bordered cmntbl" cellspacing="0" width="100%">
<thead>
<tr>
<th>ID</th>
<th>NAME</th>
<th>COUNTRY</th>
</tr>
</thead>
</table>
</div>
</div>
<div class="modal-footer">
</div>
</div>
</div>
</div>
和我的jquery一样:
$(document).ready(function() {
var table = $('#example').DataTable( {
"bLengthChange": false,
"ajax": {
"type" : "GET",
"url" : "data.php",
"dataSrc": function (json) {
return json.data;
}
}
});
});
和我的data.php一样:
$sql_sel = mysqli_query($con,"SELECT * FROM `testt2`");
$array = array();
$array['data'] = array();
while($res_sel = mysqli_fetch_row($sql_sel)){
$array['data'][] = $res_sel;
}
echo json_encode($array);
一切正常,但是我想从带有json数组和这些列名的data.php动态传递表头(ID,NAME,COUNTRY),我希望在datatable中显示。
答案 0 :(得分:0)
要获取该数据,您需要从mysqli中提取字段标题,例如
$columns = mysqli_fetch_fields($sql_sel);
$attributes = array();
foreach ($columns as $column)
{
$attributes[] = $column->name;
}
这会将列标题放入$attributes
数组中,您可以以对您的前端最有意义的方式添加该数据。