[英]How to display array in a single column in table using DataTables?
我正以一種非正統的方式工作,因此陷入了混亂。 我正在使用DataTables進行搜索和分頁。 我有一張桌子,可以在其中獲取供應商的詳細信息,例如姓名,電子郵件,地址,國家/地區以及供應商處理的材料。 我正在使用兩個表的內部聯接獲得這些結果。 在我的情況下,供應商可以處理多種材料,如屏幕截圖所示。
這是我的表格視圖的屏幕截圖:
現在的問題是,我想要每個td中每個供應商的材料,但是在我的情況下,每個td中都顯示了每種材料,這顯然破壞了我的表格設計。
這是我的HTML和JavaScript:
<table id="employee-grid" class="display" width="100%">
<thead>
<tr>
<th>Email</th>
<th>Country</th>
<th>Region</th>
<th>Material</th>
<th></th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
</table>
<script type="text/javascript" language="javascript" src="js/jquery.js"</script>
<script type="text/javascript" language="javascript" src="js/jquery.dataTables.js"></script>
<script type="text/javascript" language="javascript" >
$(document).ready(function() {
var dataTable = $('#employee-grid').DataTable( {
"processing": true,
"serverSide": true,
"ajax":{
url :"employee-grid-data.php",
}
} );
} );
</script>
這是我的ajax文件:
<?php
/* Database connection start */
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "phwdata";
$conn = mysqli_connect($servername, $username, $password, $dbname) or die("Connection failed: " . mysqli_connect_error());
/* Database connection end */
// storing request (ie, get/post) global array to a variable
$requestData= $_REQUEST;
//print_r($_REQUEST);
$columns = array(
// datatable column index => database column name
0 =>'name',
1 => 'email',
2=> 'country',
3=> 'region',
4=> 'material'
);
// getting total number records without any search
$sql = "SELECT *";
$sql.=" FROM bp";
$sql.=" INNER JOIN pdbp";
$sql.=" ON (bp.id=pdbp.bp_id)";
$query=mysqli_query($conn, $sql) ;
$totalData = mysqli_num_rows($query);
$totalFiltered = $totalData; // when there is no search parameter then total number rows = total number filtered rows.
$query=mysqli_query($conn, $sql) ;
$totalFiltered = mysqli_num_rows($query); // when there is a search parameter then we have to modify total number filtered rows as per search result.
$sql.=" ORDER BY ". $columns[$requestData['order'][0]['column']]." ".$requestData['order'][0]['dir']." LIMIT ".$requestData['start']." ,".$requestData['length']." ";
/* $requestData['order'][0]['column'] contains colmun index, $requestData['order'][0]['dir'] contains order such as asc/desc */
$query=mysqli_query($conn, $sql) or die("employee-grid-data.php: get employees");
$data = array();
while( $row=mysqli_fetch_array($query) ) { // preparing an array
$nestedData=array();
$nestedData[] = $row["name"];
$nestedData[] = $row["email"];
$nestedData[] = $row["country"];
$nestedData[] = $row["region"];
$mat = $row['material_cod'];
$material=explode(",",$mat);
foreach($material as $materials){
$sql2 = "SELECT material";
$sql2.=" FROM product_material";
$sql2.=" WHERE material_cod = '$material'";
$query2=mysqli_query($conn, $sql2);
while( $row=mysqli_fetch_array($query2)) {
$nestedData[] = $row['material'];
}
}
$data[] = $nestedData;
}
我還在DataTables嵌套對象中瀏覽了嵌套對象 ,但無法找出解決方案。
最終我得到了解決方案,我在foreach循環中聲明了數組。 在循環外聲明數組可解決我的問題。 :)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.