每页动态限制 Knp 分页

Posted

技术标签:

【中文标题】每页动态限制 Knp 分页【英文标题】:Dynamic limit per page Knp Pagination 【发布时间】:2014-03-10 08:18:56 【问题描述】:

我需要使用 Knp Pagination Bundle 动态设置每页记录数的简单解决方案。

我阅读了此页面 records per page allow user to choose - codeigniter pagination 关于动态设置每页限制的内容,我知道我需要在每个向服务器发送请求的项目中使用超链接下拉列表,并且服务器在此请求中使用参数来设置 knp 上的每页限制分页包。但我不确切知道如何在服务器上处理此操作,而且对我来说更难的是如何在与我的查询总数相关的下拉菜单中创建项目。

我的控制器:

public function indexAction()
   
    $page_title = Util::getFormattedPageTitle("Customers");

    $em    = $this->get('doctrine.orm.entity_manager');
    $dql   = "SELECT a FROM CustomersBundle:Customer a WHERE a.enable = 1 ORDER BY a.id";
    //$query = $em->createQuery($dql);
    //$customers = $query->execute();

    $query = $em->createQuery($dql); 
    $paginator  = $this->get('knp_paginator');
    $pagination = $paginator->paginate(
        $query,
        $this->get('request')->query->get('page', 1)/*page number*/,
        3/*limit per page*/
    );


    // parameters to template
    return $this->render('CustomersBundle:Default:index.html.twig', array(
        'page_title' => $page_title,
        'pagination' => $pagination,
        'image_path' => CustomersConstants::$customers_image_thumb_path
    ));

而我的视图代码是:

    % extends '::base.html.twig' %

% block title %
     page_title 
% endblock %

% block body %
    <h1>
         "customers" 
    </h1>
    <br />
    % for customer in pagination %
    <a href=" customer.url " target="_blank">
        <div dir="rtl" class="st_simple_box" id="customercustomer.id" >
            <table cellpadding="0" cellspacing="0" >
                <tr>
                    <td style="vertical-align: top; width: 115px;">
                        % if customer.imageName is not empty %
                            <img class="newsimage" src="asset(image_path) ~ customer.imageName" newsimage" src="asset('images/vakil_default_small.png') ~ customer.imageName" />
                        % endif %
                    </td>
                    <td style="text-align: center;">
                        <p><span style="font-family: Tahoma;">customer.titleFa</span></p>
                    </td>
                    <td style="text-align: justify;">
                        <p><span style="font-family: Tahoma;">customer.descriptionFa</span></p>
                    </td>
                </tr>
            </table>
        </div>
    </a>
    % endfor %
    <div class="navigation">
        <span>
         knp_pagination_render(pagination) 
        </span>
        <!--Here is my drop down html code-->
    </div>
    <br />

% endblock %

**

2014 年 3 月 12 日编辑

   ||||
   ||||
   ||||
  \\\///
   \\//
    \/

有没有办法在 cookie 上设置 MaxItemPerPage 以具有集成变量并使用此变量或在 Knp 分页显示在 twig 文件上时更改它。

答案是正确的,但因为我在许多包中使用分页来在我的实体之间分页,所以我需要一个集成变量来更改所有这些。我使用您的答案并在 Knp Pagination 上自定义 slip.html.twig 以获取“customized_sliding.html.twig”的代码

<div class="ui small pagination menu">
    <div style="clear: both;">
        % if pageCount > 1 %
            <div class="pagination" style="float: right;">
                % if first is defined and current != first %
                    <a href=" path(route, query|merge((pageParameterName): first)) " class="small icon item">
                        <i class="small double angle right icon"></i>
                    </a>
                % endif %

                % if previous is defined %
                    <a href=" path(route, query|merge((pageParameterName): previous)) " class="small icon item">
                        <i class="small angle right icon"></i>
                    </a>
                % endif %

                % for page in pagesInRange %
                    % if page != current %
                        <a href=" path(route, query|merge((pageParameterName): page)) " class="small item">
                             page 
                        </a>
                    % else %
                        <a class="small active item">
                             page 
                        </a>
                    % endif %
                % endfor %

                % if next is defined %
                    <a href=" path(route, query|merge((pageParameterName): next)) " class="small icon item">
                        <i class="small angle left icon"></i>
                    </a>
                % endif %

                % if last is defined and current != last %
                    <a href=" path(route, query|merge((pageParameterName): last)) " class="small icon item">
                        <i class="small double angle left icon"></i>
                    </a>
                % endif %
            </div>
        % endif %
        <div style="float: left;">
            <select name="maxItemPerPage" id="maxItemPerPage">
                <option selected="true" style="display:none;">Number Per Page</option>
                <option id="10">5</option>
                <option id="20">10</option>
                <option id="30">20</option>
            </select>
        </div>
    </div>
    <script type="text/javascript">
        //on select change, you navigate to indexAction and send the parameter maxItemPerPage
        $('#maxItemPerPage').change(function()
            % set currentPath = path(app.request.attributes.get('_route')) %
            var url = "path(app.request.attributes.get('_route'),'maxItemPerPage': '_itemNum')";
            var item = $('#maxItemPerPage').find(":selected").text();
            jQuery(location).attr('href', url.replace('_itemNum',item ));
        );
    </script>
