Dynamic column add with ajax call

Yilmazerhakan Source

Hi i have a datatable with ajax call.

I am adding columns with json data from server. But column count is dynamic and changes to query. And also datatables plugin does'nt have dynamic column property.

My solution is repeating dynamic column size with laravel blade php in javascript. Solution is working.

     var table = $('#neighbour_table').DataTable({
     data: result.data,
     iDisplayLength: 10,
     columns: [{
             data: "type"
         },
         {
             data: 'details',
             "render": function(data, type, row){
                if(type === 'display'){
                    data = '<a class="'+row['type']+'_neighbour label label-'+window.type[row['type']]+'" href="#" data-toggle="modal" data-target="#'+window.button_detail[row['type']]+'_details" data-tid="'+row['id']+'">'+data+'</a>';
                }
                return data;
            }
         },
         {
             data: 'left_neighbours'
         },
         {
             data: 'node'
         },
         {
             data: 'right_neighbours'
         },
         @php $i = 1 @endphp
         @for($left = $left_span; $left < 0; $left++) {
             data: 'word.{{$i++}}'
         },
         @endfor {
             data: 'word.{{$i++}}'
         },
         @for($right = 1; $right <= $right_span; $right++) {
             data: 'word.{{$i++}}'
         },
         @endfor
     ],
     order: [
         [1, 'desc']
     ],
     dom: 'Blfrtip',
     buttons: [{
             extend: "copy",
             className: "btn-sm"
         },
         {
             extend: "csv",
             className: "btn-sm"
         }
     ]
 });

But i want to make it without php I have window.left and window.left_span global variables but couldnt extend it.

I tried data of columns with external json object. But data render fields are confused with their parameters.

How i could increase or decrease JSON fields with javascript? Thanks.

phpjsondatatables

Answers

comments powered by Disqus