【问题标题】:Custom pagination in OctoberCMS十月CMS中的自定义分页
【发布时间】:2018-05-24 12:38:30
【问题描述】:

我有一个代码可以从表单中查询:

Painting::where('type',input("type"))->where('material',input("material"))->whereHas('artist', function($q)
{
     $q->where('artist_slug', '=', $this->param('slug'));
})->paginate(15);

如何使用页码列表进行自定义分页?或者也许动态加载

【问题讨论】:

    标签: laravel pagination octobercms


    【解决方案1】:

    查看RainLab Blog Plugin,它有一个很好的例子,另请参阅here

    但是,如果您想添加 URL 友好分页 paintings/2,这里有一个更完整的示例,其中 2 是页码和/或处理 URL 参数 paintings/2?type=something&material=something

    在您的绘画模型中添加一个用于在前端列出绘画的范围:

    public function scopeListPaintings($query, $options)
        {
            extract(array_merge([
                'page'               =>  1,
                'perPage'            =>  30,
                'material'           =>  null,
                'type'               =>  null,
                'artistSlug'         =>  null,
            ], $options));
    
    
            if( !empty($artistSlug) ){
                $query->whereHas('artist', function($q)
                {
                    $q->where('artist_slug', $artistSlug );
                });
            }
    
            if( !empty($material) ){
                $query->where( 'material' , $material)
            }
    
            if( !empty($type) ){
                $query->where( 'type' , $type)
            }
    
    
            return $query->paginate( $perPage, $page );
    
        }
    

    然后在你的绘画组件中定义属性并调用之前的作用域;

        public $paintings ;
    
        public $pageNumber;
    
        public $perPage; 
    
        public $urlParams;
    
        public function defineProperties()
        {
            return [
                'pageNumber' => [
                    'title'       => 'Page #',
                    'description' => 'Paintings Page #',
                    'type'        => 'string',
                    'default'     => '{{ :page }}',
                ],
                'perPage' => [
                    'title'       => 'Paintings per page',
                    'type'        => 'string',
                    'default'     => '30',
                ]
    
                .. ect make sure to add it to your page markup
            ];
    
        }
    
        private function propertyOrParam($name, $default = null)
        {
            $value = $this->property($name, $default);
            if (substr($value, 0, 1) == ':')
                return $this->param($value, $default);
    
            return $value;
        }
    
        public function getPaintings()
        {
            /** Pagination */
            $this->pageNumber    = $this->propertyOrParam('pageNumber') ;
            $this->perPage       = $this->propertyOrParam('perPage'); 
    
            /** Url Params if exist */ 
            $params = Request::query()
            $this->page['urlParams']   =  $this->urlParams  =  !empty( $params ) ? http_build_query( $params ) : null ;
    
            return (new Painting)->ListPaintings([
                    'page'          => $this->pageNumber, 
                    'perPage'       => $this->perPage,
                    'type'          => input('type'),
                    'material'      => input('material'),
                    'artistSlug'    => $this->propertyOrParam('slug')
                ]);
        }
    
        public function onRun()
        {
    
            $this->page['paintings'] = $this->paintings   = $this->getPaintings() ;
    
            // Redirect to Last page if page # not found in request
            if ($this->pageNumber >  $this->paintings->lastPage() && $this->pageNumber > 1){
                return Redirect::to($this->currentPageUrl([ $this->paramName('pageNumber') =>  $this->paintings->lastPage().$this->urlParams ]));
            }
    
        }
    

    然后你可以在你的主题中创建一个全局部分来处理分页 - 你可以在你的网站上重复使用它 - 添加以下 sn-p (借用 Forum Plugin );

    分页.htm

        {% set paginationEnabled =
        records.currentPage > 1 or
        records.lastPage > 1 or
        records.lastPage > records.currentPage
        %}
    
        {% if paginationEnabled %}
        {# How many pages to display around the current page #}
        {% set n = 2 %}
    
        {% set currentPageZeroBased = records.currentPage-1 %}
    
        {% set pageLinks = [] %}
        {% set pageSet = [] %}
    
        {% set startOffset = max(currentPageZeroBased - n, 0) %}
        {% if (startOffset + 2*n+1) > (records.lastPage-1) %}
        {% set startOffset = max(records.lastPage - 2*n - 1, 0) %}
        {% endif %}
    
        {% for page in 1..records.lastPage %}
        {% set pageLinks = pageLinks|merge([page]) %}
        {% endfor %}
    
        {% set activeBlock = pageLinks|slice(startOffset, 2*n + 1) %}
    
        {% if startOffset > 0 %}
        {% set pageSet = pageSet|merge([1]) %}
    
        {% if startOffset > 1 %}
        {% set pageSet = pageSet|merge(['...']) %}
        {% endif %}
        {% endif %}
    
        {% set pageSet = pageSet|merge(activeBlock) %}
    
        {% set diffToEnd = (records.lastPage-1) - (startOffset + 2*n+1) + 1 %}
    
        {% if diffToEnd > 0 %}
        {% if diffToEnd > 1 %}
        {% set pageSet = pageSet|merge(['...']) %}
        {% endif %}
    
        {% set pageSet = pageSet|merge([records.lastPage]) %}
        {% endif %}
    
    
        <div>
            <div>
               <div>
    
                   <div>
                       <span>Records&nbsp;&nbsp;<b>{{records.firstItem|trim}}&nbsp;-&nbsp;{{records.lastItem|trim}}</b>&nbsp;Of&nbsp;{{ records.total|number_format(0, '.', ',')}}</span>
                       <span>Page {{ records.currentPage }} of {{ records.lastPage }}</span>
                   </div>
    
               </div>
            </div>
            <div>
                <ul>
    
                    {% if records.currentPage > 1 %}
                    <li>
                        <a href="{{ this.page.baseFileName|page( { page : (records.currentPage-1) ~ urlParams }) }}" title="Previous"><i class="fa fa-angle-left"></i></a>
                    </li>
                    {% endif %}
    
                    {% for page in pageSet %}
                    {% if page == '...' %}
                    <li>
                        <a href="javascript:void(0)" type="button" class="disabled">{{ page }}</a>
                    </li>
                    {% else %}
                    <li class="{{ page == records.currentPage ? 'active' }}">
                        <a href="{{ this.page.baseFileName|page({ page : page ~ urlParams })  }}">{{ page }}</a>
                    </li>
                    {% endif %}
                    {% endfor %}
    
                    {% if records.lastPage > records.currentPage %}
                    <li>
                        <a href="{{ this.page.baseFileName|page({ page : (records.currentPage+1) ~ urlParams }) }}" title="Next"><i class="fa fa-angle-right"></i></a>
                    </li>
                    {% endif %}
                </ul>
            </div>
        </div>
    
    
        {% endif %}
    

    然后在你的页面或组件中;

    {% for painting in paintings %}
     ....
    {% endfor %}
    
    // Add the pagination
    
    {% partial "pagination" records=paintings  %}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-27
      • 2016-05-18
      • 2018-11-26
      • 2022-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多