在HABTM复选框旁边显示图像 [英] Display Image next to HABTM checkboxes
问题描述
甘蔗有人请给我一个想法!
我从一个与另一个具有HABTM关系关联的表相关的表中生成多个复选框。
我想生成带有图像以及标签中文本的多个复选框。
我的两个表是项目和items_characteristics。因此,一个项目具有HasAndBelongToMany特性,而一个项目具有特性HasAndBelongToMany Items。
echo $ this-> Form-> input('Item.ItemCharacteristic',array(
'label '=> false,
'type'=>'select',
'multiple'=>'checkbox',
'options'=> $ itemCharacteristics,
'selected'=> $ this-> HTML-> value('ItemCharacteristic.ItemCharacteristic')
));
此代码可正确生成复选框列表,并且可以正常运行:
这就是我有:
这是我想拥有的:
< img src = https://i.stack.imgur.com/mBRrW.png alt =在此处输入图片描述>
任何人都不知道如何我可以实现吗?
我假设在您的控制器中您执行了以下操作:
$ this-> request-> data = $ this-> Item-> find('first',...);
,以便 $ data
包含有关
编辑:我还假设 Item
habtm ItemCharacteristic
然后在您看来
$ checked_characteristics = Hash :: extract($ this-> data,'ItemCharacteristic。{n} .id');
foreach($ itemCharacteristics as $ id => $ itemCharacteristic)
{
$ checked = in_array($ id,$ checked_characteristics);
$ img = $ this-> HTML-> image(’cake.icon.png’); //将要显示的图标的名称
//放在此处
//根据特征
//您正在显示
echo $ this-> Form- > input(
'ItemCharacteristic.ItemCharacteristic。',
array(
'between'=> $ img,
'label'=> $ itemCharacteristic,
'value'=> $ id,
'type'=>'checkbox',
'checked'=> $ checked
)
);
}
编辑:从您的评论中我了解到 $ itemCharacteristics
来自 find('list'
)语句。
将其更改为 find('all',array('recursive'=> -1));
现在您的代码变成
foreach($ itemCharacteristics as $ itemCharacteristic)
{
$ id = $ itemCharacteristic ['ItemCharacteristic ']['ID'];
$ icon_name = $ itemCharacteristic ['ItemCharacteristic'] ['icon_name']; //或在任何位置获得图标路径
$ img = $ this-> Html-> image($ icon_name);
$ itemCharacteristicName = $ itemCharacteristic ['ItemCharacteristic'] ['name'];
//与
以上相同}
Cane Somebody give me an ideas on this Please!
I Generate multiple checkboxes from a table that is related with another table with HABTM relationship association. I would like to generate multiple checkboxes with an image along with the text in the label.
My two tables are items and items_characteristics. So an Item HasAndBelongToMany characteristics, and an ItemCharacteristic HasAndBelongToMany Items.
echo $this->Form->input('Item.ItemCharacteristic',array(
'label' =>false,
'type'=>'select',
'multiple'=>'checkbox',
'options' => $itemCharacteristics ,
'selected' => $this->Html->value('ItemCharacteristic.ItemCharacteristic')
));
This code generate the list of the checkboxes properly and works perfect: This is what i have:
Which is generated from DB from the table items_characteristics.
And this is what i wanna have:
Does Anyone have any Idea how i can achieve this Please?
I assume that in your controller you did something like:
$this->request->data = $this->Item->find('first', ... );
so that $data
contains the information about selected characteristics in form of a subarray,
edit: I also assume that Item
habtm ItemCharacteristic
then in your view
$checked_characteristics = Hash::extract($this->data, 'ItemCharacteristic.{n}.id');
foreach($itemCharacteristics as $id => $itemCharacteristic )
{
$checked = in_array($id, $checked_characteristics );
$img = $this->Html->image('cake.icon.png'); // put here the name
// of the icon you want to show
// based on the charateristic
// you are displayng
echo $this->Form->input(
'ItemCharacteristic.ItemCharacteristic.',
array(
'between' => $img,
'label' => $itemCharacteristic,
'value' => $id,
'type' => 'checkbox',
'checked' => $checked
)
);
}
edit: from your comment I understand that $itemCharacteristics
come from a find('list'
) statement.
change it into a find('all', array('recursive' => -1));
now your code becomes
foreach($itemCharacteristics as $itemCharacteristic )
{
$id = $itemCharacteristic['ItemCharacteristic']['id'];
$icon_name = $itemCharacteristic['ItemCharacteristic']['icon_name']; //or wherever you get your icon path
$img = $this->Html->image($icon_name);
$itemCharacteristicName = $itemCharacteristic['ItemCharacteristic']['name'];
// same as above
}
这篇关于在HABTM复选框旁边显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!