前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >CRM客户关系管理系统(六) 第六章、排序和搜索功能开发

CRM客户关系管理系统(六) 第六章、排序和搜索功能开发

作者头像
zhang_derek
发布2018-05-30 14:33:36
9020
发布2018-05-30 14:33:36
举报
文章被收录于专栏:有趣的django

第六章、排序和搜索功能开发

 6.1.排序功能开发

(1)kingadmin_tags.py

代码语言:javascript
复制
@register.simple_tag
def get_sorted_column(column,sorted_column,forloop):
    '''排序'''
    if column in sorted_column:    #如果这一列被排序了
        #要判断上一次排序是按什么顺序,本次取反
        last_sort_index = sorted_column[column]
        if last_sort_index.startswith('-'):
            #利用切片,去掉‘-’
            this_time_sort_index = last_sort_index.strip('-')
        else:
            #加上 '-'
            this_time_sort_index = '-%s'% last_sort_index
        return this_time_sort_index
    else:
        return forloop

(2)kingadmin/views.py

代码语言:javascript
复制
def get_orderby_result(request,querysets,admin_class):
    '''排序'''

    current_ordered_column = {}
    #通过前端获取到要排序的字段的索引(是个字符串)
    orderby_index = request.GET.get('_o')
    if orderby_index:
        #通过索引找到要排序的字段,因为索引有可能是负数也有可能是负数,要用绝对值,否则负值的时候取到了其它字段了
        orderby_key = admin_class.list_display[abs(int(orderby_index))]
        #记录下当前是按什么排序字段的
        current_ordered_column[orderby_key] = orderby_index
        if orderby_index.startswith('-'):
            orderby_key = '-' + orderby_key

        return querysets.order_by(orderby_key),current_ordered_column
    else:
        return querysets,current_ordered_column

(3)table_obj_list.html

代码语言:javascript
复制
<th><a href="?_o={% get_sorted_column column sorted_column forloop.counter0 %}">{{ column }}</a></th>

 (4)添加正序倒序的图标

Boorstrap组件:https://v3.bootcss.com/components/

把bootstrap的fonts静态文件放到kingadmin/staic/fonts下面

(5)kingadmin_tags.py

代码语言:javascript
复制
@register.simple_tag
def render_sorted_arrow(column,sorted_column):
    '''排序的图标'''

    if column in sorted_column:
        last_sort_index = sorted_column[column]
        if last_sort_index.startswith('-'):
            arrow_direction = 'bottom'

        else:
            arrow_direction = 'top'
        ele = '''<span class="glyphicon glyphicon-triangle-%s" aria-hidden="true"></span>'''% arrow_direction
        return mark_safe(ele)

    return ''

(6)table_obj_list.html

代码语言:javascript
复制
<th><a href="?_o={% get_sorted_column column sorted_column forloop.counter0 %}">
                                {{ column }}{% render_sorted_arrow column sorted_column %}
                            </a></th>

 效果:

6.2.分页、排序和过滤组合使用

(1)排序和过滤组合

table_obj_list.html

 (2)kingamdin_tags.py

代码语言:javascript
复制
@register.simple_tag
def render_filtered_args(admin_class):
    '''拼接过滤的字段'''
    if admin_class.filter_conditions:
        ele = ''
        for k,v in admin_class.filter_conditions.items():
            ele += '&%s=%s'%(k,v)
        return mark_safe(ele)
    else:
        return ''

现在过滤和排序的组合没有问题,但是分页还没有组合到一起

 (3)过滤和分页组合

table_obj_list.html

 kingadmin_tags.py

def render_paginator先添加一个参数admin_class

(4)分页、排序、过滤组合

table_obj_list.py

 kingadmin_tag.py

代码语言:javascript
复制
@register.simple_tag
def get_current_sorted_column_index(sorted_column):
    #三元运算,如果为True执行左边的,为False,执行右边的('')
    return list(sorted_column.values())[0] if sorted_column else ''

 table_obj_list.py

 kingadmin_tag.py

现在排序、过滤和分页组合就没有问题了

6.3.搜索功能开发

全局搜索 

(1)table_obj_list.html

 (2)kingadmin/views.py

代码语言:javascript
复制
from django.db.models import Q

def get_searched_result(request,querysets,admin_class):
    '''搜索'''

    search_key = request.GET.get('_q')
    if search_key:
        q = Q()
        q.connector = 'OR'

        for search_field in admin_class.search_fields:
            q.children.append(("%s__contains"%search_field,search_key))

        return querysets.filter(q)
    return querysets

现在实现的是全局搜索功能(不能过滤的同时搜索), 下面添加 过滤+搜索的功能

过滤+搜索

 只需要添加一个隐藏标签就可以

kingadmin/vies.py

 table_obj_list.html

 效果:

功能优化

(1)用户并不知道可以通过哪些字段去搜索,在搜索框里添加提示(placeholder)

代码语言:javascript
复制
 <form action="">
            <input type="search" placeholder="{% for s in admin_class.search_fields %}{{ s }},{% endfor %}" name="_q" value="{{ admin_class.search_key }}">
            <input type="submit" value="Search">
            {% for k,v in admin_class.filter_conditions.items %}
                <input type="hidden" name="{{ k }}" value="{{ v }}">
            {% endfor %}
        </form>

 (2)添加Bootstrap样式

 过滤字段提示和美化

table_obj_list.html

“过滤”按钮

kingadmin_tags.py

过滤字段提示+添加框的美化样式

末尾要加</div>闭合

显示效果:

代码已同步github  num6 排序功能开发;分页、排序、筛选组合使用;搜索功能开发

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018-04-30 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 第六章、排序和搜索功能开发
    •  6.1.排序功能开发
      • 6.2.分页、排序和过滤组合使用
        • 6.3.搜索功能开发
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档