【发布时间】:2021-02-20 19:00:03
【问题描述】:
第 1 部分
我让我的数据表与 obj 迭代一起工作,但我正在移动到 DRF-datatables.. 虽然我让示例站点运行没有问题,但我的实际实现却让我很头疼。基本上,我整个周末都在展示一个似乎正在加载数据的表格(因为分页显示了正确的信息)但我的行都是空白的:https://i.imgur.com/ImA23Zo.png
然后我尝试在脚本中添加“列”部分,这打破了整个表格:https://i.imgur.com/ykwf58P.png
如果我将 ajax 调用更改为
"ajax": "/api/entry/?format=datatables",
然后我得到最紧密的工作表:https://i.imgur.com/p5G5uzk.png - 但是问题仍然是行是空的,如果我添加列,那么一切都会中断。
我哪里错了?我也没有收到任何错误消息,因此很难调试数据表。
可能是第 2 部分..
我什至需要使用 DRF 数据表吗?我的最终目标是能够选择多行,然后编辑所有这些条目的交易。
最终目标示例
- 选择 3 笔交易
- 点击表格顶部某处的下拉菜单
- 从该下拉列表中选择交易PK
- 点击保存
- 表格显示最新数据
entry_list.html
{% extends "dashboard/base.html" %}
{% load i18n %}
{% block content %}
<!-- Page Heading -->
<div class="d-sm-flex align-items-center justify-content-between mb-4">
<h2 class="text-gray-800">{% block title %}{% trans "Imported Entries" %}{% endblock %}</h2>
<a role="button" class="btn btn-success" href="{% url 'import' %}"><i
class="fas fa-plus-circle"></i> Import New Entires</a>
</div>
<!-- Content Row -->
<div class="row">
<div class="col-md-12">
<table id="entrytable"
class="table-hover table display"
align="center"
style="width:100%">
<thead>
<tr role="row">
<th class="sorting" tabindex="0" aria-controls="dataTable" rowspan="1"
colspan="1" aria-label="" style="">ID
</th>
<th class="sorting" tabindex="0" aria-controls="dataTable" rowspan="1"
colspan="1" aria-label="" style="">Date
</th>
<th class="sorting" tabindex="0" aria-controls="dataTable" rowspan="1"
colspan="1" aria-label="" style="">Trade
</th>
<th class="sorting" tabindex="0" aria-controls="dataTable" rowspan="1"
colspan="1" aria-label="" style="">Type
</th>
<th class="sorting" tabindex="0" aria-controls="dataTable" rowspan="1"
colspan="1" aria-label="" style="">Symbol
</th>
<th class="sorting" tabindex="0" aria-controls="dataTable" rowspan="1"
colspan="1" aria-label="" style="">Amount
</th>
<th class="sorting" tabindex="0" aria-controls="dataTable" rowspan="1"
colspan="1" aria-label="" style="">Price
</th>
<th class="sorting" tabindex="0" aria-controls="dataTable" rowspan="1"
colspan="1" aria-label="" style="">Fee
</th>
<th class="sorting" tabindex="0" aria-controls="dataTable" rowspan="1"
colspan="1" aria-label="" style="">Reg Fee
</th>
<th class="sorting" tabindex="0" aria-controls="dataTable" rowspan="1"
colspan="1" aria-label="" style="">Ref
</th>
</tr>
</thead>
</table>
</div>
</div>
{% endblock %}
{% block js %}
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.2/css/bootstrap.css"/>
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.22/css/dataTables.bootstrap4.min.css"/>
<!--https://datatables.net/examples/server_side/select_rows.html-->
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.22/css/jquery.dataTables.min.css"/>
<script type="text/javascript" src="https://code.jquery.com/jquery-3.5.1.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/1.10.22/js/jquery.dataTables.min.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/1.10.22/js/dataTables.bootstrap4.min.js"></script>
<script>
$(document).ready(function() {
var selected = [];
$("#entrytable").DataTable({
"order": [[ 1, "desc" ]],
"processing": true,
"serverSide": true,
"ajax": {
"url": "/api/entry/?format=datatables",
"type": "POST",
"beforeSend": function(xhr) {
xhr.setRequestHeader("X-CSRFToken", "{{ csrf_token|escapejs }}");
}
},
"columns": [
{"data": "pk"},
{"data": "date"},
{"data": "trade.id"},
{"data": "entry_type"},
{"data": "symbol"},
{"data": "amount"},
{"data": "price"},
{"data": "fee"},
{"data": "reg_fee"},
{"data": "transaction_id"},
]
"rowCallback": function( row, data ) {
if ( $.inArray(data.DT_RowId, selected) !== -1 ) {
$(row).addClass('selected');
}
}
});
$('#entrytable tbody').on('click', 'tr', function () {
var id = this.id;
var index = $.inArray(id, selected);
if ( index === -1 ) {
selected.push( id );
} else {
selected.splice( index, 1 );
}
$(this).toggleClass('selected');
} );
} );
</script>
{% endblock %}
序列化器.py
class EntrySerializer(serializers.ModelSerializer):
class Meta:
model = Entry
fields = '__all__'
Views.py
class EntryViewSet(viewsets.ModelViewSet):
"""
API endpoint that allows groups to be viewed or edited.
"""
queryset = Entry.objects.all()
serializer_class = EntrySerializer
permission_classes = [permissions.IsAdminUser]
filter_backends = (DjangoFilterBackend, OrderingFilter,)
models.py
class Entry(models.Model):
ENTRY = 'entry'
EXIT = 'exit'
ENTRY_TYPE_CHOICES = [
(ENTRY, 'Entry'),
(EXIT, 'Exit'),
# (DIVIDEND_RECEIVED, 'Dividend Received'),
# (DIVIDEND_SOLD, 'Dividend Sold'),
]
class Meta:
verbose_name = "Entry"
verbose_name_plural = "Entries"
trade = models.ForeignKey(Trade, on_delete=models.CASCADE, null=True, blank=True)
date = models.DateTimeField(null=True, blank=True, default=datetime.datetime.now)
amount = models.FloatField(null=True)
price = models.FloatField(null=True)
fee = models.FloatField(null=True, blank=True)
entry_type = models.CharField(max_length=5, choices=ENTRY_TYPE_CHOICES, default=ENTRY)
reg_fee = models.FloatField(null=True, blank=True)
transaction_id = models.CharField(max_length=100, null=True, blank=True)
symbol = models.ForeignKey(Symbol, on_delete=models.SET_NULL, blank=True, null=True)
created_by = models.ForeignKey(User, null=True, blank=True, editable=False, on_delete=models.CASCADE)
【问题讨论】:
标签: javascript python django ajax datatables