【问题标题】:Additional attributes in select options选择选项中的附加属性
【发布时间】:2020-11-13 03:35:07
【问题描述】:

在 createForm.php 我有代码:

$this->add([
        'type' => Element\Select::class,
        'name' => 'subcategory_id',
        'options' =>[
                'label' => 'Subcategory',
                'empty_option' => 'Select...',
                'value_options' => $subcategoriesTable->fetchAllSubcategories(),
        ],
        'attributes' => [
                'required' => false,
                'class' => 'custom-select',
        ],
]);

在 SubcategoriesTable.php 中

public function fetchAllSubcategories()
{
        $sqlQuery = $this->sql->select()->order('sub_name ASC');
        $sqlStmt = $this->sql->prepareStatementForSqlObject($sqlQuery);
        $handler = $sqlStmt->execute();

        $row = [];

        foreach($handler as $tuple){
                $row[$tuple['subcategory_id']] = $tuple['sub_name'];
        }
        return $row;
}

并从我的数据库生成记录到我的表单:

<option value="1">Name1</option>

如何更改我的代码以制作这样的附加属性?

<option value="1" data-chained="parent_name">Name1</option>

父名称值来自另一个选择。也是用同样的方法生成的。

【问题讨论】:

    标签: php forms select laminas


    【解决方案1】:

    您必须进行一些更改。

    SubcategoriesTable 中,您必须检索父级的名称(我使用parent_name 作为键,因为我们不知道确切的列名称是什么......):

    public function fetchAllSubcategories()
    {
        $sqlQuery = $this->sql->select()->order('sub_name ASC');
        $sqlStmt = $this->sql->prepareStatementForSqlObject($sqlQuery);
        $handler = $sqlStmt->execute();
        
        $row = [];
        
        foreach($handler as $tuple){
            $row[$tuple['subcategory_id']] = [
                'sub_name' => $tuple['sub_name'],
                'parent_name' => $tuple['parent_name']
            ];
        }
        return $row;
    }
    

    然后,在CreateForm

    $valueOptions = [];
    foreach($subcategoriesTable->fetchAllSubcategories() as $subcategoryId => $subcategory){
        $valueOptions[] = [
            'value' => $subcategoryId,
            'label' => $subcategory['sub_name'],
            'attributes' => [
                'data-chained' => $subcategory['parent_name']
            ]
        ];
    }
            
    $this->add([
        'type' => Element\Select::class,
        'name' => 'subcategory_id',
        'options' =>[
            'label' => 'Subcategory',
            'empty_option' => 'Select...',
            'value_options' => $valueOptions,
        ],
        'attributes' => [
            'required' => false,
            'class' => 'custom-select',
        ],
    ]); 
    

    重新阅读您的问题,我发现您需要此值才能进行另一个选择。我建议你添加父母的ID而不是父母的名字,这样会更容易处理。

    例如:

    public function formAction() {
        $subcagetoryForm = new Form();
        $subcagetoryForm->add([
            'type' => Select::class,
            'name' => 'subcategory_id',
            'options' => [
                'label' => 'Subcategory',
                'empty_option' => 'Select...',
                'value_options' => [
                    [
                        'value' => 1,
                        'label' => 'Name 1',
                        'attributes' => [
                            'data-chained' => 'parent 1'
                        ]
                    ],
                    [
                        'value' => 2,
                        'label' => 'Name 2',
                        'attributes' => [
                            'data-chained' => 'parent 2'
                        ]
                    ]
                ],
            ],
            'attributes' => [
                'required' => false,
                'class' => 'custom-select',
            ],
        ]);
        return [
            'subcagetoryForm' => $subcagetoryForm
        ];
    }
    

    在视图中:

    <?= $this->formElement($this->subcagetoryForm->get('subcategory_id')); ?>
    

    结果:

    []

    但是,我建议您创建一个自定义元素以从表单中删除所有这些逻辑。如果您需要在另一个表单中使用自定义元素,它将很有帮助。你可以看看this question

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-20
      • 1970-01-01
      • 2012-07-02
      • 2013-05-22
      • 2016-12-14
      相关资源
      最近更新 更多