【发布时间】:2016-11-01 11:37:09
【问题描述】:
集成 DataTables 插件非常简单,集成 Editor 插件也非常轻松 - 在某种程度上。但是,客户端/服务器端对我来说是一个负担。
以下是用于数据表和编辑器的 JavaScript。我无法解决的部分是后面代码中的这个 sn-p
var table = $('#theader').DataTable( {
**bProcessing: true,
bServerSide: true,
start: 1,
dataSrc: "id",
sAjaxSource: 'load_user_json',**
}
JavaScript后面是与JavaScript相关的html代码。
在 DataTables/Editor 站点上给出的示例在服务器端使用 PHP。我对 PHP 的了解为零,我不知道如何用 Python 替换它以将 JSON(通过上面的代码 sn-p)传回使用 Ajax 的 Javascript,这是 DataTables 插件的当前要求。
一切看起来都很棒。除了让新/编辑/删除操作起作用之外,一切都正常。我从 DataTables/Editor 网站上的以下示例开始。
https://editor.datatables.net/examples/styling/bootstrap.html
JAVASCRIPT(数据表/编辑器)
$(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 %}
【问题讨论】:
-
您是否考虑过使用 DataTables PHP 库?作者编写了一个服务器端库,它的文档很好(尽管很难找到)。 Link to Documentation
-
谢谢,实际上我没有考虑过。需要考虑的事情
标签: django datatables