【发布时间】:2016-04-10 08:58:44
【问题描述】:
我有一个这样的结果表(左表)
我希望它变得像(右表)
在我之前的问题here,
@Clayton 已经在 php laravel 中提供了答案(在前端分组)
$data = [];
foreach ($result as $row) {
if (!isset($data[ $row['nama_customer'] ])) {
$data[ $row['nama_customer'] ] = [];
}
$data[ $row['nama_customer'] ][] = $row;
}
var_dump($data);
但由于某种原因,现在我需要在 ajax 中制作它
我已经尝试过这样的事情
$.post('myurl', {id:id} , function(result) {
var data = []; var idcustomer;
for (x in result) {
idcustomer = result[x]['id_customer'];
if(typeof( data[idcustomer] ) != "undefined" && data[idcustomer] !== null)
data[idCustomer] = [];
data[idCustomer][] = data[x];
}
console.log(data);
});
我想我已经用该代码解决了 isset 函数
但是当我找到二维数组时我会感到困惑
谁能告诉我如何在ajax中做到这一点
@Solved
function groupedTable() {
var seen = []; var a = 0;
$('#mytable td:first-child').each(function()
{
var $this = $(this);
var index = $this.index();
var txt = $this.text();
if (seen[index] === txt)
{
$($this.parent().prev().children()[index]).attr('rowspan', a);
$this.hide();
}
else {
seen[index] = txt;
a = countLoop(txt, $this);
}
});
}
function countLoop(txt) {
var span = 0;
$('#mytable td:first-child').each(function()
{
$this = $(this);
var txt1 = $this.text();
if (txt1 == txt)
span++;
});
return span;
}
【问题讨论】:
-
如果您已经在后端解决了它,为什么不从后端本身以 json 格式发送所需的格式,例如
{customer1: {[item1: 3], [item2: 3] ..}, customer2: {[..]}} -
@RaunakKathuria 对不起,我的解释不清楚...它不在后端,但我在前端将其分组,但使用 php 方式显示数据
-
你能澄清一件事,你所拥有的结果表是来自 ajax 的 html 响应,还是你得到的结果是 json?
-
@RaunakKathuria 嗯..因为我没有在
$.post中设置指定响应,我认为$.post默认响应是xml...如果我错了请纠正我
标签: javascript php mysql ajax