Data table not populating data in django-datatable-view

后端 未结 3 1095
伪装坚强ぢ
伪装坚强ぢ 2021-01-16 11:33

I\'ve started a new Django project with trying to test out django-datatable-view.

I\'m getting a JS error saying Uncaught TypeError: $$.each is not a function. Altho

相关标签:
3条回答
  • 2021-01-16 11:59

    In addition to openHBP answer, I had to put the initilization in a document.ready :

    <script type="text/javascript">                                                    
        $(document).ready(function() {                                                 
            var opts = {};                                                             
            var datatable = datatableview.initialize($('.datatable'), opts);           
            var table = datatable.api;                                                 
        } );                                                                           
    </script>
    
    0 讨论(0)
  • 2021-01-16 12:00

    I think the code belongs to 0.8 version and you've updated your package to 0.9. If that's the case you can look at the migration guide here

    the 0.9 version has multiple breaking changes. It no longer supports datatable_options in the class instead, things are shifted to Meta class

    0 讨论(0)
  • 2021-01-16 12:22

    I had exactly the same issue. So instead of

    <script type="text/javascript">
        $(function(){
            datatableview.initialize('.datatable');
        });
    </script>
    

    We have to init datatable so:

    <script type="text/javascript">
        var opts = {};
        var datatable = datatableview.initialize($('.datatable'), opts);
        var table = datatable.api();
    </script>
    

    And in your views.py, instead of

    class MyDatatableView(DatatableView):
        model = Revenue
        columns = ["title", "body", "created"]
        search_fields = ["title", "body"]
    

    You have to do (with the model or the query_set in the DatatableView:

    class MyDatatable(Datatable):
        class Meta:
            columns = ["title", "body", "created"]
            search_fields = ["title", "body"]
    
    
    class MyDatatableView(DatatableView):
        model = Revenue
        datatable_class = MyDatatable
    

    But then I get the following js essor, any idea? I'm using jQuery 3.3.1 and this version of datatable: http://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js

    Uncaught TypeError: datatable.api is not a function
        at HTMLDocument.<anonymous> (datatable:187)
        at c (jquery.min.js:3)
        at Object.fireWith [as resolveWith] (jquery.min.js:3)
        at Function.ready (jquery.min.js:3)
        at HTMLDocument.H (jquery.min.js:3)
    

    I just found the reason, the api call must be api and not api() because the () are added in the datatableview.js

    var table = datatable.api;
    

    My new issue is the search, it returns a 500 server error wichi is "FieldError('Related Field got invalid lookup: {}'.format(lookup_name))" But even if I add the column on which I want to search like "title__name", I still have the alert in the search stating:

    DataTables warning: table id=DataTables_Table_0 - Ajax error. For more information about this error, please see http://datatables.net/tn/7

    0 讨论(0)
提交回复
热议问题