5-crm项目-kingadmin,列表页---过滤

 

展示客户列表页面--------过滤

过滤
这里面使用了很巧妙的设计思路
使用了很多我不知道的方法,比如:
field_obj = admin_class.model._meta.get_field(filter_field)
field_obj.get_choices()[1:]
总体的思路是
1,把过滤字段进行配置
2,把admin_class传递到前端
3,把过滤条件传递到前端,
4,渲染页面过滤元素,这个时候就是枚举值的问题,外键的问题,时间的问题,选中的问题,

 

 

###

第一步:views

@login_required
def display_table_objs(request,app_name,table_name):

    print("-->",app_name,table_name)  # 这是通过url取到的,
    #models_module = importlib.import_module('%s.models'%(app_name))
    #model_obj = getattr(models_module,table_name)
    admin_class = king_admin.enabled_admins[app_name][table_name]
    #admin_class = king_admin.enabled_admins[crm][userprofile]

    if request.method == "POST": #action 来了

        print(request.POST)
        selected_ids = request.POST.get("selected_ids")
        action = request.POST.get("action")
        if selected_ids:
            selected_objs = admin_class.model.objects.filter(id__in=selected_ids.split(','))
        else:
            raise KeyError("No object selected.")
        if hasattr(admin_class,action):
            action_func = getattr(admin_class,action)
            request._admin_action = action
            return action_func(admin_class,request,selected_objs)

    #object_list = admin_class.model.objects.all()
    object_list,filter_condtions = table_filter(request,admin_class) #过滤后的结果

    object_list = table_search(request,admin_class,object_list)  # 查询后的结果

    object_list,orderby_key = table_sort(request, admin_class, object_list) #排序后的结果
    print("orderby key ", orderby_key)
    paginator = Paginator(object_list, admin_class.list_per_page)  # 分页

    page = request.GET.get('page')
    try:
        query_sets = paginator.page(page)
    except PageNotAnInteger:
        # If page is not an integer, deliver first page.
        query_sets = paginator.page(1)
    except EmptyPage:
        # If page is out of range (e.g. 9999), deliver last page of results.
        query_sets = paginator.page(paginator.num_pages)

    return render(request,"king_admin/table_objs.html",{"admin_class":admin_class,----->把这个给前端,然后可以用来获取到有哪些过滤字段
                                                        "query_sets":query_sets,
                                                        "filter_condtions":filter_condtions,
                                                        "orderby_key":orderby_key,
                                                        "previous_orderby": request.GET.get("o",''),
                                                        "search_text":request.GET.get('_q','')})

对应的table_filter

def table_filter(request,admin_class):
    '''进行条件过滤并返回过滤后的数据'''
    filter_conditions = {}
    keywords = ['page','o','_q']
    for k,v in request.GET.items():
        if k in keywords:#保留的分页关键字 and 排序关键字
            continue
        if v:
            filter_conditions[k] =v
    print("filter coditions",filter_conditions)

    return admin_class.model.objects.filter(**filter_conditions).\
               order_by("-%s" % admin_class.ordering if admin_class.ordering else  "-id"),\
               filter_conditions

 

第2步:tag

@register.simple_tag
def render_filter_ele(filter_field,admin_class,filter_condtions):
    #select_ele = '''<select class="form-control" name='%s' ><option value=''>----</option>''' %filter_field
    select_ele = '''<select class="form-control" name='{filter_field}' ><option value=''>----</option>'''
    field_obj = admin_class.model._meta.get_field(filter_field)  ---->处理了筛选项的三种情况,枚举值的,外键的,日期的
    if field_obj.choices:
        selected = ''
        for choice_item in field_obj.choices:
            print("choice",choice_item,filter_condtions.get(filter_field),type(filter_condtions.get(filter_field)))
            if filter_condtions.get(filter_field) == str(choice_item[0]):
                selected ="selected" ------>如果过滤条件中有这个项,就选中

            select_ele += '''<option value='%s' %s>%s</option>''' %(choice_item[0],selected,choice_item[1])
            selected =''

    if type(field_obj).__name__ == "ForeignKey":
        selected = ''
        for choice_item in field_obj.get_choices()[1:]:  ----->这个get_choices方法不知道,
            if filter_condtions.get(filter_field) == str(choice_item[0]):   ---->filter_condtions.get(filter_field),字典,get,根据键取值,
                selected = "selected"
            select_ele += '''<option value='%s' %s>%s</option>''' %(choice_item[0],selected,choice_item[1])
            selected = ''
    if type(field_obj).__name__ in ['DateTimeField','DateField']:
        date_els = []
        today_ele = datetime.now().date()
        date_els.append(['今天', datetime.now().date()])
        date_els.append(["昨天",today_ele - timedelta(days=1)])
        date_els.append(["近7天",today_ele - timedelta(days=7)])
        date_els.append(["本月",today_ele.replace(day=1)])
        date_els.append(["近30天",today_ele - timedelta(days=30)])
        date_els.append(["近90天",today_ele - timedelta(days=90)])
        date_els.append(["近180天",today_ele - timedelta(days=180)])
        date_els.append(["本年",today_ele.replace(month=1,day=1)])
        date_els.append(["近一年",today_ele  - timedelta(days=365)])

        selected = ''
        for item in date_els:
            select_ele += '''<option value='%s' %s>%s</option>''' %(item[1],selected,item[0])


        filter_field_name = "%s__gte" % filter_field

    else:
        filter_field_name = filter_field
    select_ele += "</select>"
    select_ele = select_ele.format(filter_field=filter_field_name)

    return mark_safe(select_ele)

 

第三步:html

 {% for filter_field in admin_class.list_filters %}
                    <div class="col-lg-2">
                    <span>{{ filter_field }}</span>   ----->这是展示这个过滤字段是什么
                    {% render_filter_ele filter_field admin_class filter_condtions %}   ----->这是在把那些字段都渲染成为html代码
                    </div>   ------>每一个过滤字段就是一个div

                  {% endfor %}

                    <button type="SUBMIT" class="btn btn-success">检索</button>

 

 

 

###

posted @ 2020-08-10 18:28  技术改变命运Andy  阅读(122)  评论(0编辑  收藏  举报