4

在 Prestashop 模块中,我想显示一个选中的复选框。为此,我只是采用了这样的辅助类方法

$display_settings = array(
            'form' => array(
                'legend' => array(
                    'title' => $this->l( 'Display Settings' ),
                    'icon' => 'icon-cogs'
                ),
                'input' => array(
                array(
                    'type' => 'checkbox',
                    'name' => 'display',
                    'values' => array(
                        'query' => array(
                            array(
                                'id' => 'show_header',
                                'name' => $this->l('show header'),
                                'val' => '1',
                                'checked' => 'checked'
                            ),
                        ),
                        'id' => 'id',
                        'name' => 'name'
                    )
                ),
                ),
                'submit' => array(
                    'title' => $this->l( 'Save Display Settings' ),
                    'class' => 'button pull-right',
                    'name' => 'save-main-display-settings',
                )
            ),
        );

但这个只显示checkbow(未选中)。我试图将 val 更改为 0,1。但这对我不起作用。那么有人可以告诉我如何在助手类中选中一个复选框。任何帮助或建议都将是非常可观的。谢谢

4

2 回答 2

4

请删除 'checked' => 'checked' 没有必要。您的代码的其余部分没问题 - 但它只是 FORM 结构定义,如果您想用数据填充它(选中的复选框是数据定义而不是结构),您需要向 HelperForm 提供数据

要选中复选框,请通过以下方式设置它的值:

$helper = new HelperForm();
$helper->fields_value['display_show_header'] = true;

名称“display_show_header”是您的名称“display”和“show_header”的串联,当查看渲染复选框时,您也可以在 firebug 中看到此名称。

于 2014-08-26T18:23:35.170 回答
1

一个完整的例子:

/**
 * Create the form that will be displayed in the configuration of your module.
 */
protected function renderForm()
{
    $helper = new HelperForm();
    // ...
    $helper->tpl_vars = array(
        'fields_value' => $this->getConfigFieldsValues(),
        'languages' => $this->context->controller->getLanguages(),
        'id_language' => $this->context->language->id,
    );

    return $helper->generateForm(array($this->getConfigForm()));
}
/**
 * Helper Function to fill Checkbox Fields with Data
 */
public function getConfigFieldsValues()
{
    $fields_value = array();

    $shop_groups = $this->getShopGroups();
    foreach($shop_groups as $shop_group) {
        $shop_group_id = $shop_group['id_shop_group'];

        $subshops = $this->getSubshops($shop_group['id_shop_group']);
        foreach($subshops as $subshop) {
            $shop_id = $subshop['id_shop'];
            $fields_value['mwdsubshoporderstate_' . $shop_group_id . '_' . $shop_id] = $this->getStatus($shop_id);
        }
    }

    return $fields_value;
}
/**
 * Create the structure of form.
 */
protected function getConfigForm()
{
    $form = array();
    $form_input = array();

    $shop_groups = $this->getShopGroups();
    foreach($shop_groups as $shop_group) {
        $subshops = $this->getSubshops($shop_group['id_shop_group']);

        $form_input[] = array(
            'type'      => 'checkbox',
            'label'     => $this->l($shop_group['name'] . ' :'),
            'desc'   => $this->l(''),
            'name'      => 'mwdsubshoporderstate_' . $shop_group['id_shop_group'],
            'values'    => array(
                'query' => $subshops,
                'id'    => 'id_shop',
                'name'  => 'name',
            )
        );
    }

    $form = array(
        'form' => array(
            'legend' => array(
                'title' => $this->l('Diverse Einstellungen'),
                'icon' => 'icon-cogs',
            ),
            'input' => $form_input,
            'submit' => array(
                'title' => $this->l('Speichern'),
            ),
        ),
    );

    return $form;
}
于 2017-06-27T08:33:44.847 回答