在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。但它对我没有用。那么有人可以告诉我如何在帮助程序类中选中复选框。任何帮助或建议都将非常适合。感谢
答案 0 :(得分:4)
请删除'已选中' => '检查'没有必要。其余的代码是可以的 - 但它只是FORM结构定义,如果你想用数据填充它(选中复选框是数据定义而不是结构)你需要向HelperForm提供数据。
要选中复选框,请按以下方式设置值:
$helper = new HelperForm();
$helper->fields_value['display_show_header'] = true;
名称" display_show_header"是你的名字的连接"显示"和#34; show_header",在查看渲染复选框时,您还可以在firebug中看到此名称。
答案 1 :(得分: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;
}