Pass param to ajax?

Pass param to ajax?

lol_userlol_user Posts: 10Questions: 4Answers: 0

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

  • jLinuxjLinux Posts: 981Questions: 73Answers: 75
    edited October 2015

    I think what you're looking for, is the ajax.data, and if you need to change the data type (POST, PUT, GET, etc), then ajax.type should suffice (No direct reference docs for that though, oddly enough)

    Heres the example in the docs:

    $('#example').dataTable( {
        "ajax": {
            "url": "data.json",
            "type": "POST",
            "data": {
                "user_id": 451
            }
        }
    } );
    
  • lol_userlol_user Posts: 10Questions: 4Answers: 0

    I thought about this option, but I don't know how to connect

    ajax: '{!! route('port.data') !!}', 
    

    with ajax.data option....e.g. where to put it

  • allanallan Posts: 63,356Questions: 1Answers: 10,444 Site admin
    Answer ✓

    Two options:

    1. Use ajax.url() and set the id as a GET parameter
    2. Use ajax.data as a function and have that function obtain the id to be sent (be it from a variable, a DOM element or wherever).

    Allan

This discussion has been closed.