select 选项中的附加属性

Additional attributes in select options

在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>

父名称值来自另一个 select。同样的方法生成。

您必须进行一些更改。

SubcategoriesTable 中,您必须检索 parent 的名称(我使用 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',
    ],
]); 

Re-reading 你的问题,我看到你需要另一个值 select。我建议你添加 parent 的 ID 而不是 parent 的名称,这样会更容易处理。

例如:

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')); ?>

结果:

[]

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