</div>

我想从 cookie 中获取并存储 MaxItemPerPage,因此无需更改所有捆绑代码。 例如在我的控制器中,我不知道必须从 cookie 中获取 $maxItemPerPage

$pagination = $paginator->paginate(
            $query,
            $this->get('request')->query->get('page', 1)/*page number*/,
            $maxItemPerPage/*limit per page*/
        );

我需要通过 javascript 更改 html 标记的值来更改 cookie 上的 maxItemPerPage 的值,并将页面重定向到同一个控制器,而无需将 maxItemPerPage 发送到控制器。

【问题讨论】:

【参考方案1】:

这很容易做到(如果我理解得很好)

public function indexAction($maxItemPerPage=20)
   
    $page_title = Util::getFormattedPageTitle("Customers");

    $em    = $this->get('doctrine.orm.entity_manager');
    $dql   = "SELECT a FROM CustomersBundle:Customer a WHERE a.enable = 1 ORDER BY a.id";
    //$query = $em->createQuery($dql);
    //$customers = $query->execute();

    $query = $em->createQuery($dql); 
    $paginator  = $this->get('knp_paginator');
    $pagination = $paginator->paginate(
        $query,
        $this->get('request')->query->get('page', 1)/*page number*/,
        $maxItemPerPage /*limit per page*/
    );


    // parameters to template
    return $this->render('CustomersBundle:Default:index.html.twig', array(
        'page_title' => $page_title,
        'pagination' => $pagination,
        'image_path' => CustomersConstants::$customers_image_thumb_path
    ));

在视图中

   % extends '::base.html.twig' %

    % block title %
         page_title 
    % endblock %
% block javascript%
<script type="text/javascript">

//on select change, you navigate to indexAction and send the parameter maxItemPerPage
$('#maxItemPerPage').change(function()

var url = 'path('controller_index_route','maxItemPerPage':_itemNum)';
var item = $('#maxItemPerPage').find(":selected").text();

window.location.href = url.replace('_itemNum',item );
)

</script>
    % endblock %

    % block body %
        <h1>
             "customers" 
        </h1>
        <br />
        % for customer in pagination %
        <a href=" customer.url " target="_blank">
            <div dir="rtl" class="st_simple_box" id="customercustomer.id" >
                <table cellpadding="0" cellspacing="0" >
                    <tr>
                        <td style="vertical-align: top; width: 115px;">
                            % if customer.imageName is not empty %
                                <img class="newsimage" src="asset(image_path) ~ customer.imageName" newsimage" src="asset('images/vakil_default_small.png') ~ customer.imageName" />
                            % endif %
                        </td>
                        <td style="text-align: center;">
                            <p><span style="font-family: Tahoma;">customer.titleFa</span></p>
                        </td>
                        <td style="text-align: justify;">
                            <p><span style="font-family: Tahoma;">customer.descriptionFa</span></p>
                        </td>
                    </tr>
                </table>
            </div>
        </a>
        % endfor %
        <div class="navigation">
            <span>
             knp_pagination_render(pagination) 
            </span>
            <!--Here is my drop down html code-->
        </div>
        <br />
<select name="maxItemPerPage" id="maxItemPerPage">
<option id="10">10</option>
<option id="20">20</option>
<option id="30">30</option>
</select>

    % endblock %

【讨论】:

这正是我想要的。谢谢

以上是关于每页动态限制 Knp 分页的主要内容,如果未能解决你的问题,请参考以下文章

PHP分页类及用法

反应表动态页面大小,但有大小限制和分页

如何限制 SHOW TABLES 查询

Django对页面限制的分页显示仍然是最后的结果无休止

使用MySQL的递延Join连接实现高效分页 - Aaron

如何使用 start 和 limit 进行分页