Python flask实战订餐系统微信小程序-27搜索和分页功能的实现

wechat 虚幻 662℃ 0评论

搜索和分页功能的实现

分页功能的实现

修改配置文件,总的展现数为1,

PAGE_SIZE=1
PAGE_DISPLAY=10

理论上应该展示3页

当我们点击尾页的时候,这里报错了

43b368bd9ee7d13b0f527c3440fd2ec2 - Python flask实战订餐系统微信小程序-27搜索和分页功能的实现

是因爲没有正确的传递p进去,而且需要下次传递时,将上一次置空

@route_account.route( "/index" )
def index():
    resp_data = {}
    req = request.values
    page = int(req['p'])if ('p' in req and req['p']) else 1
    query = User.query
    page_params = {
        'total':query.count(),
        'page_size':app.config['PAGE_SIZE'],
        'page' :page,
        'display':app.config['PAGE_DISPLAY'],
        'url':request.full_path.replace("&p={}".format(page),"")
    }

我们将默认值改回去

搜索功能的实现

添加js方法,點擊搜索提交

account文件夹下 添加index.js

;
var account_index_ops = {
    init:function () {
        this.eventBind();
    },
    eventBind:function () {

        $(".wrap_search .search").click(function(){
            $(".wrap_search").submit()
        });
    }
}

$(document).ready(function () {
    account_index_ops.init()
})

模板里加载js

18c3edf51d873335a3a6bafdfe4e8d18 - Python flask实战订餐系统微信小程序-27搜索和分页功能的实现

account.py添加接口調整

# -*- coding: utf-8 -*-
from flask import Blueprint,request,redirect,jsonify
from common.libs.Helper import ops_render,iPagination,getCurrentDate
from common.models.User import User
from common.libs.UrlManager import UrlManager
from common.libs.user.UserService import UserService
from application import app,db
from sqlalchemy import or_

route_account = Blueprint( 'account_page',__name__ )

@route_account.route( "/index" )
def index():
    resp_data = {}
    req = request.values
    page = int(req['p'])if ('p' in req and req['p']) else 1
    query = User.query

    if 'mix_kw' in req:
        rule = or_(User.nickname.ilike("%{0}%".format(req['mix_kw'])), User.mobile.ilike("%{0}%".format(req['mix_kw'])))
        query = query.filter(rule)

    if 'status' in req and int(req['status'])>-1:
        query = query.filter(User.status == int(req['status']))

    page_params = {
        'total':query.count(),
        'page_size':app.config['PAGE_SIZE'],
        'page' :page,
        'display':app.config['PAGE_DISPLAY'],
        'url':request.full_path.replace("&p={}".format(page),"")
    }

默认保留搜索页面

Account.py

    resp_data[ 'list'] = list
    resp_data[ 'pages'] = pages
    resp_data[ 'search_con'] = req

    return ops_render( "account/index.html" , resp_data)

index.html

                <div class="form-group">
                    <div class="input-group">
                        <input type="text" name="mix_kw" placeholder="请输入姓名或者手机号码" class="form-control" value="{{search_con['mix_kw']}}">
                        <input type="hidden" name="p" value="{{search_con['p']}}">
                        <span class="input-group-btn">

账号状态保存

base_setting.py

STATUS_MAPPING = {
    "1":"正常",
    "0":"已删除"
}

Account.py

    resp_data[ 'list'] = list
    resp_data[ 'pages'] = pages
    resp_data[ 'search_con'] = req
    resp_data[ 'status_mapping' ] = app.config['STATUS_MAPPING']
    return ops_render( "account/index.html" , resp_data)

index.html

<div class="row m-t p-w-m">
    <div class="form-group">
        <select name="status" class="form-control inline">
            <option value="-1">请选择状态</option>
            {% for tmp_key in status_mapping %}
                <option value="{{tmp_key}}" {% if tmp_key == search_con['status'] %} selected {% endif %}>{{status_mapping[tmp_key]}}</option>
            {% endfor %}
        </select>
    </div>

转载请注明:虚坏叔叔 » Python flask实战订餐系统微信小程序-27搜索和分页功能的实现

喜欢 (6)

您必须 登录 才能发表评论!