简体   繁体   English

无法在django-datatables-view的数据表服务器端处理中使用搜索功能

[英]Can't use search feature in datatables server-side processing with django-datatables-view

I want to implement datatables with server-side processing because I have a lot of data to present in a single page(simple datatables are taking a lot of time).I went through the documentation in django-datatables-view 我想通过服务器端处理来实现数据表,因为我在一个页面中要呈现大量数据(简单的数据表会花费很多时间)。我浏览了django-datatables-view中的文档

I tried to implement it in my project,I can now successfully retrieve data from the server side but cannot use ' search ' feature. 我尝试在项目中实现它,现在可以从服务器端成功检索数据,但不能使用“ 搜索 ”功能。

If I type some thing in searchbox,it showing processing for a while but not updating the list of data displayed in the tables. 如果在搜索框中键入某些内容,它将显示一段时间的处理过程,但不会更新表中显示的数据列表。

I used python 3.6.5 and Django 2.0.6 我使用python 3.6.5和Django 2.0.6

Below is the code which I used: 下面是我使用的代码:

html : html:

  <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.min.css">
  <script src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>

  <script type="text/javascript">
    $(document).ready(function() {
    var a = $('#tl').dataTable( {
      "bPaginate": true,
      "bProcessing": true,
      "bServerSide": true,
      "sAjaxSource": "{% url 'order_list_json' %}"
    });
  });
  </script>

urls.py: urls.py:

from django.conf.urls import url
from django.contrib import admin
from .import views
from .views import OrderListJson

from django.contrib.staticfiles.urls import staticfiles_urlpatterns

urlpatterns = [
    .
    .
    .
    .
    url(r'^tabledata/$',OrderListJson.as_view(), name='order_list_json')
]

#used to access files in static folder using url 'static/'
urlpatterns += staticfiles_urlpatterns()

views.py: views.py:

from django_datatables_view.base_datatable_view import BaseDatatableView

.
.
.
.
.

class OrderListJson(BaseDatatableView):
    model  = Network1
    columns = ['barcode', 'volret', 'state', 'flags', 'capacity','typefull','ip']
    order_columns = ['barcode', 'volret', 'state', 'flags', 'capacity','typefull','ip']
    max_display_length = 100


    def get_initial_queryset(self):
        return Network1.objects.filter(barcode__in=Masterlist.objects.values('barcode'))

    def render_column(self, row, column):
        return super(OrderListJson, self).render_column(row, column)

    def filter_queryset(self, qs):

        search = self.request.GET.get(u'search[value]', None)
        if search:
            qs = qs.filter(name__istartswith=search)

        return qs

can anyone please help me to implement the search feature? 谁能帮我实现搜索功能?

I think your error is here: 我认为您的错误在这里:

qs = qs.filter(name__istartswith=search)

name should be the name of the column you want to search : name应该是您要搜索的列的名称:

['barcode', 'volret', 'state', 'flags', 'capacity','typefull','ip']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM