gpt4 book ai didi

javascript - 在ajax中分组结果查询

转载 作者:行者123 更新时间:2023-11-29 01:37:56 24 4
gpt4 key购买 nike

我有一个这样的结果表(左表)

enter image description here enter image description here

我想让它变成像(右表)

在我之前的问题中 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 中做到这一点


@已解决

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;
}

最佳答案

<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>

关于javascript - 在ajax中分组结果查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34630258/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com