TP-ORM中使用分页时需要自定义分页的html,tp框架中通过provider.php配置中更换分页驱动,webman中怎么更换?

whc

tp中的方法:截图

1181 2 2
2个回答

xiuwang

\think\Paginator::maker('app\common\Bootstrap');

  • whc 2022-09-27

    \think\Paginator::maker(function ($items, $listRows, $currentPage, $total, $simple, $options){
    return new \app\index\Bootstrap($items, $listRows, $currentPage, $total, $simple, $options);
    });

    在调用分页paginate方法前这样设置可以实现,可不可类似tp那样,做一下类的绑定就能相互替换这样的?

  • tj224589 2022-09-27

    要么等thinkorm更新分页参数中提供替换参数,要么就改 webman,在自定义函数文件 实现一个model,其中添加一下这段代码

  • whc 2022-09-27

    好的,谢谢

yangpeng

新建一个自定义查询类

<?php

namespace App\model;

use extend\tools\BearPage;
use think\db\Query;

class MyQuery extends Query
{
    public function pages($params = [])
    {
        $total = $this->count();
        $list = $this->page(get_per_page(), get_per_limit())->select()->toArray();
        $page = BearPage::getPageHtml(get_per_page(), ceil($total / get_per_limit()), $total, request()->path(), array_merge(request()->get(), $params));
        return ['list' => $list, 'page' => $page];
    }
}

在thinkorm.php文件中添加
截图

新建一个分页实现类

<?php

namespace extend\tools;

class BearPage
{
    private static $current_page;
    private static $last_page;
    private static $total;
    private static $url;
    private static $params;

    /**
     * 生成分页代码
     * @param int $current_page 当前页码
     * @param int $last_page 最后一页
     * @param int $total 总条数
     * @param string $url 地址
     * @param array $params 请求参数
     */
    public static function getPageHtml($current_page, $last_page, $total, $url, $params)
    {
        self::$current_page = $current_page;
        self::$last_page = $last_page;
        self::$total = $total;
        self::$url = $url;
        unset($params['_pjax']);
        self::$params = $params;
        return self::render();
    }

    /**
     * 上一页按钮
     * @param string $text
     * @return string
     */
    private static function getPreviousButton($text = "&laquo;")
    {
        if (self::$current_page == 1) {
            return self::getDisabledTextWrapper($text);
        }
        self::$params['page'] = self::$current_page - 1;
        return self::getAvailablePageWrapper(self::$url . '?' . http_build_query(self::$params), $text);
    }

    /**
     * 下一页按钮
     * @param string $text
     * @return string
     */
    private static function getNextButton($text = '&raquo;')
    {
        if (self::$current_page == self::$last_page) {
            return self::getDisabledTextWrapper($text);
        }
        self::$params['page'] = self::$current_page + 1;
        return self::getAvailablePageWrapper(self::$url . '?' . http_build_query(self::$params), $text);
    }

    /**
     * 页码按钮
     * @return string
     */
    private static function getLinks()
    {
        $block = [
            'first' => null,
            'slider' => null,
            'last' => null,
        ];

        $side = 3;
        $window = $side * 2;

        if (self::$last_page < $window + 6) {
            $block['first'] = self::getUrlRange(1, self::$last_page);
        } elseif (self::$current_page <= $window) {
            $block['first'] = self::getUrlRange(1, $window + 2);
            $block['last'] = self::getUrlRange(self::$last_page - 1, self::$last_page);
        } elseif (self::$current_page > (self::$last_page - $window)) {
            $block['first'] = self::getUrlRange(1, 2);
            $block['last'] = self::getUrlRange(self::$last_page - ($window + 2), self::$last_page);
        } else {
            $block['first'] = self::getUrlRange(1, 2);
            $block['slider'] = self::getUrlRange(self::$current_page - $side, self::$current_page + $side);
            $block['last'] = self::getUrlRange(self::$last_page - 1, self::$last_page);
        }

        $html = '';

        if (is_array($block['first'])) {
            $html .= self::getUrlLinks($block['first']);
        }

        if (is_array($block['slider'])) {
            $html .= self::getDots();
            $html .= self::getUrlLinks($block['slider']);
        }

        if (is_array($block['last'])) {
            $html .= self::getDots();
            $html .= self::getUrlLinks($block['last']);
        }

        return $html;
    }

    private static function getUrlRange($first_page, $last_page)
    {
        $urls = [];
        for ($i = $first_page; $i <= $last_page; $i++) {
            self::$params['page'] = $i;
            $urls[$i] = self::$url . '?' . http_build_query(self::$params);
        }
        return $urls;
    }

    /**
     * 渲染分页html
     * @return mixed
     */
    private static function render()
    {
        $options = [10, 20, 30, 50, 100];
        $option = '';
        $limit = get_per_limit();
        foreach ($options as $item) {
            if ($item == $limit) {
                $option .= '<option value="' . $item . '" selected>' . $item . '</option>';
            } else {
                $option .= '<option value="' . $item . '">' . $item . '</option>';
            }
        }
        $html = '
            <div class="box-footer">
                <label class="control-label pull-left" style="margin-right: 10px; font-weight: 100;">
                    <small>共&nbsp;%s&nbsp;条记录</small>&nbsp;
                    <small>每页显示</small>&nbsp;
                    <select class="input-sm" onchange="changePerLimit(this)">%s</select>&nbsp;
                    <small>条记录</small>
                </label>
                <ul class="pagination pagination-sm no-margin pull-left">%s %s %s</ul>
            </div>';

        return sprintf(
            $html,
            self::$total,
            $option,
            self::getPreviousButton(),
            self::getLinks(),
            self::getNextButton()
        );
    }

    /**
     * 生成一个可点击的按钮
     *
     * @param string $url
     * @param int $page
     * @return string
     */
    private static function getAvailablePageWrapper($url, $page)
    {
        return '<li><a data-pjax href="' . htmlentities($url) . '">' . $page . '</a></li>';
    }

    /**
     * 生成一个禁用的按钮
     *
     * @param string $text
     * @return string
     */
    private static function getDisabledTextWrapper($text)
    {
        return '<li class="disabled"><span>' . $text . '</span></li>';
    }

    /**
     * 生成一个激活的按钮
     *
     * @param string $text
     * @return string
     */
    private static function getActivePageWrapper($text)
    {
        return '<li class="active"><span>' . $text . '</span></li>';
    }

    /**
     * 生成省略号按钮
     *
     * @return string
     */
    private static function getDots()
    {
        return self::getDisabledTextWrapper('...');
    }

    /**
     * 批量生成页码按钮.
     *
     * @param array $urls
     * @return string
     */
    private static function getUrlLinks(array $urls)
    {
        $html = '';

        foreach ($urls as $page => $url) {
            $html .= self::getPageLinkWrapper($url, $page);
        }

        return $html;
    }

    /**
     * 生成普通页码按钮
     *
     * @param string $url
     * @param int $page
     * @return string
     */
    private static function getPageLinkWrapper($url, $page)
    {
        if (self::$current_page == $page) {
            return self::getActivePageWrapper($page);
        }
        return self::getAvailablePageWrapper($url, $page);
    }
}
年代过于久远,无法发表回答
×
🔝