【问题标题】:Datatable foreach detail/child row数据表 foreach 详细信息/子行
【发布时间】:2016-02-02 03:15:20
【问题描述】:

我想显示foreach子行数据表

假设我有这样的 ajax 数据

"data": [
{
  "date"      : "1/17/2016",
  "supplier"  : "supplier1",
  "total"     : "$10",
  "payment"   : "Cash",
  "product" : Array[2] 
     0: "product1"
     1: "product2"
  "price" : Array[2]
     0: "$5"
     1: "$5"
},
{
  "date"      : "2/1/2016",
  "supplier"  : "supplier2",
  "total"     : "$3",
  "payment"   : "Cash",
  "product" : Array[1] 
     0: "product1"
  "price" : Array[1]
     0: "$3"
},
{
  "date"      : "1/17/2016",
  "supplier"  : "supplier3",
  "total"     : "$15",
  "payment"   : "Cash",
  "product" : Array[3] 
     0: "product1"
     1: "product2"
     2: "product3"
  "price" : Array[2]
     0: "$3"
     1: "$5"
     2: "$7"
},

我想通过链接hereproduct & price 数组创建数据表子行

我只在function format 中编辑脚本以满足我的需求

function format ( d ) {
    // `d` is the original data object for the row
    return '<table class="table table-border table-hover">'+
      '<tr>'+
         '<td>Product</td>'+
         '<td>Price</td>'+
      '</tr>' +

      '<?php 
          $loop = 5; 
          echo $loop;             <-- here

          for ($i=0; $i<$loop; $i++) {                      
             echo "<tr><td>'+d.product['$i']+'</td> <td>'+d.price['$i']+'</tr>"; 
       } ?>' +

    '</table>';
}

它运行得很好...我可以显示我想要的数据..但我必须手动定义$loop...

我尝试使用$loop = "'+d.product.length+'"
当我 echo php 中的那个 var
它显示真实值
(假设我在product 中有3 数组,它也显示3

但不知何故,当它进入 for 部分时,就像 $loop 变成 0
因为不显示任何行(如果我将条件设置为 $i&lt;=$loop 它会在每一行父级中显示一行详细信息)

我也发现了一些奇怪的东西
$loop = "'+d.product_.length+'" . "'+d.product_.length+'";
echo $loop ==> 33 (假设产品数组计数是3

但如果我将其更改为sum,则结果为0
$loop = "'+d.product_.length+'" + "'+d.product_.length+'";
echo $loop ==> 0 (如果产品数组计数也是3 )

如何解决它,这样我就可以知道我的脚本应该循环多少次

【问题讨论】:

  • @GuruprasadRao 不知道如何在演示中添加我的 ajax 数据源...但我想是这样的包括在内?
  • @GuruprasadRao 我的问题基本上是设置 var $loop 自动...但我猜 php 在设置 $loopjavascript var 时会搞砸......我猜我采取错误的方法
  • @GuruprasadRao 你能举个例子吗,不太明白..我还在学习 ajax 和 jquery :D..我也会将我的真实数据添加到我的帖子中......等一下跨度>
  • @GuruprasadRao 我只需要在console.log 中复制返回的 json 即可。如果没有...你能给我链接教程如何获得正确的 json 数据吗?

标签: javascript jquery ajax datatables


【解决方案1】:

您实际上并不需要php 来在您的额外表格中附加一个循环,而是可以使用jquery$.each.. 您只需要在append 之前构造您的表格主体结构如下:

/* Formatting function for row details - modify as you need */
function format ( d ) {
    console.log(d.product);
    var trs=''; //just a variable to construct
    $.each($(d.product),function(key,value){
        trs+='<tr><td>'+value+'</td><td>'+d.price[key]+'</td></tr>';
        //loop through each product and append it to trs and am hoping that number of price 
        //values in array will be equal to number of products
    })
    // `d` is the original data object for the row
    return '<table class="table table-border table-hover">'+
           '<thead>'+
              '<th>Product</th>'+
                '<th>Price</th>'+  
           '</thead><tbody>' 
               + trs +
        '</tbody></table>';
}

$(document).ready(function() {
    var table = $('#example').DataTable({
        "ajax": 'https://raw.githubusercontent.com/kshkrao3/JsonFileSample/master/Employees.json',
        "columns": [
            {
                "class":          'details-control',
                "orderable":      false,
                "data":           null,
                "defaultContent": ''
            },
            { "data": "date"},
            { "data": "supplier"},
            { "data": "total"},
            { "data": "payment"}
        ]
    });

    // Add event listener for opening and closing details
   $('#example tbody').on('click', 'td.details-control', function () {
        var tr = $(this).closest('tr');
        var row = table.row( tr );

        if ( row.child.isShown() ) {
          // This row is already open - close it
          row.child.hide();
          tr.removeClass('shown');
        }
        else {
          // Open this row
          row.child( format(row.data()) ).show();
          tr.addClass('shown');
        }
    });
});

注意:您的 click event 中有一个错误。您正在尝试 dTable.row,因为它必须是 table.row,因为您在 table 变量中持有引用。

DEMO

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-16
    • 2020-12-28
    • 1970-01-01
    相关资源
    最近更新 更多