How to delete severals row from the datatable and django models at the same time?
How to delete severals row from the datatable and django models at the same time?
Hi,
I am trying to delete severals rows at the same time from the datatable and django at the same time.
Any idea, why this code doesn`t work?
Here is my code:
```<script>
$(document).ready(function() {
var table = $('#dtBasicExample').DataTable();
var rows = table
.rows( '.selected' )
.remove()
.draw();
$('#dtBasicExample tbody').on( 'click', 'tr', function () {
$(this).toggleClass('selected');
} );
$('#button').click( function () {
alert( table.rows('.selected').data().length +' row(s) selected' );
} );
} );
$(document).ready(function(){
$('#dtBasicExample').DataTable({
}
var table = $('#dtBasicExample').DataTable();
$('#dtBasicExample tbody').on( 'click', 'tr', function () {
$(this).toggleClass('selected');
} );
$('#button').click( function () {
alert( table.rows('.selected').data().length +' row(s) selected' );
} );
});
</script>```
Answers
rows().remove() removes them from the data table. That's it.
If you are not using Editor you would need to write the ajax call to delete those rows from the server yourself.
Editor will do all CRUD operations for you. Otherwise: Happy coding - it all by yourself.
Personally I have never used Data Tables without Editor and its server side components: If you are using this for a business application with a real database server side you can do this without Editor - but only if your hourly rate is less than $5 an hour. Otherwise: Buy a single developer Editor license. (No, I am not affiliated with Spry Media etc. ...).
I see there is no Editor back end for Python ... If I recall it correctly @kthorngren developed something for it?! Not sure.
I've not written anything specific for Django. I've written some server side editor Python code for my projects. IF you wish to write your won you will need to follow the protocol described here:
https://editor.datatables.net/manual/server
Or you can use a third party tool like this:
https://pypi.org/project/djangorestframework-datatables-editor/
Haven't used it myself so not sure how well it works.
But as rf1234 says you can create your own Ajax request, with the rows you want to delete, to send to your Django script.
Kevin
Thank you @rf1234 and @kthorngren. I am not using Editor yet, I ll find out if there is a tutorial for Django. I am learning Django at the moment, here it`s what I have done:
The way how I am doing it is to create a boolean to_delete in my model, when the checkbox is selected, I am calling the function delete_multiple_company in my view. However, this doesn`t work.
I`ve created my view:
I am trying to delete severals rows at the same time in django. I am using datatables. The way how I am doing it is to create a boolean to_delete in my model, when the checkbox is selected, I am calling the function delete_multiple_company in my view. However, this doesn`t work. Any idea, what I am doing wrong please. Many Thanks,
I`ve created my view:
views.py
urls.py
url(r'^(?P<company_id>[0-9]+)/delete_multiple_company/$', views.delete_multiple_company, name='delete_multiple_company'),
models.py
index.html
What exactly is not working? Its hard to say by looking at code snippets. Start by using the browser's Network Inspector tool to see what is being sent from the browser when the checkbox is selected. Just follow and debug the path for the request until you find where its breaking.
Kevin