Integrating the DataTables plugin was straightforward and getting the Editor add-on integrated was also pretty painless - up to a point. However, the client/server side has been a bear for me.
The following is the JavaScript for DataTables and Editor. The part I cannot resolve is this snippet from the code that follows
var table = $('#theader').DataTable( { **bProcessing: true, bServerSide: true, start: 1, dataSrc: "id", sAjaxSource: 'load_user_json',** } After the JavaScript is the html code relevant to the JavaScript.
The examples given, on the DataTables/Editor site, use PHP on the server side. I know zero about PHP and I cannot figure out how to replace it with Python to pass back JSON (via the code snippet above) to the Javascript that follows using Ajax which is a current requirement of the DataTables plugin.
Everything looks great. Everything work except getting the new/edit/delete action to work. I started with the following example on the DataTables/Editor site.
https://editor.datatables.net/examples/styling/bootstrap.html
JAVASCRIPT (DataTables / Editor)
$(document).ready(function() { $(".dropdown-toggle").dropdown(); }); $(document).ready(function() { $(".dropdown-toggle").dropdown(); }); $(document).ready(function edit_users() { var csrftoken = getCookie('csrftoken'); var editorUser = new $.fn.dataTable.Editor( { ajax: '', table: "#theader", fields: [ { label: "ID:", name: "ID" }, { label: "Name:", name: "NAME" }, { label: "CM:", name: "CM" }, { label: "Email:", name: "EMAIL" } ] } ); if ( !$.fn.dataTable.isDataTable( '#theader' ) ) { $.ajaxSetup({ beforeSend: function(xhr, settings) { if (!csrfSafeMethod(settings.type) && !this.crossDomain) { xhr.setRequestHeader("X-CSRFToken", csrftoken); } } }); var table = $('#theader').DataTable( { bProcessing: true, bServerSide: true, start: 1, dataSrc: "id", sAjaxSource: 'load_user_json', columns: [ { data: "ID" }, { data: "NAME" }, { data: "CM" }, { data: "EMAIL" } ], select: true } ); } new $.fn.dataTable.Buttons( table, [ { extend: "create", editor: editorUser }, { extend: "edit", editor: editorUser }, { extend: "remove", editor: editorUser } ] ); table.buttons().container() .appendTo( $('.col-sm-6:eq(0)', table.table().container() ) ); $('#theader tfoot th').each( function () { var title = $(this).text(); $(this).html( '<input type="text" placeholder="Search '+title+'" />' ); } ); table.columns().every( function () { var that = this; $('input', this.footer() ).on( 'keyup change', function () { if ( that.search() !== this.value ) { that .search( this.value ) .draw(); } } ); } ); } ); HTML
{% extends "base.html" %} {% load crispy_forms_tags %} {% load staticfiles %} {% block content %} {% if queryset %} <h2>Current Users</h2> <table id="theader" class="table table-bordered table-hover small order-column"> <thead> <tr> <th>ID</th> <th>NAME</th> <th>CM</th> <th>EMAIL</th> </tr> </thead> <tfoot> <tr> <th>ID</th> <th>NAME</th> <th>CM</th> <th>EMAIL</th> </tr> </tfoot> <tbody> {% for row in queryset %} <tr id=forloop.counter> <!-- Needed for DataTables row identification --> <td>{{ row.operator_id }}</td> <td>{{ row.fullname }}</td> <td>{{ row.cm }}</td> <td>{{ row.email }}</td> </tr> {% endfor %} </tbody> </table> {% else %} <h4>No SKUs have been defined</h4> {% endif %} <script src="{% static 'js/searchable-users-table.js' %}"></script> {% endblock %}