How to create lists in symfony?

I am new to the symfony platform. I would like to create a list of entries that would look like this:

enter image description here

I need some filters at the top, a list of items in the middle and pagination at the bottom. The list must support both editable and read mode. In read-only mode, the user can simply view the data in edit mode, he will be able to update values ​​in several fields and columns.

Since I will be doing a lot of these lists, I would like to use an interface like Forms in Symfony2, instead of constantly tweaking the branch templates.

Am I missing some Symfony Forms features that could create lists like this? Is there any other general way to implement this? Can you give me some hints that form related classes to expand support for lists?

+5
source share
2 answers

To turn Symfony forms into lists, I created a new type of type ListType, which accepts an arbitrary type of nested collection. This way I can create lists with different columns. It will look something like this:

class ListType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { if (empty($options['collection_type']) || !$options['collection_type'] instanceof AbstractType) { throw new \InvalidArgumentException(); } $builder->add('rows', 'collection', array('type' => $options['collection_type'])); $builder->add('save', 'submit', array('label' => 'Save')); } ... } 

I will use the $ options array to provide or enable and disable other functions, such as a search filter and pagination.

To have a convenient interface for displaying lists, for example:

 {{ list(list) }} 

I created a Twig extension:

 class ListExtension extends \Twig_Extension { public function getFunctions() { return array( new \Twig_SimpleFunction('list', array($this, 'listFunction'), array('is_safe' => array('html'), 'needs_environment' => true)) ); } public function listFunction(\Twig_Environment $env, FormView $form) { return $env->resolveTemplate($this->defaultTemplate)->renderBlock('list', array('form' => $form)); } ... } 

It displays a β€œlist” of Twig block. I will also add other subblocks equally. This extension provides greater feed than mold blocks.

I registered the list extension as a service:

 # app/config/services.yml services: my.twig.list.extension: class: MyBundle\Twig\Extension\ListExtension arguments: ["::my_theme.html.twig"] public: false tags: - { name: twig.extension } 

Now all I have to do is create a form of my ListType type and pass it an array of string entities.

Thanks to Elias Van Ootegem for pointing me in the right direction.

+2
source

APYdataGridBundle is the perfect solution for this, there

+1
source

All Articles