You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

296 lines
10 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

{% extends 'layout.html' %}
{% block content %}
<div class="container">
<div style="margin-bottom: 10px">
<input type="button" value="新建订单" class="btn btn-success" data-toggle="modal" data-target="#myModal">
</div>
<!-- 订单列表 -->
<div class="panel panel-default">
<!-- Default panel contents -->
<div class="panel-heading">
<span class="glyphicon glyphicon-th-list" aria-hidden="true"></span>
订单列表
</div>
<!-- Table -->
<table class="table table-bordered">
<thead>
<tr>
<th>ID</th>
<th>订单号</th>
<th>名称</th>
<th>价格</th>
<th>状态</th>
<th>管理员</th>
<th>操作</th>
</tr>
</thead>
<tbody>
{% for obj in queryset %}
<tr uid="{{ obj.id }}">
<th>{{ obj.id }}</th>
<td>{{ obj.oid }}</td>
<td>{{ obj.title }}</td>
<td>{{ obj.price }}</td>
<td>{{ obj.get_status_display }}</td>
<td>{{ obj.admin.username }}</td>
<td>
<input uid="{{ obj.id }}" type="button" class="btn btn-primary btn-xs btn-edit" value="编辑">
<input uid="{{ obj.id }}" class="btn btn-danger btn-xs btn-delete" type="button" value="删除">
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
<!-- 分页列表 -->
<ul class="pagination">
{{ page_string }}
</ul>
</div>
<!-- 新建/编辑 订单(对话框) -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
aria-hidden="true">&times;</span></button>
<h4 class="modal-title" id="myModalLabel">新建</h4>
</div>
<div class="modal-body">
<form id="formAdd">
<div class="clearfix">
{% for field in form %}
<div class="col-xs-6">
<div class="form-group" style="position: relative;margin-bottom: 20px;">
<label>{{ field.label }}</label>
{{ field }}
<span class="error-msg" style="color: red;position: absolute;"></span>
</div>
</div>
{% endfor %}
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">取 消</button>
<button id="btnSave" type="button" class="btn btn-primary">保 存</button>
</div>
</div>
</div>
</div>
<!-- 删除(对话框) -->
<div class="modal fade" id="deleteModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="alert alert-danger alert-dismissible fade in" role="alert">
<h4>是否确定删除?</h4>
<p style="margin: 10px 0;"> 删除后,所有关联的相关数据都会被删除。</p>
<p style="text-align: right;">
<button id="btnConfirmDelete" type="button" class="btn btn-danger">确 定</button>
<button type="button" class="btn btn-default" data-dismiss="modal">取 消</button>
</p>
</div>
</div>
</div>
{% endblock %}
{% block js %}
<script type="text/javascript">
var DELETE_ID;
var EDIT_ID;
$(function () {
bindBtnAddEvent();
bindBtnSaveEvent();
bindBtnDeleteEvent();
bindBtnConfirmDeleteEvent();
bindBtnEditEvent();
})
function bindBtnAddEvent() {
$("#btnAdd").click(function () {
// 将正在编辑的ID设置为空
EDIT_ID = undefined;
// 清空对话框中的数据
$("#formAdd")[0].reset();
// 设置对话框的标题
$("#myModalLabel").text("新建");
// 点击新建按钮,显示对话框。
$('#myModal').modal('show');
});
}
function bindBtnSaveEvent() {
$("#btnSave").click(function () {
// 清除错误信息
$(".error-msg").empty();
if (EDIT_ID) {
// 编辑
doEdit();
} else {
// 添加
doAdd();
}
});
}
function doEdit() {
// 向后台发送请求添加的Ajax请求
$.ajax({
url: "/order/edit/" + "?uid=" + EDIT_ID, // -> /order/edit/?uid=12
type: "post",
data: $("#formAdd").serialize(),
dataType: "JSON",
success: function (res) {
if (res.status) {
// 清空表单 $("#formAdd")是jQuery对象 -> $("#formAdd")[0] DOM对象
$("#formAdd")[0].reset();
// 关闭对话框
$('#myModal').modal('hide');
// 刷新页面
location.reload();
} else {
if (res.tips) {
alert(res.tips);
} else {
// 把错误信息显示在对话框中。
$.each(res.error, function (name, errorList) {
$("#id_" + name).next().text(errorList[0]);
})
}
}
}
})
}
function doAdd() {
// 向后台发送请求添加的Ajax请求
$.ajax({
url: "/order/add/",
type: "post",
data: $("#formAdd").serialize(),
dataType: "JSON",
success: function (res) {
if (res.status) {
// alert("创建成功");
// 清空表单 $("#formAdd")是jQuery对象 -> $("#formAdd")[0] DOM对象
$("#formAdd")[0].reset();
// 关闭对话框
$('#myModal').modal('hide');
// 刷新页面
location.reload();
} else {
// 把错误信息显示在对话框中。
$.each(res.error, function (name, errorList) {
$("#id_" + name).next().text(errorList[0]);
})
}
}
})
}
function bindBtnDeleteEvent() {
$(".btn-delete").click(function () {
// alert("点击了删除");
// 显示删除对话框
$("#deleteModal").modal('show');
// 获取当前行的ID并赋值给全部变量。
DELETE_ID = $(this).attr("uid");
});
}
function bindBtnConfirmDeleteEvent() {
$("#btnConfirmDelete").click(function () {
$.ajax({
url: "/order/delete/", // => /order/delete/?uid=123
type: "GET",
data: {
uid: DELETE_ID
},
dataType: "JSON",
success: function (res) {
if (res.status) {
// 隐藏删除框
// $("#deleteModal").modal('hide');
// 在页面上将当前一行数据删除js
// $("tr[uid='" + DELETE_ID + "']").remove();
// 要删除的ID制空
// DELETE_ID = 0;
// 简单的思路:
location.reload();
} else {
// 删除失败
alert(res.error);
}
}
})
});
}
function bindBtnEditEvent() {
$(".btn-edit").click(function () {
// 清空对话框中的数据
$("#formAdd")[0].reset();
var uid = $(this).attr("uid");
EDIT_ID = uid;
// 发送Ajax去后端获取当前行的相关数据 /order/detail/?uid=123
$.ajax({
url: "/order/detail/",
type: "get",
data: {
uid: uid
},
dataType: "JSON",
success: function (res) {
if (res.status) {
// 将数据赋值到对话框中的标签中。
$.each(res.data, function (name, value) {
$("#id_" + name).val(value);
})
// 修改对话框的标题
$("#myModalLabel").text("编辑");
// 点击编辑,显示对话框
$('#myModal').modal('show');
} else {
alert(res.error);
}
}
})
});
}
</script>
{% endblock %}