user3041764
user3041764

Reputation: 849

Django - datatable reload with Ajax

I'm looking for properly solution to reload datatable in Django with Ajax.

At this moment it works pretty good with one exception. Table are auto paged by bootstrap. When Ajax reload table it shows all results without splitting on the pages.

orders_list.html:

<div id="DataTables_Table_0_wrapper" class="dataTables_wrapper no-footer">
        <div class="datatable-scroll">
        <table class="table datatable-basic dataTable table-striped no-footer" id="DataTables_Table_0" role="grid" aria-describedby="DataTables_Table_0_info">
            <thead>
                <tr role="row">
                    <th class="sorting_asc" tabindex="0" aria-controls="DataTables_Table_0" rowspan="1" colspan="1" aria-label="" aria-sort="ascending">Nr.</th>
                    <th class="sorting" tabindex="0" aria-controls="DataTables_Table_0" rowspan="1" colspan="1" aria-label="">Data</th>
                    <th class="sorting" tabindex="0" aria-controls="DataTables_Table_0" rowspan="1" colspan="1" aria-label="">Przewoźnik</th>
                    <th class="sorting" tabindex="0" aria-controls="DataTables_Table_0" rowspan="1" colspan="1" aria-label="">Status</th>
                    <th class="text-center sorting_disabled" rowspan="1" colspan="1" aria-label="Akcje" style="width: 100px;">Akcje</th>
                </tr>
            </thead>
            <tbody id="orders_table">
                {% include "orders/orders_table.html" %}
            </tbody>
        </table>
        </div>
</div>

orders_table.html:

{% for order in orders %}
    <tr role="row">
        <td class="sorting_1">{{ order.shop_order_id }}</td>
        <td class="">
            <span data-popup="tooltip" title="{{ order.date }}">
                {{ order.date|date:"M d, Y" }}
            </span>
        </td>
        <td>{{ order.carrier }}</td>
        <td>
            {% if order.packed %}
                <span class="label bg-grey">
                    Spakowano
                </span>
            {% else %}
                <span class="label label-info">
                    {{ order.status }}
                </span>
            {% endif %}
        </td>
{% endfor %}

javascript:

setInterval(function(){
    $.ajax({    
       url: '/Project/zamowienia/orders_test',
          success: function(data) {
          $('#orders_table').html(data);
          }
    });
}, 10000)

urls.py:

urlpatterns = [
    url(r'^$', orders_list),
    url(r'^orders_test$', orders_list_test),
]

views:

def orders_list(request):
    orders = Order.objects.all().order_by('-shop_order_id')
    carriers = Carriers.objects.all().order_by('name')
    statuses = Status.objects.all().order_by('name')
    Status().import_status()
    Order().import_orders()
    return render(
            request, 'orders/orders_list.html', 
            {
                'namespace': 'Zamówienia',
                'icon': 'credit-card',
                'statuses': statuses,
                'carriers': carriers,
                'orders': orders
            }
        )


def orders_list_test(request):
    orders = Order.objects.all().order_by('-shop_order_id')
    return render(
                request, 'orders/orders_table.html', 
                {
                    'orders': orders
                }
            )

Upvotes: 1

Views: 1657

Answers (1)

tomaszyo
tomaszyo

Reputation: 26

It's obvious, use table.draw() :).

Upvotes: 1

Related Questions