我有一个像这样的结果表(左表)
我希望它变得像(右表)
在我之前的问题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;
}
答案 0 :(得分:2)
<script>
$(document).ready(function() {
function MergeCommonRows(table, columnIndexToMerge) {
previous = null;
cellToExtend = null;
table.find("td:nth-child(" + columnIndexToMerge + ")").each(function() {
jthis = $(this);
content = jthis.text() if (previous == content) {
jthis.remove();
if (cellToExtend.attr("rowspan") == undefined) {
cellToExtend.attr("rowspan", 2);
} else {
currentrowspan = parseInt(cellToExtend.attr("rowspan"));
cellToExtend.attr("rowspan", currentrowspan + 1);
}
} else {
previous = content;
cellToExtend = jthis;
}
});
};
MergeCommonRows($("#aTable"), 1);
}
</script>
答案 1 :(得分:2)
我们可以通过jquery来实现。参考下面的url并在body标签内调用MergeCommonRows函数它将起作用
function MergeCommonRows(table, columnIndexToMerge){ previous = null; cellToExtend = null; table.find("td:nth-child("+columnIndexToMerge+")").each(function(){ jthis = $(this); content = jthis.text() if(previous == content){ jthis.remove(); if(cellToExtend.attr("rowspan") == undefined){ cellToExtend.attr("rowspan", 2); } else{ currentrowspan = parseInt(cellToExtend.attr("rowspan")); cellToExtend.attr("rowspan", currentrowspan+1); } } else{ previous = content; cellToExtend = jthis; } }); };