每页动态限制 Knp 分页 [英] Dynamic limit per page Knp Pagination

查看:20
本文介绍了每页动态限制 Knp 分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我阅读了这个页面 每页记录允许用户选择 - codeigniter pagination 关于动态设置每页限制,我知道我需要在每个项目中添加一个带有超链接的下拉列表,该下拉列表将请求发送到服务器,服务器使用此请求上的参数来设置 knp 分页包上的每页限制.但我不知道如何在服务器上处理这些操作,而且我更难的是如何在与查询总数相关的下拉列表中创建项目.

我的控制器:

公共函数 indexAction(){$page_title = Util::getFormattedPageTitle("客户");$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($查询,$this->get('request')->query->get('page', 1)/*页码*/,3/*每页限制*/);//模板参数return $this->render('CustomersBundle:Default:index.html.twig', array('page_title' =>$page_title,'分页' =>$分页,'图像路径' =>CustomerConstants::$customers_image_thumb_path));}

我的视图代码是:

 {% extends '::base.html.twig' %}{% 块标题 %}{{ 页面标题 }}{% 结束块 %}{% 块体 %}<h1>{{ 顾客" }}<br/>{% 用于分页中的客户 %}<a href="{{ customer.url }}" target="_blank"><div dir="rtl" class="st_simple_box" id="customer{{customer.id}}" ><table cellpadding="0" cellspacing="0" width="100%"><tr><td style="vertical-align: top; width: 115px;">{% 如果 customer.imageName 不为空 %}<img class="newsimage" src="{{asset(image_path) ~ customer.imageName}}" alt="无图片/>{% 别的 %}<img class="newsimage" src="{{asset('images/vakil_default_small.png') ~ customer.imageName}}" alt="无图片"/>{% 万一 %}</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>

</a>{% 结束为 %}<div class="导航"><跨度>{{ knp_pagination_render(分页) }}</span><!--这是我的下拉 html 代码-->

<br/>{% 结束块 %}

**

2014 年 3 月 12 日编辑

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

有什么方法可以在 cookie 上设置 MaxItemPerPage 以具有集成变量并使用此变量或在树枝文件上显示 Knp 分页时更改它.

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

<div style="clear: both;">{% if pageCount >1%}<div class="pagination" style="float: right;">{% if first 已定义且当前 != first %}<a href="{{ path(route, query|merge({(pageParameterName): first})) }}" class="小图标项"><i class="小双角右图标"></i></a>{% 万一 %}{% 如果先前已定义 %}<a href="{{ path(route, query|merge({(pageParameterName): previous})) }}" class="小图标项"><i class="小角度右图标"></i></a>{% 万一 %}{% 为 pagesInRange %} 中的页面{% if page != current %}<a href="{{ path(route, query|merge({(pageParameterName): page})) }}" class="small item">{{ 页 }}</a>{% 别的 %}<a class="小活动项目">{{ 页 }}</a>{% 万一 %}{% 结束为 %}{% 如果定义了下一个 %}<a href="{{ path(route, query|merge({(pageParameterName): next})) }}" class="小图标项"><i class="左小角图标"></i></a>{% 万一 %}{% if last 已定义且当前 != last %}<a href="{{ path(route, query|merge({(pageParameterName): last})) }}" class="小图标项目"><i class="小双角左图标"></i></a>{% 万一 %}

{% 万一 %}<div style="float: left;"><select name="maxItemPerPage" id="maxItemPerPage"><option selected="true" style="display:none;">每页数量</option><option id="10">5</option><option id="20">10</option><option id="30">20</option></选择>

<script type="text/javascript">//在选择更改时,导航到 indexAction 并发送参数 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 ));});

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

$pagination = $paginator->pagination($查询,$this->get('request')->query->get('page', 1)/*页码*/,$maxItemPerPage/*每页限制*/);

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

解决方案

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

公共函数 indexAction($maxItemPerPage=20){$page_title = Util::getFormattedPageTitle("客户");$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($查询,$this->get('request')->query->get('page', 1)/*页码*/,$maxItemPerPage/*每页限制*/);//模板参数return $this->render('CustomersBundle:Default:index.html.twig', array('page_title' =>$page_title,'分页' =>$分页,'图像路径' =>CustomerConstants::$customers_image_thumb_path));}

在视图中

 {% extends '::base.html.twig' %}{% 块标题 %}{{ 页面标题 }}{% 结束块 %}{% 阻止 javascript%}<script type="text/javascript">//在选择更改时,导航到 indexAction 并发送参数 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 );}){% 结束块 %}{% 块体 %}<h1>{{ 顾客" }}<br/>{% 用于分页中的客户 %}<a href="{{ customer.url }}" target="_blank"><div dir="rtl" class="st_simple_box" id="customer{{customer.id}}" ><table cellpadding="0" cellspacing="0" width="100%"><tr><td style="vertical-align: top; width: 115px;">{% 如果 customer.imageName 不为空 %}<img class="newsimage" src="{{asset(image_path) ~ customer.imageName}}" alt="无图片/>{% 别的 %}<img class="newsimage" src="{{asset('images/vakil_default_small.png') ~ customer.imageName}}" alt="无图片"/>{% 万一 %}</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>

</a>{% 结束为 %}<div class="导航"><跨度>{{ knp_pagination_render(分页) }}</span><!--这是我的下拉 html 代码-->

<br/><select name="maxItemPerPage" id="maxItemPerPage"><option id="10">10</option><option id="20">20</option><option id="30">30</option></选择>{% 结束块 %}

I need straightforward solution for dynamically set number of records per page with Knp Pagination Bundle.

I read the this page records per page allow user to choose - codeigniter pagination about dynamically set per page limits and I know I need a drop down with hyperlink inside each item that send a request to server and server use parameter on this request to set limit per page on knp pagination bundle. But I don't know exactly how to handle this actions on server and also and more harder to me how to create items on drop down related to the total number on my query.

my controller:

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
    ));
}

And my view code is:

    {% 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="customer{{customer.id}}" >
            <table cellpadding="0" cellspacing="0" width="100%">
                <tr>
                    <td style="vertical-align: top; width: 115px;">
                        {% if customer.imageName is not empty %}
                            <img class="newsimage" src="{{asset(image_path) ~ customer.imageName}}" alt="No Image/>
                        {% else %}
                            <img class="newsimage" src="{{asset('images/vakil_default_small.png') ~ customer.imageName}}" alt="No Image"/>
                        {% 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 %}

**

Edited on 12 March 2014

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

Is there any way to set MaxItemPerPage on cookie to have a integrated variable and use this variable or change it while Knp Pagination is showed up on twig file.

the answer was correct but because of I used pagination in many bundles to paginate between my entities so I need an integrate variable to change all of them. I use your answer and customize sliding.html.twig on Knp Pagination to reach this code for "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>

I want to fetch and store MaxItemPerPage from cookie so there is no need to change all bundles code. For example in my controller I don't know have to fetch the $maxItemPerPage from cookie

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

And I need change the value of maxItemPerPage on cookie by changing the value of html tag by javascript and redirect the page to same controller and no need to send maxItemPerPage to controller.

解决方案

This can be done easily (if i understood well)

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
    ));
}

in the view

   {% 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="customer{{customer.id}}" >
                <table cellpadding="0" cellspacing="0" width="100%">
                    <tr>
                        <td style="vertical-align: top; width: 115px;">
                            {% if customer.imageName is not empty %}
                                <img class="newsimage" src="{{asset(image_path) ~ customer.imageName}}" alt="No Image/>
                            {% else %}
                                <img class="newsimage" src="{{asset('images/vakil_default_small.png') ~ customer.imageName}}" alt="No Image"/>
                            {% 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 分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
PHP最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