我希望能够显示数据表中每个子行的内容
假设我的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"
},
我想创建一个数据表格子行,用于显示“产品和价格”数组。请参考此链接。
我只需要编辑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+'"
当我在php中echo
该变量时
它会显示实际值
(假设我在product
中有3
个数组,它也会显示3
)
但不知何故,当它进入for
部分时,就好像$loop
变成了0
因为没有显示任何行(如果我将条件设置为$i<=$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
)
如何解决它,以便我可以知道我的脚本应该循环多少次
$loop
... 但我猜想当使用javascript var
设置$loop
时,PHP 会出现问题... 我想我可能采取了错误的方法。 - Surya Matadewaconsole.log
返回的 JSON,对吗?如果不是,你能给我一个链接教程,告诉我如何获取正确的 JSON 数据吗? - Surya Matadewa