2

就像在如何处理包含 500 多个项目的 Symfony 表单集合中一样,我有一个很大的 symfony 表单集合。在提到的线程中,有人建议对集合使用分页。

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

表单类型是这样的

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

我的控制器生成一个包含集合的表单

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

然后我尝试使用分页器

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

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

4

2 回答 2

3

解决方案:查询集合的所有实体,对这些实体进行分页,并将集合的数据属性设置为分页值。

控制器:

$allEntities = $em->getRepository('MyBundle:Entity')->findAll();
$paginator = $this->get('knp_paginator');
$pagination = $paginator->paginate(
    $allEntities,
    $request->get('page', 1),
    10
);

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

表格类型:

private $data;

public function __construct($data) {
    $this->data = $data;
}

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->add('myType', 'collection', array(
            'type' => new MyType(),
            'data' => $this->data,
        ))
    ;
}
于 2015-02-25T16:24:49.953 回答
3

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 %}

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

于 2017-06-20T08:33:20.670 回答