我想在我的Django應用程序中實現一個基本的DataTables表。它只是似乎完全呈現時,在模板中不包括我的模板變量:Django模板中斷DataTable
current_orders.html
{% load staticfiles %}
<!DOCTYPE html>
<html lang="en">
<head>
<!-- jQuery -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<!-- DataTables.net -->
<link rel="stylesheet" href="{% static 'css/jquery.dataTables.min.css' %}">
<script src="{% static 'js/jquery.dataTables.min.js' %}"></script>
<script>
$(document).ready(function(){
$('#current_orders_table').DataTable();
});
</script>
<!-- Material Design Lite -->
<script src="https://storage.googleapis.com/code.getmdl.io/1.0.4/material.min.js"></script>
<link rel="stylesheet" href="https://storage.googleapis.com/code.getmdl.io/1.0.4/material.yellow-deep_purple.min.css" />
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
</head>
<body>
<table id="current_orders_table" class="display mdl-data-table <mdl-js-data-table></mdl-js-data-table> mdl-shadow--2dp">
<thead>
<tr>
<th class="mdl-data-table__cell--non-numeric">Customer Name</th>
<th>Date order was placed via Email</th>
<th>Opportunity ID</th>
<th>BTMW Reference</th>
<th>Sales Order Number</th>
<th>Supplier PO Numbers</th>
<th>SAS Date of Delivery</th>
<th>Serial Numbers Recorded?</th>
<th>INCare Reference Number</th>
<th>Contract Start Date</th>
<th class="mdl-data-table__cell--non-numeric">INCare Notes</th>
<th class="mdl-data-table__cell--non-numeric">Ongoing Progress</th>
<th class="mdl-data-table__cell--non-numeric">Actual Status of Order</th>
<th>Status Updated</th>
<th>Invoice Raised</th>
<th>Invoice Notes</th>
</tr>
</thead>
<tbody>
{% for order in orders %}
<tr>
<td class="mdl-data-table__cell--non-numeric">{{ order.customer }}</td>
<td>{{ order.order_date }}</td>
<td>{{ order.op_ref }}</td>
<td>{{ order.btmw_ref }}</td>
<td>{{ order.sales_order_number }}</td>
<td>{{ order.supplier_po_numbers }}</td>
<td>{{ order.sas_date_of_delivery }}</td>
<td>{{ order.sas_notes }}</td>
<td>{{ order.serial_numbers_recorded }}</td>
<td>{{ order.incare_ref_number }}</td>
<td>{{ order.contract_start }}</td>
<td class="mdl-data-table__cell--non-numeric">{{ order.incare_notes }}</td>
<td class="mdl-data-table__cell--non-numeric">{{ order.ongoing_progress }}</td>
<td class="mdl-data-table__cell--non-numeric">{{ order.actual_status }}</td>
<td>{{ order.status_updated }}</td>
<td>{{ order.invoice_raised }}</td>
<td>{{ order.invoice_notes }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</body>
</html>
views.py
def current_orders(request):
orders = get_list_or_404(Order, closed=False)
return render(request, 'tracker/current_orders.html',
{'orders': orders})
這會很樂意渲染一張桌子,但沒有這些額外的善良;搜索欄,分頁,排序箭頭等。但是,當我從<tbody>
刪除<tr> to </tr>
行,那些與{{ }}
模板變量,這些元素渲染罰款。
從chrome DevTools我可以看到有一個錯誤,但我的JavaScript知識很少,它讓我難倒了。在網上查看這似乎並不是一個常見問題,所以我的假設是我缺少一些基本的東西。任何幫助將非常感激。
Chrome DevTools error
Uncaught TypeError: Cannot read property 'mData' of undefined(anonymous function) @ jquery.dataTables.min.js:92m.extend.each @ jquery.js:384m.fn.m.each @ jquery.js:136(anonymous function) @ jquery.dataTables.min.js:92m.extend.each @ jquery.js:384m.fn.m.each @ jquery.js:136m @ jquery.dataTables.min.js:85h.fn.DataTable @ jquery.dataTables.min.js:163(anonymous function) @ (index):18m.Callbacks.j @ jquery.js:3148m.Callbacks.k.fireWith @ jquery.js:3260m.extend.ready @ jquery.js:3472J @ jquery.js:3503
和線路92 dataTables.min.js的:
g.slice());q=[];g=this.getElementsByTagName("thead");0!==g.length&&(fa(o.aoHeader,g[0]),q=qa(o));if(null===e.aoColumns)
{p=[];g=0;
for(i=q.length;g<i;g++)
p.push(null)
}else
p=e.aoColumns;g=0;
for(i=p.length;g<i;g++)
Ga(o,q?q[g]:null);
hb(o,e.aoColumnDefs,p,function(a,b){
la(o,a,b)});
if(s.length){
var u=function(a,b){
return a.getAttribute("data-"+b)!==null?b:null
};
h(s[0]).children("th, td").each(function(a,b){
var c=o.aoColumns[a];
if(c.mData===a){
var d=u(b,"sort")||u(b,"order"),e=u(b,"filter")||u(b,"search");
這可能是不相關的,但我看到身體中有17列,頭部只有16列。當表格格式不正確時,DataTables可能很容易被破壞。這只是乍一看,也許我沒有看到什麼...... – trpt4him
哦,血腥地獄。是的,就是這樣,謝謝! – bordeltabernacle