0

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 = {{$someId}} 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 :)

4
  • 1
    Maybe. At least you should use $('#hiddenId').val(). $('#hiddenId') will not do it ;-) Commented Oct 13, 2017 at 0:15
  • Yes of course, I was on the rush when I post it, but this will not resolve my problem ..... Thank you Commented Oct 13, 2017 at 7:35
  • 1
    Assuming the hidden variable exists before the datatable is populated, and assuming you use the correct js code to get the value of #hiddenId, then what you have done should work. Commented Oct 13, 2017 at 9:37
  • Dear friends thank you for your time and attention! I don't know what I did wrong (or good) but now it's working :) Commented Oct 13, 2017 at 17:03

1 Answer 1

2

Server-side: use add coloumn from controller

$data = DB::table('clienti')->get(['id','denumire','cui','telefon','email']);

return Datatables::of($data)
->addColumn('clear', function ($data) {
return '<a href="#clear-' . $data->id . '"  onclick="clear(' . $data->id . ',$(this))"  class="btn btn-xs btn-danger"><i class="glyphicon glyphicon-trash"></i> Clear</a>';
                })
                ->escapeColumns([])
                ->make(true);

And add to columns with initial js of datatables

{data: 'clear', name: 'cleat', orderable: false, searchable: false }

or use js based columns render() function, official doc and examples here: https://datatables.net/reference/option/columns.render

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.