Django编辑或删除表中的选定行-ListView
我有一个带有复选框的表,我希望能够删除或编辑表中所有选定行的特定字段值 下面是一个示例表,重新创建它会非常棒,但我在视图和模板中都没有找到这样做的示例 我的当前视图,它正在使用表。我可以从哪里开始从基本表过渡到上面示例中的交互式表 Views.pyDjango编辑或删除表中的选定行-ListView,django,listview,django-rest-framework,Django,Listview,Django Rest Framework,我有一个带有复选框的表,我希望能够删除或编辑表中所有选定行的特定字段值 下面是一个示例表,重新创建它会非常棒,但我在视图和模板中都没有找到这样做的示例 我的当前视图,它正在使用表。我可以从哪里开始从基本表过渡到上面示例中的交互式表 Views.py class EntryList(LoginRequiredMixin, ListView): context_object_name = 'entry_list' paginate_by = 100 # paginate_b
class EntryList(LoginRequiredMixin, ListView):
context_object_name = 'entry_list'
paginate_by = 100
# paginate_by = 5
#ordering = ['-pk']
model = Entry
template_name = "portfolios/entry_list.html"
def get_queryset(self):
return Entry.objects.filter(created_by=self.request.user).order_by('-pk')
def post(self, request, *args, **kwargs):
ids = self.request.POST.get('ids', "")
# ids if string like "1,2,3,4"
ids = ids.split(",")
try:
# Check ids are valid numbers
ids = map(int, ids)
except ValueError as e:
return JsonResponse(status=400)
# delete items
self.model.objects.filter(id__in=ids).delete()
return JsonResponse({"status": "ok"}, status=204)
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">
<div class="card shadow mb-4">
<div class="card-body">
<div id="dataTable_wrapper" class="dataTables_wrapper dt-bootstrap4">
<div class="row">
</div>
<div class="row">
<div class="col-sm-12">
<table class="table-responsive-xl table table-hover table-striped table-bordered dataTable" id="dataTable" width="100%"
cellspacing="0" role="grid" aria-describedby="dataTable_info">
<thead>
<tr role="row">
<th class="sorting" tabindex="0" aria-controls="dataTable" rowspan="1"
colspan="1" aria-label="" style="">
</th>
<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>
<tbody>
{% for entry in object_list %}
<tr role="row">
<td class="text-center">
<div class="custom-control-lg custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input form-control-lg" data-id="{{ entry.pk}}" id="check{{ entry.pk }}">
<label class="custom-control-label" for="check{{ entry.pk }}"></label>
</div>
</td>
<td><a href="{% url 'entry-update' entry.pk %}">{{ entry.pk }}</a></td>
<td>{{ entry.date | date:"M d, Y h:i:s A"}}</td>
<td><a href="/trade/{{ entry.trade.id }}">{{ entry.trade.id }}</a></td>
<td>{{ entry.entry_type }}</td>
<td>{{ entry.symbol }}</td>
<td>{{ entry.amount }}</td>
<td>{{ entry.price }}</td>
<td>{{ entry.fee }}</td>
<td>{{ entry.reg_fee }}</td>
<td>{{ entry.transaction_id }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
<!--Pagination-->
<div class="row">
<div class="col-12 ">
<div class="pagination">
<span class="step-links">
{% if page_obj.has_previous %}
<a href="?page=1">« first</a>
<a href="?page={{ page_obj.previous_page_number }}">previous</a>
{% endif %}
<span class="current">
Page {{ page_obj.number }} of {{ page_obj.paginator.num_pages }}.
</span>
{% if page_obj.has_next %}
<a href="?page={{ page_obj.next_page_number }}">next</a>
<a href="?page={{ page_obj.paginator.num_pages }}">last »</a>
{% endif %}
</span>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
{% endblock content %}
{%extends“dashboard/base.html”%}
{%load i18n%}
{%block content%}
{%block title%}{%trans“导入的条目”%}{%endblock%}
身份证件
日期
贸易
类型
象征
数量
价格
费用
注册费
裁判
{对象_列表%中的条目为%0}
{{entry.date}date:“md,yh:i:sa”}
{{entry.entry_type}
{{entry.symbol}
{{entry.amount}
{{entry.price}}
{{entry.fee}
{{entry.reg_fee}
{{entry.transaction_id}
{%endfor%}
{%如果页面_obj.has_previous%}
{%endif%}
第{{Page_obj.paginator.num_pages}页中的第{{Page_obj.number}页。
{%如果页面_obj.has_next%}
{%endif%}
{%endblock内容%}
这取决于您是如何实现前端的,但假设您有标准的django模板,那么我建议您看看。它有很多功能,但是非常稳定,有一个不错的功能集,文档也很好。你可以用你想要的方式来设计它
此外,您还可以链接到引导表-这应该是相同的原则。通读文档以了解其工作原理,您必须使用Django模板标记来呈现表中的数据
请注意,该表是使用HTML/Jquery实现的,因此它与Django没有直接关系。您所需要做的就是迭代模板()中的django对象
编辑
如何删除所选行
假设您可以选择N行,然后单击按钮删除所有这些行
这可以按如下方式进行:
- 将处理程序添加到“删除”按钮:
- 标识所选行
- 发送Ajax请求以删除行
- 处理成功响应以从表中删除删除的行
这里也有同样的原则。选择行后,使用一个识别所选行的处理程序,然后您可以使用datatables api更新给定字段()。我的表工作正常,处于引导状态,并且已经有一个复选框。我的问题是让桌子有动作。如何删除所选行?如何选择行并快速更改所有选定行的字段?感谢更新后的答案!我会在这里更新,如果我有任何问题,但现在,这似乎是100%的正确方向。我必须考虑哪个方向对我的情况最好,数据表还是模板标签方法。如果您有任何关于哪一个是理想的反馈,我总是乐于接受反馈。请注意,您可以同时使用模板标记和数据表-数据表只是一个库,可以帮助处理渲染表的大多数方面。让datatables工作起来可能很棘手,但有相当好的文档和专门的论坛,所以值得花时间
// SIMPLIFIED CODE SAMPLE
$("#delete-btn").click(function () {
var selectedRows = table.rows({"selected": true});
var dataObj = {
// parse selectedRows to get object ids
}
$.ajax({
url: '/api/delete-rows/',
type: 'post',
data: dataObj,
success: function (data, status, xhr) {
// remove the selected rows from the view
table.rows({"selected": true}).deselect().remove().draw();
}
})
}