How to add delete button
How to add delete button
Shoddy Smoke
Posts: 4Questions: 2Answers: 0
Hi all, I have a datatable that connects to my database Models.py file. It connects to everything well but know I want to add a delete button so that users can delete their appointments. I have done some research and tried implementing a few things but it so far haven’t got any luck. Do you guys have any idea how to do it?
$(document).ready(function() {
$('#data').DataTable( {
"ajax": '/api/data',
columns: [
{data: 'first_name'},
{data: 'second_name'},
{data: 'date', searchable: true},
{data: 'slot_time', searchable: true},
{data: 'Description'}
],
} );
} )
Answers
This example show one way to add buttons to the rows:
http://live.datatables.net/xijecupo/1/edit
You can create a delete button event handler that uses jQuery ajax to send the row ID, for example, to the Python script to delete. The Python script should then return the status and in the Ajax
success
function userow().remove()
to remove the row in the client or if an error occurs handle it appropriately. Another option is to useajax.reload()
to refresh the table instead ofrow().remove()
.Kevin
I don't really understand how to do it from the python script side of things. Are there any examples? Thanks
No, there are no Python examples. The Python side is out of scope for this forum but if you ask Python questions I'll try to answer. Are you using a framework like Django or Flask? There should be examples of how to process the ajax request. Maybe Stack Overflow will have solutions you can try.
Basically you will need to use your Python DB library to delete the row then return either a success or failure message to the client.
Kevin
I am using Flask. Thanks
See this example https://github.com/akshatchaurasiya/CRUD-operation-in-php for add, delete and update button in your data table in PHP language.