First off, while it is possible to specify background colours for
<option> elements in css, not all browsers will honour your request
and will display them normally.
Anyway, the select options passed to the form helper can be in 2
formats. The first, the simple keyed id => label array, is sufficient
for most use cases. However there is another, where your array is NOT
keyed, but each entry must be an array that contains at least the
'value' and 'name' keys. All other keys will be used for attributes on
the <option> element. This is useful for disabling an option, for
example.
So this:
$options = array(
666 => 'The number of the beast',
667 => 'The neighbour of the beast'
);
is the same as this:
$options = array(
array('value' => 666, 'name' => 'The number of the beast'),
array('value' => 667, 'name' => 'The neighbour of the beast'),
);
You just need to change your view code to this:
// ---------
$colourOptions = array();
foreach ($colours as $id => $hex) {
$colourOptions[] = array(
'value' => $id,
'name' => $hex,
'style' => 'background-color: ' . $hex
);
}
echo $form->input('Colour.id', array('options' => $colourOptions,
'multiple' => true));
// ---------
To be honest though; even in the browsers that do support this it
looks pretty crappy. If you want better-looking select boxes, try an
unobtrusive javascript replacement like elSelect (mootools,
http://www.cult-f.net/2007/12/14/elselect/).
Also, I've always found that the standard multiple-select boxes in
html are very annoying. Try using checkboxes ('multiple' =>
'checkbox') or an unobtrusive script like
http://c82.net/samples/checklist-samples.html
or http://www.frequency-decoder.com/demo/alternative-selectlist-widgit/.
hth
grigri
On Nov 19, 12:18 pm, Hipnotik <[EMAIL PROTECTED]> wrote:
> Hi
> I would like to generate form field <select> with multiple option with
> each row with other background colour. Rows colours should be taken
> from database. Example table schema:
>
> id
> name
> background_colour // in example: #f0f0f0
>
> #controller code
> $colours = $this->Colour->find('list');
> $this->set("colours", $colours);
>
> #view code
> // form initialization code ...
> echo $form->input("Colour.id",
> array(
> "type"=>"select",
> "options"=>$colours,
> "multiple"=>true)
> );
>
> How to set different colours for each row?
>
> Thanks for help
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"CakePHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---