【问题标题】:prestashop show helper form checkbox checkedprestashop 显示助手表单复选框已选中
【发布时间】:2014-08-19 08:45:05
【问题描述】:

在 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。但这对我不起作用。那么有人可以告诉我如何在助手类中选中一个复选框。任何帮助或建议都将是非常可观的。谢谢

【问题讨论】:

    标签: php html-helper prestashop prestashop-1.6


    【解决方案1】:

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

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

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

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

    【讨论】:

    • 注意:字符串"1" / "0" 也可以作为true / false =>选中/未选中
    【解决方案2】:

    一个完整的例子:

    /**
     * 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;
    }
    

    【讨论】:

      猜你喜欢
      • 2015-03-19
      • 2013-09-09
      • 1970-01-01
      • 2019-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多