一、前端代码

  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. JS prototype chaining(原型链)整理中······

    初学原型链整理 构造器(constructor).原型(prototype).实例(instance); 每一个构造器都有一个prototype对象,这个prototype对象有一个指针指向该构造器: ...

  2. Spring——事务

    Spring事务 事务的ACID特性 原子性(Atomicity):在事务中的操作,要么都执行,要么都不执行! 一致性(Consistency):数据从一种状态,同时到达另一种状态. 持久性(Dura ...

  3. 【Solidity】学习(2)

    address 地址类型 40个16进制数,160位 地址包括合约地址和账户地址 payable 合约充值 balance,指的是当前地址的账户value,单位是wei this指的是当前合约的地址 ...

  4. Shader_ShaderForge_NGUI_序列帧/

    序列帧 Shader篇 Shader Forge序列帧算法! 附上Shader代码部分: // Shader created with Shader Forge v1.26 // Shader For ...

  5. JavaScript视频分享,学无止境。

    http://blog.sina.com.cn/s/blog_735ed39a0102voy3.html

  6. Batch_Size 详解

    Batch_Size(批尺寸)是机器学习中一个重要参数,涉及诸多矛盾,下面逐一展开. 首先,为什么需要有 Batch_Size 这个参数? Batch 的选择,首先决定的是下降的方向.如果数据集比较小 ...

  7. Azure Sphere–“Object reference not set to an instance of an object” 解决办法

    在开发Azure Sphere应用时,如果出现项目无法编译,出现“Object reference not set to an instance of an object”时,必须从下面两个方面进行检 ...

  8. [转]kaldi基于GMM做分类问题

    转自:http://blog.csdn.net/zjm750617105/article/details/55211992 对于每个类别的GMM有几种思路: 第一是将所有训练数据按类别分开,每类的数据 ...

  9. 通过脚本下载GO被墙或常用的相关包

    脚本描述 脚本依赖环境:Windows,GO,GIT 脚本将创建 temp 目录,并拷贝相关包到第一个 GOPATH 中 可将脚本保存到本地自行添加被墙或者常用的包 完整脚本代码 @echo off ...

  10. [深入学习Web安全](11)之XSS玩法

    [深入学习Web安全](11)之XSS玩法 本文转自:i春秋社区 前言这篇paper,我们将学习如何优雅的调戏XSS.我们会教大家一些不常用的,但很实用的XSS姿势.我们在正式进入主题之前,先来说一下 ...