簡體   English   中英

在ajax中分組結果查詢

[英]Grouping result query in ajax

我有一個這樣的結果表(左表)

在此處輸入圖片說明 在此處輸入圖片說明

我希望它變得像(右表)

在我以前的問題在這里

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

我們可以通過jquery實現。 請參考以下網址並在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; } }); };

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM