Yii2:在分页器上呈现页面大小选择器的简便方法

问题描述

是否有任何简单且本地化的方法将每个页面选择器的html呈现添加到ListView的分页器上?我看了看文档:

  1. Yii widget listview
  2. Yii data pagination
  3. Yii widgets linkpager

找到了我需要的一切,除了渲染页面大小选择器。这有点奇怪,因为这是非常常见的功能

解决方法

没有本地方法可以执行此操作,您可以创建自己的方法或使用此方法:

在模型过滤器中定义:

    class YOUR_CLASS_FILTER extend YOUR_MODEL
       
    
        ...
           public $pagesize; // Property pagesize.
           const ITEMS_PER_PAGE_INIT = 12; // Initial items per page
           ...
        
           //Add or edit this in the same model `search` method:
           public function search($params){    
           ...
           $dataProvider = new ActiveDataProvider([
                    'query' => $query,'pagination' => [
                        // this $params['pagesize'] is an id of dropdown list that we set in view file
                        'pagesize' => ($this->pagesize) ? $this->pagesize : self::ITEMS_PER_PAGE_INIT,],]);
           ...
           }    
           ... 
           public function rules()
            {
                return [
                    ...
                    [['pagesize'],'integer'],...
                ];
            }
            ...
    /**
     * Return Static Array of elements per page
     * @return array
     */
    public static function itemsPerPage()
    {
        return array(
            12 => 12,24 => 24,48 => 48
        );
    }
}

这是我在视图文件中实现的方式:

<?= $form->field($filter,'pagesize')->dropDownList(
    $filter->itemsPerPage(),array(
      'id' => 'pagesize','class' => 'form-control','onchange' => 'this.form.submit()',))->label("Items per page: ")
?>
,

创建一个包含指向当前页面链接的菜单,并使用不同的每页选项作为GET参数:

import six

import pandas as pd
import numpy as np
def render_mpl_table (data,col_width=1.0,row_height=0.625,font_size=8,header_color='#40466e',row_colors=['#f1f1f2','w'],edge_color='w',bbox=[0,1,1],header_columns=0,ax=None,**kwargs):
if ax is None:
    size = (np.array (data.shape [::-1]) + np.array ([0,1])) * np.array ([col_width,row_height])
    fig,ax = plt.subplots (figsize=size)
    ax.axis ('off')

mpl_table = ax.table (cellText=data.values,bbox=bbox,colLabels=data.columns,**kwargs)

mpl_table.auto_set_font_size (False)
mpl_table.set_fontsize (font_size)

for k,cell in six.iteritems (mpl_table._cells):
    cell.set_edgecolor (edge_color)
    if k [0] == 0 or k [1] < header_columns:
        cell.set_text_props (weight='bold',color='w')
        cell.set_facecolor (header_color)
    else:
        cell.set_facecolor (row_colors [k [0] % len (row_colors)])
return ax


df = pd.DataFrame ({'Label': ['a','a','b','c','d','d'],'Month': ['Type  1','Type  1','Type 2','Type 3','Type 2'],'Activity': ['cc','cc','tt','bb',]})
tt = pd.crosstab (index=[df ['Activity']],columns=[df ['Label'],df ['Month']],margins=True)


tab_me = render_mpl_table (tt,col_width=2.0)
plt.show ()