一、前端代码

  1,父组件free_course.vue

<template>
<div id="free_course">
<el-container>
<el-header class="header"><Header :current_state="current_state"/></el-header>
<div style="background-color: #f6f6f6">
<div>
<Filters @url="url_change"/>
</div >
<div v-for="item in info">
<content_free :item="item"/>
</div>
<el-pagination
@current-change="handleCurrentChange"
background
layout="prev, pager, next"
:page-size="page_size"
:current-page.sync="current_page"
:total=total>
</el-pagination>
</div>
<div class="nothing" v-show="info.length==0"></div>
<div class="nothing1" v-show="info.length==1"></div>
<Footer/>
</el-container>
</div>
</template> <script>
import Header from '../common/header'
import Footer from '../common/footer'
import Filters from './filter'
import content_free from './content_free'
export default {
name:'free_course',
data:function () {
return {
page:2,
info:[],
current_state:1,
total:10,
page_size:1,
current_page:1,
params:'',
url:'http://127.0.0.1:8000/course/course'
}
},
components:{
Header,Footer,Filters,content_free
},
methods:{
handleCurrentChange(val){
// 名字必须固定
let url = this.url;
url+="?page="+val+"&page_size="+this.page_size+'&'+this.params;
this.$axios.get(url).then(res=>{
this.info = res.data.results;
this.total =res.data.count;
}).catch(error=>{
console.log(error.response);
})
},
url_change:function (params) {
this.params=params;
this.current_page=1;
this.handleCurrentChange(1);
}
},
created:function () {
this.handleCurrentChange(1)
}
}
</script>

  父组件的css

<style scoped>
.el-header,.el-footer{
padding: 0;
}
.el-header{
height: 80px !important;
}
.el-pagination{
width: 380px;
margin: 0 auto;
}
.nothing{
height: 519px;
background-color: rgb(246, 246, 246)
}
.nothing1{
height: 229px;
background-color: rgb(246, 246, 246)
}
</style>

  2,子组件filter.vue

<template>
<div id="filter">
<div class="filter">
<el-row :gutter="5">
<el-col :span="3"><span>学科分类:</span></el-col>
<el-col :span="3"><span class="selected" @click="all($event,'course_category',0)">全部</span></el-col>
<el-col v-for="item in info " :span="3" >
<span @click="single($event,'course_category',item.id,0)">{{item.name}}</span>
</el-col>
</el-row>
<el-row>
<el-col :span="3"><span>筛&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;选:</span></el-col>
<el-col :span="3"><span class="selected" @click="all($event,'ordering',1)">默认</span></el-col>
<el-col :span="3"><span @click="single($event,'ordering','-students',1)">人气</span></el-col>
<el-col :span="3"><span class="tubiao"><span class="left">价格</span><i class="el-icon-caret-top top" :style='tubiao_status==1 ? ss :""' @click="price($event,'ordering','-price',1)"></i><i class="el-icon-caret-bottom bottom" :style='tubiao_status==2 ? ss :""' @click="price($event,'ordering','price',2)"></i></span></el-col>
</el-row>
</div>
</div>
</template> <script>
export default {
name:'Filters',
data:function () {
return {
info:[],
url_dict:{},
tubiao_status:'',
ss:{
color:'#ffc210',
},
}
},
methods:{
url:function(){
let params='';
for (let item in this.url_dict){
params=params+`${item}=${this.url_dict[item]}&`
}
this.$emit('url',params);
},
price:function(event,type,value,num){
this.url_dict[type]=value;
this.tubiao_status=num;
this.url();
let target=event.currentTarget;
let parenttarget=target.parentElement.parentElement.parentElement.children;
for(let item in parenttarget){
if (parenttarget[item].firstElementChild){
parenttarget[item].firstElementChild.classList.remove('selected');
}
}
},
selected:function (event) {
let target=event.currentTarget;
let parenttarget=target.parentElement.parentElement.children;
for(let item in parenttarget){
if (parenttarget[item].firstElementChild){
parenttarget[item].firstElementChild.classList.remove('selected');
}
}
target.classList.add('selected');
},
all:function (event,type,num) {
this.selected(event);
delete this.url_dict[type];
if (num==1){
this.tubiao_status='';
}
this.url()
},
single:function (event,type,value,num) {
this.selected(event);
this.url_dict[type]=value;
if (num==1){
this.tubiao_status='';
}
this.url()
},
},
created:function () {
let _this=this;
this.$axios.get('http://127.0.0.1:8000/course/courseclassify/')
.then(function (res) {
_this.info=res.data;
}).catch(function (error) {
console.log(error.response)
})
}
}
</script>

  子组件的css

