dimanche 17 mai 2020

Yarja Datatables - getting results for specific id

I am trying to have the ability for the user to click on a link and get jobs for a specific operative. As a test I am only using three field from a job table

id,operative_id,address

The operative ID is the parameter I wish to pass to the datatable ajax controller.

The operative ID is sent by the jobs controller to a blade template in the normal manner. I then have a table area:

<table class="table data-table" id="thetable">
                       <thead>
                         <th>id</th>
                         <th>address</th>
                         <th>operative id</th>
                         <th></th>
                       </thead> 
                       <tbody>
                       </tbody>
                    </table>

In the javascript area of the blade template I have

<script type="text/javascript">

  $(function () {
  var table = $('.data-table').DataTable({
    "iDisplayLength": 25,
    "lengthMenu": [ [10, 25, 50,100,200, -1], [10, 25, 50,100,200, "All"] ],
        columnDefs: [
        {
            targets: -1,
            className: 'dt-right'
        }],
        processing: true,
        serverSide: true,
        ajax:"",
        columns: [
            { data: 'jobID', name: 'jobID' , orderable: false, searchable: false},
            {data: 'address', name: 'address'},
            {data: 'operative_id', name: 'operative' },
            {data: 'action', name: 'action', orderable: false, searchable: false},
        ],

    });
  });

</script>

In my controller I have this function:

public function testData (Request $request, $id)
          {
            $jb = DB::table('job')
              ->select('id AS jobID','operative_id','address')->where('operative_id',$id)->get();
              return Datatables::of($jb)
             ->addColumn('action', function($pubs){
                             $btn = '<div style="float:right">
                             <a href="#" class="btn btn-outline-secondary btn-xs" title="show details" style="margin-right:.5em;font-size:.75em"><i class="fas fa-book" ></i></a><a href="#" class="btn btn-outline-secondary btn-xs" title="show details" style="margin-right:.5em;font-size:.75em"><i class="fas fa-edit" ></i></a></div>';
                            return $btn;
                      })
              ->make(true);
          }

The controller (DatatablesController) has other functions that work perfectly and are much more complex, but I only want to show the jobs of the $id passed in. The error I am getting is a 500 error

Class App\Http\Controllers\DataTablesController does not exist

which of course it does.

For example this works perfectly:

public function allJobs(Request $request)
        {
          $user = Auth::user();
          $cl = $user->client_id;
          $jb = DB::table('job')
          ->join('job_status', 'job.jobStatus_id', '=', 'job_status.id')
          ->join('customers', 'job.customer_id', '=', 'customers.id')
          ->join('users', 'job.operative_id', 'users.id')
          ->where('job.client_id', $cl)
          ->select(['job.id as id', 'job_status.status as status', 'job.customer_id as customer_id', 'customers.customer as customer', 'users.name as operative','job.address as address','job.postcode as postcode','job.slug as slug','job_status.id as jobStatusID'])
          ->get();
            return Datatables::of($jb)
             ->addColumn('action', function($pubs){
                             $btn = '<div style="float:right">
                             <a href="/admin/viewJob/'.$pubs->id.'" class="btn btn-outline-secondary btn-xs" title="show" style="margin-right:.5em;font-size:.75em"><i class="fas fa-book" ></i></a><a href="/admin/jobEdit/'.$pubs->id.'" class="btn btn-outline-secondary btn-xs" title="edit" style="margin-right:.5em;font-size:.75em"><i class="fas fa-edit" ></i></a></div>';
                            return $btn;
                      })
              ->make(true);

        }

The route is defined as

Route::get('testData/{id}','DataTablesController@testData')->name('testData');

I just cannot see the way around it. Help is greatly appreciated!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire