jeudi 12 octobre 2017

DataTables with AJAX - How to pass in a column hyperlink an external variable

I'm beginner in DataTables or dev in general :)

I use Laravel 5.4 and several DataTables which get their data using ajax calls requests and everything it's working just fine :) . One of the tables have a column with a hyperlink on it I need to send further in the hyperlink an external variable which is not returned by Ajax response but it's hidden in same form with the table. So, I have the table definition:

$('#tabelClientiOferta').DataTable({
    lengthMenu: [[15, 25, 100, -1], [15,25, 100, "All"]],
    processing: true,
    serverSide: true,
    ajax: 'ajaxClienti',
      columns: [
        {data:'id',name:'id' , sClass: "hidden", "bSearchable": false },
        {data: 'denumire', name: 'denumire',
            "fnCreatedCell": function (nTd, sData, oData, iRow, iCol) {
                $(nTd).html("<a href='selectieFurnizor?idClient=" + oData.id + "'>" + oData.denumire + "</a>")
            }
        },
        { data: 'cui', name: 'cui' },
        { data: 'telefon', name: 'telefon', "bSearchable": false},
        { data: 'email', name: 'email', "bSearchable": false },
    ]
}); 

Controller function which respond to ajax call:

public function clienti(Request $request)
{
  return  Datatables::of(DB::table('clienti')->get(['id','denumire','cui','telefon','email']))->make(true);
} 

HTML template with table and hidden variable: @extends ('master')

@section('content')
    <div class="container">
        <div class="row">
            <div class="col-sm-12">
                <div class="tabelOferte" style ="width: 900px">
                    <table id = "tabelClientiOferta" class="table table-responsive table-striped table-hover">
                        <thead >
                        <tr style="font-weight: bold" >
                            <td>id</td>
                            <td>Denumire</td>
                            <td>CUI</td>
                            <td>Telefon</td>
                            <td>Email</td>
                        </tr>
                        </thead>
                    </table>
                </div>
                <input id = "hiddenId" name="hiddenId" type="text" value =  hidden />
            </div>
        </div>
    </div>
@stop

So I need to pass the hidden variable as the second parameter to the "denumire" column hyperlink, something like:

$(nTd).html("<a href='selectieFurnizor?idClient=" + oData.id + "&hiddenId="+$('#hiddenId') "'>" + oData.denumire + "</a>")

.

Is that possible? The solution which I use now is to return a view from the controller and include in it a static DataTable (with data already prepared and sent by the controller).

Thank you for your attention :)



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire