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?

ArielisArielis Posts: 3Questions: 2Answers: 0
edited June 29 in Free community support

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

  • rf1234rf1234 Posts: 1,648Questions: 51Answers: 230

    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.

  • kthorngrenkthorngren Posts: 10,229Questions: 25Answers: 2,334

    I see there is no Editor back end for Python

    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

  • ArielisArielis Posts: 3Questions: 2Answers: 0

    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

    def delete_multiple_company(request, company_id):
        company = get_object_or_404(Company, pk=company_id)
        company = Company.objects.get(pk=company_id)
        company.objects.filter(to_delete=True).delete()    
        return HttpResponseRedirect(reverse("company:index_company"))
    

    urls.py

    url(r'^(?P<company_id>[0-9]+)/delete_multiple_company/$', views.delete_multiple_company, name='delete_multiple_company'),

    models.py

    class Company(models.Model):
        to_delete = models.BooleanField(default=False)
    

    index.html

            <a href="{% url 'company:delete_multiple_company' company.id %}" id="table" class="btn btn-default btn-sm active float: right" style="float: right;"><span class="fa fa-plus"></span>Delete Companies</a>
    
            <table id="dtBasicExample" class="table table-striped table-hover">
                 <thead>
                    <tr>
                       <th>Select</th>
                       <th>#</th>
                       <th>Checked ?</th> 
                    </tr>
                 </thead>
                    <tbody>
                       {% for company in companys.all %}
                    <tr>
                       <td id="{{ company.id }}"><input type="checkbox" class="companyCheckbox" name="checkedbox" id="{{ company.id }}" value="{{ company.id }}"></td>                              
                       <td>{{ company.id }}</td>
                       <td>{{ company.to_delete }}</td>
                   </tr>
                       {% endfor %}
    
                    </tbody>
            </table>
    
  • kthorngrenkthorngren Posts: 10,229Questions: 25Answers: 2,334

    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

Sign In or Register to comment.