I have a table, using the DataTables API, that is populated with a list of entries from a database. I would like my user to select a entry from the table, click a delete button, and the selected entry will be deleted from the database. I'm using Laravel. So, my initial instinct is to send the selected entry in a post request where I will process the selected entry via a Controller.
1) How can I forward my selected entry (var rowData) to my Laravel Controller via a post request once the user clicks the delete button?
2) Is there a better solution?
[HTML]
<div class="table-responsive">
<table class="table table-bordered" width="100%" id="someTable"></table>
</div>
<form action="/somePage/deleteObject" method="post">
<input type="hidden" value="delete">
<button id="delBtn" type="submit" class="btn" >
Delete
</button>
</form>
[Javascript] - Populates table and provides events for when an entry is selected.
<script type="text/javascript">
$(document).ready(function(){
var table = $('#someTable').DataTable( {
select: {
style: 'single'
},
"scrollX": true,
"scrollY": '70vh',
"scrollCollapse": true,
"paging": false,
dom: 'ft',
"aoColumns": [
{ "sTitle": "Name", "mData": "name" },
{ "sTitle": "Email", "mData": "email" },
{ "sTitle": "Group", "mData": "group" },
{ "sTitle": "Access Level", "mData": "level" }
],
"aaData": {!! collect($users)->toJson() !!}
} );
// Event handling: Pulled directly from DataTables example
table
.on( 'select', function ( e, dt, type, indexes ) {
document.getElementById("delBtn").disabled = false;
var rowData = table.rows( indexes ).data().toArray();
} )
.on( 'deselect', function ( e, dt, type, indexes ) {
document.getElementById("delBtn").disabled = true;
var rowData = null;
} );
} );
</script>
[Laravel Route]
Route::post('/somePage/deleteObject', 'Controller_Page@deleteObject');
[Laravel Controller]
public function deleteObject(){
$value = Request::get('rowData');
dd("Reached Delete", $value);
}
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire