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

B站配套视频教程观看

搜索和分页功能的实现

分页功能的实现

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

PAGE_SIZE=1
PAGE_DISPLAY=10

理论上应该展示3页

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

是因爲没有正确的传递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

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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

虚坏叔叔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值