Search code examples
jquerylaraveleloquentdatatablesyajra-datatable

Sorting of datatable not working in laravel


I have a table where I am able to get data from database. Here I am using two different tables to match the record and accordingly get the values if both table data gets matched. The values in database are ", " separated which I am able to split using explode function. Now the problem is that every element in database starts with "," and it returns first row as blank. Now when I am using Datatable to show the data, sorting is not working here tried too many solutions from different blogs but nothing working for me. I guess because it returns first row as blank sorting is not working.

Here is my view:

 <table id="tablevar" class="table table-sm">
      <thead>
        <tr>
          <th>
            Top Correlators
          </th>
          <th>
            Positive/Negative Correlation
          </th>
        </tr>
      </thead>
      @foreach(explode(', ', $detail->Topcorrelators) as $row)

          <tr>
              <td>
                {{ $row }}
              </td>
              @foreach($Correlations_list as $corr)
                @if($corr->Variable == $row )
                  <td>
                     @if($corr->Corr > 0)
                    <div class="progress">
                        <div class="progress-bar large progress-bar-striped" role="progressbar" style="width: 100%;background-color:#e9ecef;" aria-valuenow="10" aria-valuemin="0" aria-valuemax="100"></div> 
                        <div class="progress-bar progress-bar-striped" role="progressbar" style="width: 100%;background-color:#CA0088 " aria-valuenow="15" aria-valuemin="0" aria-valuemax="100"></div>
  
                        </div>
                        Positive Correlator( {{ $corr->Corr }} )
                    @else
                    <div class="progress">
                        <div class="progress-bar large progress-bar-striped" role="progressbar" style="width: 100%;background-color:#EAB330;" aria-valuenow="10" aria-valuemin="0" aria-valuemax="100"></div> 
                        <div class="progress-bar large progress-bar-striped" role="progressbar" style="width: 100%;background-color:#e9ecef;" aria-valuenow="10" aria-valuemin="0" aria-valuemax="100"></div> 
                        </div>
                        Negative Correlator( {{ $corr->Corr }} )
                    @endif
                  </td> 

                  @endif
              @endforeach
          </tr>
      @endforeach
    </table>   

and this is my controller code:

 $Correlations_list=Correlation::all();
            return view('admin.members.Detailspage',compact('detail','Correlations_list'));

and this is my script:

$('#tablevar').DataTable({
      "paging": true,
      "lengthChange": false,
      "searching": false,
      "ordering": true,
      "info": true,
      "autoWidth": false,
      "responsive": true,
    });

Any help ?


Solution

  • You can check for empty value in that case

    @foreach(explode(', ', $detail->Topcorrelators) as $row)
      @if(is_empty($row))
       @continue;
      @endif
      // ... rest of your code
    @endforeach;
    

    That should skip that empty row and fix your issue. You will find more info here: https://laravel.com/docs/5.4/blade#loops