<style scoped>
.filter{
width: 1060px;
height: 150px;
margin: 0 auto;
background-color: white;
padding: 20px;
margin-top: 20px;
}
.selected{
padding: 6px 16px;
color: #ffc210;
border: 1px solid #ffc210!important;
border-radius: 30px;
}
.el-row{
margin-top: 20px;
}
.tubiao{
position: relative;
display: inline-block;
height: 22px;
}
.tubiao .top{
position: absolute;
top: 0;
left: 34px;
cursor: pointer;
}
.tubiao .bottom{
position: absolute;
bottom: 0;
left: 34px;
cursor: pointer;
}
.tubiao .left{
position: absolute;
left: 0;
top: 0;
width: 32px;
cursor: default;
}
.el-col>span{
cursor: pointer;
}
</style>

  二、后端代码

  settings.py配置

REST_FRAMEWORK = {
#过滤组件
'DEFAULT_FILTER_BACKENDS': ('django_filters.rest_framework.DjangoFilterBackend',
# 解决过滤组件和排序组件之间的覆盖问题
'rest_framework.filters.OrderingFilter'),
}

  views.py

class StandardPageNumberPagination(PageNumberPagination):
page_size_query_param = 'page_size'
max_page_size = 1
class CourseView(ListAPIView):
queryset = Course.objects.filter(status=0,is_delete=False).order_by('orders')
serializer_class = CourseModelSeralizer
#设置过滤字段
filter_fields = ('course_category',)
#必须把这个删掉,它和后面的排序字段冲突
# filter_backends = [OrderingFilter]
ordering_fields = ('id', 'students', 'price')
pagination_class = StandardPageNumberPagination

  serializer.py

from rest_framework import serializers
from .models import * class CourseClassifyModelSerializer(serializers.ModelSerializer):
class Meta:
model=CourseClassify
fields=('id','name',) class TeacherModelSerializer(serializers.ModelSerializer):
class Meta:
model=Teacher
fields=['name','role','image','brief']
class CourseLessonModelSerializer(serializers.ModelSerializer):
class Meta:
model=CourseLesson
fields = [ 'name', 'section_link','duration','free_trail','orders'] class CourseChapterModelSerializer(serializers.ModelSerializer):
coursesections=CourseLessonModelSerializer(many=True)
class Meta:
model=CourseChapter
fields=['chapter','name','coursesections'] class PriceServicesModelSerializer(serializers.ModelSerializer):
"""价格服务策略序列化器"""
class Meta:
model = PriceService
fields = ("condition","sale",'remaintime') class PriceServiceTypeModelSerializer(serializers.ModelSerializer):
"""价格服务类型序列化器"""
priceservices = PriceServicesModelSerializer(many=True)
# priceservices 价格策略
class Meta:
model = PriceServiceType
fields = ("id","name","priceservices") class CourseModelSeralizer(serializers.ModelSerializer):
teacher=TeacherModelSerializer()
coursechapters=CourseChapterModelSerializer(many=True)
price_service_type=PriceServiceTypeModelSerializer() class Meta:
model=Course
fields=['id','name','course_img','students','brief_url','levels','lessons','pub_lessons','price','teacher','coursechapters','price_service_type','current_price']

