【问题标题】:How to display array in a single column in table using DataTables?如何使用 DataTables 在表格的单列中显示数组?
【发布时间】:2018-02-08 06:40:52
【问题描述】:

我的工作一团糟,因为我正在以一种非正统的方式工作。我正在使用 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 nested object 中的嵌套对象,但我无法找出解决方案。

【问题讨论】:

  • 你能用json_encode显示数组结果吗?

标签: javascript php arrays ajax datatables-1.10


【解决方案1】:

最后我得到了解决方案,我在 foreach 循环中声明了数组。在循环外声明数组解决了我的问题。 :)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    • 2021-08-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-22
    • 1970-01-01
    • 2019-09-04
    相关资源
    最近更新 更多