ajax结合sweetalert使用

ajax可以在不刷新页面的情况下与后端进行交互,在对数据进行操作的时候,可以让ajax与sweetalert一起使用,sweetalert是页面框,当用户在删除数据的时候,可以给用户一个确认的机会,这个时候就可以使用ajax与sweetalert一起使用,在不刷新页面的情况下进行页面的交互。

1. var $btn = $(this);
# this 表示当前对象本身,表示出发时间的当前元素,是一个实参,
2.$('.cancel').click(function ()
# cancel相当于一个类的标签,使用.cancel可以查找到这个属性
3.data:{'delete_id':$btn.attr('userId')}
# attr attr指的属性是html标签属性,
4.$btn.parent().parent().remove()
# parent() 父亲元素,表示删除a标签的父亲的父亲

案例代码

当用户删除数据的时候会有一个提示框提示用户是都确定要删除数据,如果用户点确定,就将数据进行删除

# views中

import time
from django.http import JsonResponse
from app01 import models
# 测试模态框
def model_box(request):
if request.is_ajax():
back_dic = {'code':1000,'msg':''}
u_id = request.POST.get('id')
time.sleep(3)
models.Book.objects.filter(pk=u_id).delete()
back_dic['msg'] = '数据已删除'
return JsonResponse(back_dic)
book_obj = models.Book.objects.all()
return render(request,'model_box.html', locals())
# HTML中

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>模态框测试</title>
{% load static %}
<script src="{% static 'jquery/jquery.min.js' %}"></script>
<script src="{% static 'bootstrap-3.3.7-dist/bootstrap.min.js' %}"></script>
<script src="{% static 'dist/sweetalert.js' %}"></script>
<link rel="stylesheet" href="{% static 'dist/sweetalert.css' %}">
<link rel="stylesheet" href="{% static 'bootstrap-3.3.7-dist/bootstrap.min.css' %}"> </head>
<body>
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="tab-content">
<p><h3 class="text-center">模态框展示</h3></p>
<table class="table table-bordered table-hover table-striped">
<thead>
<tr>
<th>序号</th>
<th>书名</th>
<th>价格</th>
<th class="text-center" id="d1">操作</th>
</tr>
</thead>
<tbody>
{% for foo in book_obj %}
<tr>
<td>{{ forloop.counter }}</td>
<td>{{ foo.book_name }}</td>
<td>{{ foo.price }}</td>
<td class="text-center" id="{{ foo.id }}">
<a href="" class="btn btn-success btn-primary btn-sm">编辑</a>
<a href="#" class="btn btn-danger btn-primary btn-sm xxx" userid={{ foo.pk }}>删除</a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
</div>
</div> <script>
$('.xxx').click(function(){
var $btn = $(this);
swal({
title: "你确定删除嘛",
text: "删除操作是不可以恢复的!!!",
type: "warning",
showCancelButton: true,
confirmButtonClass: "btn-danger",
confirmButtonText: "是的,我确定!",
cancelButtonText: "不,我再想想!",
closeOnConfirm: false,
closeOnCancel: false
},
function(isConfirm) {
if (isConfirm) {
$.ajax({
url:'',
type:'post',
data:{'id':$btn.attr('userid')},
success:function(data){
if(data.code==1000){
swal("Deleted!", "Your imaginary file has been deleted.", "success");
$btn.parent().parent().remove()}
else{
swal('有bug,不能删除','warning')
}
}
});
} else {
swal("Cancelled", "Your imaginary file is safe :)", "error");
}
});
})
</script>
</body>
</html>

bulk_create批量插入数据

当我们有多条数据的时候,我们可以使用bulk_create来进行批量插入,bulk_create是将数据批量插入到数据库中。如果直接将多条数据直接的传进前端,前端会因为一次性刷新太慢,可能会导致报错的页面,所以回引入分页器的知识,下边会讲。

pager_list = []
for i in range(1000):
pager_list.append(models.Pager(title='This is %s book'%i))
pager_obj = models.Pager.objects.bulk_create(pager_list)

分页器的使用

分页器是将多条数据按照一定的条件进行分配,像百度搜索出来的结果,规定每一页就显示一定的数量,通过点击下边的分页按钮,将数据一点一点的取出,这样可以让用户又更好的体验,不会出现一次又多个结果的时候页面因为数据过多而不能进行刷新。所以使用分页器可以极大的方便我们日常办公的使用。

实例:

将数据库中的多条数据进行接收,并进行分页显示出来。

关于分页器的难点:

1.将多条数据按照固定的数量进行分页,并确定页数,使用方法:divmod()

2.当前页码要随着用户的输入,放在中间,分三种情况,在最前边,在最后边,在中间。

3.对每一页的数据进行切分,将每一个分页中的数据对应每一个个切分的内容,

4.分页器的代码要在后端中写,再将写好的前端代码块发送到后端

# 分页器调用模板
class Pagination(object):
def __init__(self, current_page, all_count, per_page_num=2, pager_count=11):
"""
封装分页相关数据
:param current_page: 当前页
:param all_count: 数据库中的数据总条数
:param per_page_num: 每页显示的数据条数
:param pager_count: 最多显示的页码个数 用法:
queryset = model.objects.all()
page_obj = Pagination(current_page,all_count)
page_data = queryset[page_obj.start:page_obj.end]
获取数据用page_data而不再使用原始的queryset
获取前端分页样式用page_obj.page_html
"""
try:
current_page = int(current_page)
except Exception as e:
current_page = 1 if current_page < 1:
current_page = 1 self.current_page = current_page self.all_count = all_count
self.per_page_num = per_page_num # 总页码
all_pager, tmp = divmod(all_count, per_page_num)
if tmp:
all_pager += 1
self.all_pager = all_pager self.pager_count = pager_count
self.pager_count_half = int((pager_count - 1) / 2) @property
def start(self):
return (self.current_page - 1) * self.per_page_num @property
def end(self):
return self.current_page * self.per_page_num def page_html(self):
# 如果总页码 < 11个:
if self.all_pager <= self.pager_count:
pager_start = 1
pager_end = self.all_pager + 1
# 总页码 > 11
else:
# 当前页如果<=页面上最多显示11/2个页码
if self.current_page <= self.pager_count_half:
pager_start = 1
pager_end = self.pager_count + 1 # 当前页大于5
else:
# 页码翻到最后
if (self.current_page + self.pager_count_half) > self.all_pager:
pager_end = self.all_pager + 1
pager_start = self.all_pager - self.pager_count + 1
else:
pager_start = self.current_page - self.pager_count_half
pager_end = self.current_page + self.pager_count_half + 1 page_html_list = []
# 添加前面的nav和ul标签
page_html_list.append('''
<nav aria-label='Page navigation>'
<ul class='pagination'>
''')
first_page = '<li><a href="?page=%s">首页</a></li>' % (1)
page_html_list.append(first_page) if self.current_page <= 1:
prev_page = '<li class="disabled"><a href="#">上一页</a></li>'
else:
prev_page = '<li><a href="?page=%s">上一页</a></li>' % (self.current_page - 1,) page_html_list.append(prev_page) for i in range(pager_start, pager_end):
if i == self.current_page:
temp = '<li class="active"><a href="?page=%s">%s</a></li>' % (i, i,)
else:
temp = '<li><a href="?page=%s">%s</a></li>' % (i, i,)
page_html_list.append(temp) if self.current_page >= self.all_pager:
next_page = '<li class="disabled"><a href="#">下一页</a></li>'
else:
next_page = '<li><a href="?page=%s">下一页</a></li>' % (self.current_page + 1,)
page_html_list.append(next_page) last_page = '<li><a href="?page=%s">尾页</a></li>' % (self.all_pager,)
page_html_list.append(last_page)
# 尾部添加标签
page_html_list.append('''
</nav>
</ul>
''')
return ''.join(page_html_list)
# views中

from django.shortcuts import render
from app01 import models
from app01.utils import mypage
def Pager(request):
pager = models.Pager.objects.all() current_page = request.GET.get('page', 1)
all_count = pager.count()
page_obj = mypage.Pagination(current_page=current_page,all_count=all_count,per_page_num=10,pager_count=5)
page = pager[page_obj.start:page_obj.end] return render(request,'index.html',locals())
# HTML中

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
{% load static %}
<script src="{% static 'jquery/jquery.min.js' %}"></script>
<script src="{% static 'bootstrap-3.3.7-dist/bootstrap.min.js' %}"></script>
<script src="{% static 'dist/sweetalert.js' %}"></script>
<link rel="stylesheet" href="{% static 'dist/sweetalert.css' %}">
<link rel="stylesheet" href="{% static 'bootstrap-3.3.7-dist/bootstrap.min.css' %}">
</head>
<body>
<div class="container-fluid">
<div class="row">
<div class="col-md-8 col-md-offset-2">
{% for book in page %}
<p>{{ book.title }}</p>
{% endfor %}
{{ page_obj.page_html|safe }}
</div>
</div>
</div> </body>
</html>

最新文章

  1. BZOJ3082: Graph2
  2. postgis数据库文件shapefile导入 dbf file (.dbf) can not be opened.shapefile import failed.
  3. 提高D3js力导向图加载速度(转)
  4. 电赛菜鸟营培训(三)&mdash;&mdash;STM32F103CB之串口通信
  5. Ubuntu彻底删除MySQL重装MySQL
  6. 归并排序 &amp; 计数排序 &amp; 基数排序 &amp; 冒泡排序 &amp; 选择排序 ----&gt; 内部排序性能比较
  7. newClass a = Func(3)中隐藏的操作
  8. 用Intellij Idea创建简单的Servlet
  9. ubuntu - sudo in php exec
  10. DC综合流程
  11. Unity IOC注入详细配置(MVC,WebApi)
  12. DOM的event对象的属性和方法
  13. Web安全检测工具的使用.
  14. Docker使用Mysql镜像命令
  15. python模块和包(模块、包、发布模块)
  16. Java笔记(十一)通用容器类和总结
  17. SQL Server 索引自动组织维护
  18. [转载]Javascript .then()这个方法是什么意思?
  19. maven入门安装及HelloWorld实现
  20. centos 7 nginx 安装

热门文章

  1. LeetCode 32. 最长有效括号(Longest Valid Parentheses) 31
  2. [案例一] Spring中的事件驱动模型(机制)
  3. [转帖]B树索引、位图索引和散列索引
  4. Automatically generating nice graphs at end of your Load Test with Apache JMeter and JMeter-Plugins
  5. 一起来学Spring Cloud | 第八章:消息总线(Spring Cloud Bus)
  6. (一)线性表(linear list)
  7. Json 文件读写以及和IniFile 转换
  8. linux 创建虚拟机常见错误
  9. 访问Harbor报502 Bad Gateway
  10. 转换属性transform