Pass param to ajax?
Pass param to ajax?
Hello!
Im pretty new to datatables and to laravel as well, but i managed to get it working but not the way i need still...
I use yajara laravel datatables plugin . I got my table displayed and at present if ii doubleclick on a row in shows me another view with all the data from another table. But i need to pass an ID of the row i clicked and pass this id to that view to display only relevant rows from other table..
By doubleclick a send a request like this: http://fttb/port?id=37
Route:===============
Route::controller('port', 'PortController', [
'anyData' => 'port.data',
'index' => 'port',
]);
in PortController: ===========================
public function index()
{
return view('ports.index');
}
public function anyData()
{
return Datatables::of(Port::select('*')->where('dev_id','=',37))->make(true);
}
And my view===================
@extends('master')
@section('content')
<table class="display" id="ports-table">
<thead>
<tr>
<th>Dev_id</th>
<th>Portnumber</th>
<th>Descriiption</th>
<th>Speed</th>
<th>Created At</th>
<th>Updated At</th>
</tr>
</thead>
</table>
@stop
@push('scripts')
<script>
$(function() {
var table = $('#ports-table').DataTable({
responsive: true,
processing: true,
serverSide: true,
ajax: '{!! route('port.data') !!}',
select: true,
columns: [
{ data: 'dev_id', name: 'dev_id' },
{ data: 'portnum', name: 'portnum' },
{ data: 'description', name: 'description' },
{ data: 'speed', name: 'speed' },
{ data: 'created_at', name: 'created_at', searchable: 'false' },
{ data: 'updated_at', name: 'updated_at', searchable: 'false' }
],
});
});
</script>
@endpush
Now by doubleclicking it shows me the row from my 'ports' table with 37th id but only because I asked for it directly from controller's function anyData(). I need to get this ID from http request and pass it somehow to controller ...
This question has an accepted answers - jump to answer
Answers
I think what you're looking for, is the
ajax.data, and if you need to change the data type (POST, PUT, GET, etc), thenajax.typeshould suffice (No direct reference docs for that though, oddly enough)Heres the example in the docs:
I thought about this option, but I don't know how to connect
with ajax.data option....e.g. where to put it
Two options:
ajax.url()and set the id as a GET parameterajax.dataas a function and have that function obtain the id to be sent (be it from a variable, a DOM element or wherever).Allan