El Hombre Sin Nombre
El Hombre Sin Nombre

Reputation: 3092

Calling id in Laravel Datatable

I use a Laravel Datatable in Laravel to create a Datatable with filters.I need too a custom column so I add this code:

 ->addColumn('estado', function ($previsitas) {
            return $previsitas->f_salida ?  '<a id="delete" class="text-danger" data-toggle="tooltip" data-placement="top" title="Cambiar estado" href="' . route('visitas.estado', [$previsitas->id]) . '"><strong>Salida</strong></a>' :'<a class="text-success" data-toggle="tooltip" data-placement="top" title="Cambiar estado" href="' . route('visitas.estado', [$previsitas->id]) . '"><strong>Entrada</strong></a>';
        })->rawColumns(['estado'])

This code works perfectly. I need too call this column to prompt a message with sweetalert. I use this...

$('#delete').click(function () {
    swal('Test');
});

When i click in the column nothing happens, but when i create directly element with a id='delete' in the blade works.

So the error is simple: I can´t call a element with id in the datatable column, but i can call the element if directly i use in the blade. Why this error is happening ?

Upvotes: 2

Views: 198

Answers (2)

Tim Lewis
Tim Lewis

Reputation: 29258

I think you need to use jQuery event delegation for this. Currently, calling $("#delete").click(...) assigns a click event to the DOM element $("#delete"), which at the time of assignment, doesn't exist. If you assign the click event to "body" with a target of "#delete", this should work for you:

$("body").on("click", "#delete", function () {
    swal("Test");
});

Upvotes: 2

Priya Bhojani
Priya Bhojani

Reputation: 130

This code

$('#delete').click(function () {
    swal('Test');
});

might be executing before

->addColumn('estado', function ($previsitas) {
            return $previsitas->f_salida ?  '<a id="delete" class="text-danger" data-toggle="tooltip" data-placement="top" title="Cambiar estado" href="' . route('visitas.estado', [$previsitas->id]) . '"><strong>Salida</strong></a>' :'<a class="text-success" data-toggle="tooltip" data-placement="top" title="Cambiar estado" href="' . route('visitas.estado', [$previsitas->id]) . '"><strong>Entrada</strong></a>';
        })->rawColumns(['estado'])

is added to the DOM

Upvotes: 0

Related Questions