vue_drf之多级过滤、排序、分页的更多相关文章

  1. DRF 过滤排序分页异常处理

    DRF 中如何使用过滤,排序,分页,以及报错了如何处理?10分钟get了~

  2. 三 drf 认证,权限,限流,过滤,排序,分页,异常处理,接口文档,集xadmin的使用

    因为接下来的功能中需要使用到登陆功能,所以我们使用django内置admin站点并创建一个管理员. python manage.py createsuperuser 创建管理员以后,访问admin站点 ...

  3. drf07 过滤 排序 分页 异常处理 自动生成接口文档

    4. 过滤Filtering 对于列表数据可能需要根据字段进行过滤,我们可以通过添加django-fitlter扩展来增强支持. pip install django-filter 在配置文件sett ...

  4. day74:drf:drf其他功能:认证/权限/限流/过滤/排序/分页/异常处理&自动生成接口文档

    目录 1.django-admin 2.认证:Authentication 3.权限:Permissions 4.限流:Throttling 5.过滤:Filtering 6.排序:OrderingF ...

  5. DRF之过滤排序分页异常处理

    一.过滤 对于列表数据要通过字段来进行过滤,就需要添加 django-filter 模块 使用方法: # 1.注册,在app中注册 settings.py INSTALLED_APPS = [ 'dj ...

  6. Contoso 大学 - 3 - 排序、过滤及分页

    原文 Contoso 大学 - 3 - 排序.过滤及分页 目录 Contoso 大学 - 使用 EF Code First 创建 MVC 应用 原文地址:http://www.asp.net/mvc/ ...

  7. Ecside基于数据库的过滤、分页、排序

    首先ecside展现列表.排序.过滤(该三种操作以下简称为 RSF )的实现原理完全和原版EC一样, 如果您对原版EC的retrieveRowsCallback.sortRowsCallback.fi ...

  8. Mysql 单表查询-排序-分页-group by初识

    Mysql 单表查询-排序-分页-group by初识 对于select 来说, 分组聚合(((group by; aggregation), 排序 (order by** ), 分页查询 (limi ...

  9. jsp+oracle 排序分页+Pageutil类

    1.rownum和排序 Oracle中的rownum的是在取数据的时候产生的序号,所以想对指定排序的数据去指定的rowmun行数据就必须注意了. SQL> select rownum ,id,n ...

随机推荐

  1. queue deque

  2. File(File f, String child) File(String parent, String child)

    (转载)File(File f, String child) 根据f 抽象路径名和 child 路径名字符串创建一个新 File 实例. f抽象路径名用于表示目录,child 路径名字符串用于表示目录 ...

  3. 阿里云Centos+Django+Nginx+uWSGI

    针对系统中自带的Python2.7版本 1.安装python-devel yum install python-devel 2.安装uwsgi pip install uwsgi 3.测试uwsgi是 ...

  4. shell脚本新建文件夹或用到目录时多出M或者?之类的

    新建问价加多出? 删除多显示M   建立软连接多\n等 可能是文件兼容问题, 1.首先用vi命令打开文件[root@localhost test]# vi test.sh   2.在vi命令模式中使用 ...

  5. 补发————DOM与BOM

    什么是Dom? DOM是w3c(万维网联盟)的标准. DOM定义了HTML与ML文档的标准: w3c文档对象模型(DOM)是中立于平台与语言的接口,他允许程序和脚本动态访问和更新文档的内容.结构和样式 ...

  6. C#-Database-连接

    using System.Data; using System.Data.SqlClient; //先打开两个类库文件 SqlConnection con = new SqlConnection(); ...

  7. Gigabyte Z170N-WIFI 黑苹果 10.12

    简述 (此文在我的个人博客长期更新)[http://aiellochan.com/2018/02/11/play/Gigabyte-Z170N-WIFI-%E9%BB%91%E8%8B%B9%E6%9 ...

  8. MySQL优化技巧

    目录 MySQL的特点 数据类型优化 整型类型 小数类型 字符串类型 时间类型 主键类型的选择 特殊类型的数据 索引优化 一个使用Hash值创建索引的技巧 前缀索引 多列索引 聚簇索引 覆盖索引 重复 ...

  9. 使用Nginx+Uwsgi部署Python Flask项目

    第一次用Flask做Web(也是第一次用Python做Web),在部署的时候遇到了不少问题,现在将过程就下来,供在这方面也有疑惑的人参考.(PS:使用Apache+mod_wsgi部署模式的可以参考另 ...

  10. eclipse遇到的问题

    引用不了R文件,可能是导包导错了cannot be resolved or is not a field:首先检查你的XML是否保存了,再检查你的import导入的R文件是你包名+R还是android ...