使用KnpPaginatorBundle分页Symfony表单集合 [英] Pagination of a Symfony form collection with KnpPaginatorBundle

查看:121
本文介绍了使用KnpPaginatorBundle分页Symfony表单集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

就像如何处理500多种Symfony表单集合一样项目我有一个很大的symfony表单集合.在提到的主题中,有人建议使用分页进行收集.

like in How to handle Symfony form collection with 500+ items i have got a big symfony form collection. In the mentioned thread someone suggests to use pagination for the collection.

如何使用knpPaginatorBundle对这样的表单集合进行分页?

How can i paginate such a form collection with the knpPaginatorBundle?

表单类型是这样的

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->add('myType', 'collection', array(
            'type' => new MyType(),
        ))
    ;
}

我的控制器使用集合生成一个表单

My controller generates a form with the collection

$form = $this->createForm(new MyFormCollectionType());

然后我正在尝试使用分页器

and then i'm trying to use the paginator

$paginator = $this->get('knp_paginator');
$pagination = $paginator->paginate(
    $form['myType'],
    $request->get('page', 1),
    10
);

我认为我为paginate($ target)使用了错误的目标,symfony表单集合中正确的目标是什么?

I think i'm using the wrong target for paginate($target), whats the correct target in a symfony form collection?

推荐答案

Symfony3上的示例:

Example on Symfony3 :

目录控制器

// I want to paginate products (my collection)
$products = $catalog->getProducts();   // ArrayCollection

// Pagination
$paginator  = $this->get('knp_paginator');
$pagination = $paginator->paginate($products, $request->query->getInt('page', 1), 10);

// build the form with catalog data
$form = $this->createForm(CatalogProductType::class, $catalog, array("pagination" => $pagination));

// [...]

// show page
return $this->render('catalogs/products.html.twig', array(
    'catalog'    => $catalog,
    'form'       => $form->createView(),
    'pagination' => $pagination
));

目录表单(CatalogProductType)

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('products',   CollectionType::class, array(
            'entry_type'    => ProductType::class,
            'data'          => $options['pagination'],
            'allow_add'     => true,
            'allow_delete'  => true,
            'prototype'     => true,
            'label'         => false,
            'by_reference'  => false
        ))
        ->add('save', SubmitType::class, array(
            'attr'    => array('class' => 'button-link save'),
            'label'   => 'Validate'
        ))
    ;
}

public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults(array(
        'data_class'    => 'AppBundle\Entity\Catalog',
        'pagination'    => null    // Don't forget this !
    ));
}

我的观点(products.html.twig)

<div class="navigation">{{ knp_pagination_render(pagination) }}</div>

{% if pagination.getTotalItemCount > 0 %}
    {% for widget in form.products.children %}
        {# Refers to a Twig macro #}
        {{ _self.prototype_product(widget) }}
    {% endfor %}
{% else %}
    <div class="error">The is no product in this catalog</div>
{% endif %}

我的产品集合现在已分页.

My products collection is now paginated.

这篇关于使用KnpPaginatorBundle分页Symfony表单集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