数据表foreach细节/子行

时间:2016-02-02 03:15:21

标签: javascript jquery ajax datatables

我想显示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"
},

我想在链接here之后为product & 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知道它的变量时,它显示实际值
(说我3 product数组3也显示for 但不知何故,当它进入$loop部分时,$i<=$loop变为0 因为不显示任何行(如果我将条件设置为$loop = "'+d.product_.length+'" . "'+d.product_.length+'",那么每行父母中显示一行详细信息)

我发现了一些奇怪的东西 echo $loop;
33 ==&gt; 3 (例如,如果产品数组计数为sum

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

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

1 个答案:

答案 0 :(得分:5)

你真的不需要php在这里添加一个循环附加表,而是可以使用jquery&#39; s $.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变量。

<强> https://www.google.com/design/spec/material-design/introduction.html