1

I am using Nested-datatables (https://github.com/AndrejGajdos/nested-datatables) so I have to use JSON. I want to pass JSON from the controller to ajax and then use it in order to create a nested table. The problem is that I need the exact JSON format so I will not get "undefined" result. This is the example of the JSON format that I need (like in the link above) :

      var dataInJson = [
        {
          data: {
            name: 'b1',
            street: 's1',
            city: 'c1',
            departments: 10,
            offices: 15
          },
          kids: [
            {
              data: {
                department: 'HR',
                supervisor: 'Isidor Bristol',
                floor: 1,
                employees: 15
              },
              kids: [
                {
                  data: {
                    name: 'Klement Nikodemos',
                    phone: '+938462',
                    hire_date: 'January 1, 2010',
                    id: 3456
                  },
                  kids: []
                },
                {
                  data: {
                    name: 'Madhava Helmuth',
                    phone: '+348902',
                    hire_date: 'May 23, 2002',
                    id: 1234
                  },
                  kids: []
                },
                {
                  data: {
                    name: 'Andria Jesse',
                    phone: '456123',
                    hire_date: 'October 23, 2011',
                    id: 9821
                  },
                  kids: []
                }
              ]
            },
            {
              data: {
                department: 'development',
                supervisor: 'Jim Linwood',
                floor: 2,
                employees: 18
              },
              kids: [
                {
                  data: {
                    name: 'Origenes Maxwell',
                    phone: '345892',
                    hire_date: 'February 1, 2004',
                    id: 6234
                  },
                  kids: []
                }
              ]
            },
            {
              data: {
                department: 'testing',
                supervisor: 'Zekeriya Seok',
                floor: 4,
                employees: 11
              },
              kids: []
            }
          ]
        },
        {
          data: {
            name: 'b2',
            street: 's10',
            city: 'c2',
            departments: 3,
            offices: 10
          },
          kids: [
            {
              data: {
                department: 'development',
                supervisor: 'Gallagher Howie',
                floor: 8,
                employees: 24
              },
              kids: [
                {
                  data: {
                    name: 'Wat Dakota'
                  },
                  kids: []
                }
              ]
            },
            {
              data: {
                department: 'testing',
                supervisor: 'Shirley Gayle',
                floor: 4,
                employees: 11
              },
              kids: []
            }
          ]
        },
        {
          data: {
            name: 'b3',
            street: 's3',
            city: 'c3',
            departments: 2,
            offices: 1
          },
          kids: [
            {
              data: {
                department: 'development'
              },
              kids: [
                {
                  data: {
                    name: 'Wat Dakota'
                  },
                  kids: []
                }
              ]
            },
            {}
          ]
        },

        {
          data: {
            name: 'b4',
            city: 'c4'
          },
          kids: []
        }
      ];

In controller I have ajax function:

    public function ajax(){
        $parent = FirstLine::
        select('yehida','data_type')
        ->where('first_year',2019)->where('second_year',2019)->where('first_period_no',1)->where('second_period_no',2)->where('periodical','רבעוני')
        ->addSelect(DB::raw('SUM(first_period) as first_period'))
        ->groupBy('yehida')
        ->groupBy('data_type')
        ->orderBy('yehida')
        ->orderBy('data_type')
        ->get();


        $child = FirstLine::
        select('yehida','hativa','data_type')
        ->where('first_year',2019)->where('second_year',2019)->where('first_period_no',1)->where('second_period_no',2)->where('periodical','רבעוני')
        ->addSelect(DB::raw('SUM(first_period) as first_period'))
        ->groupBy('yehida')
        ->groupBy('hativa')
        ->groupBy('data_type')
        ->orderBy('yehida')
        ->orderBy('hativa')
        ->orderBy('data_type')
        ->get();


        $value = [];
        foreach ($parent as $ch) {
            $options = [];
            foreach($child as $ch2) {
                $options[] = ['data' => [
                    'yehida' => $ch2->yehida,
                    'hativa' => $ch2->hativa,
                    'data_type' => $ch2->data_type,
                    'first_period' => $ch2->first_period],
                    
                    'kids' => []
                ];

                if($ch->yehida == $ch2->yehida){
                    $value[] = [
                        'data' =>[
                            'yehida' => $ch->yehida,
                            'data_type' => $ch->data_type,
                            'first_period' => $ch->first_period
                        ],
                        'kids' =>
                            $options,
                        
                    ];
                }
            }

        }

        
        $value = json_encode($value);
        return response()->json($value);
    }

My script with ajax call:

<script>

        var temp;

        $.ajax({
        'async': false,
        type: 'GET', //THIS NEEDS TO BE GET
        url: '{!! route('get.ajax') !!}', 
        dataType: 'json',
        success: function (data) {
                temp = data;
            },error:function(){ 
                console.log(data);
            }
        });

        var dataInJson = temp;

        var settings = {
          iDisplayLength: 20,
          bLengthChange: false,
          bFilter: false,
          bSort: false,
          bInfo: false
        };
  
        var tableT = new nestedTables.TableHierarchy(
          'example',
          dataInJson,
          settings
        );
        tableT.initializeTableHierarchy();

        var tableEle = document.querySelector('#example .table');
        tableEle.addEventListener('onShowChildHierarchy', function(e) {
        console.log(e);
        });

        // '#example' is wrapper ID for table hierarchy
        var tableEle = document.querySelector('#example .table');
        tableEle.addEventListener('onHideChildHierarchy', function(e) {
        console.log(e);
        });

      </script>

How am I supposed to build my own JSON format so it will be like the JSON example? please help me

m.m
  • 41
  • 1
  • 5
  • take a look at [Eloquent: API Resources](https://laravel.com/docs/5.8/eloquent-resources) – Alan Sep 12 '19 at 13:13

0 Answers0