Neversaysblack Neversaysblack - 4 years ago 146
PHP Question

Grouping result query in ajax

I have a result table like this (left table)

enter image description here
enter image description here

I want it to become like (right table)

In my previous question here,

@Clayton has already provide answer how to do it in php laravel (grouped in front end)


$data = [];
foreach ($result as $row) {
if (!isset($data[ $row['nama_customer'] ])) {
$data[ $row['nama_customer'] ] = [];
}
$data[ $row['nama_customer'] ][] = $row;
}
var_dump($data);


but for some reason now I need to make it in ajax

I already try something like this

$.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);

});


I think I already solved isset function with that code

but when I find 2 dimensional array I get confused


can someone give me an idea of how to do it in ajax

Answer Source
<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>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download