phracktale/symfony-form-tree

创建具有缩进的树形结构选择字段

v1.0.2 2017-02-11 10:47 UTC

This package is not auto-updated.

Last update: 2024-09-27 07:25:48 UTC


README

Build Status

此扩展提供了在Symfony表单中显示Doctrine树实体类型的功能。它为选择列表中的选项名添加了前缀,以指示树级别。

它已过测试,应该与symfony 3.0+兼容

如果想在symfony 2中使用它,请查看此扩展的"symfony-2"分支

<select name="..." data-level-prefix="-">
    <option value="1">Motors</option>
    <option value="2">Electronics</option>
    <option value="3">-Cell phones</option>
    <option value="4">--Samsung</option>
    <option value="5">-Computers</option>
    <option value="6">Fasion</option>
</select>

安装

  1. 使用composer.json

    composer require yavin/symfony-form-tree:^1.0
    
  2. 在您的服务文件中添加类型猜测器(可选)

    <service class="Yavin\Symfony\Form\Type\TreeTypeGuesser">
        <argument type="service" id="doctrine"/>
        <tag name="form.type_guesser"/>
    </service>

    或者如果您使用yml格式

    services:
        symfony.form.type_guesser.tree:
            class: Yavin\Symfony\Form\Type\TreeTypeGuesser
            arguments: [ "@doctrine" ]
            tags:
                - { name: form.type_guesser }
  3. 然后向树模型添加字段。在这个例子中

    use Yavin\Symfony\Form\Type\TreeType;
    
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('category'); //extension will guess field type
    
        //or this is full example with default options:
    
        $builder->add('category', TreeType::class, [
            'class' => Category::class, // tree class
            'levelPrefix' => '-',
            'orderFields' => ['treeLeft' => 'asc'],
            'prefixAttributeName' => 'data-level-prefix',
            'treeLevelField' => 'treeLevel',
        ]);
    }

    此扩展假定在树模型中您有treeLefttreeLevel字段。可以在字段选项中更改。

    这里是示例树实体。

其他

许可证

MIT