Reputation: 3092
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
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
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