i had a little problem. I want to add a span HTML tag into my Kartik-Select2 widget's list item. The idea is that i have some colors in my db, and i want to show in the list what color can you choose. Not just the nem. But i use bootstrap colors and other color, that i have two columns (color_code and class). I hope you understand what i want tho achieve with this.
So i tried to make an anonymus function to build the listitem and after the span, but in the return statement this will be a string, and the PHP compiler cant resolve that is an HTML code.
So my question is, how can i do this? Because in return statement i can give back one thing. So i cant give back the name and the tag. So help me if you can pls.
My Color table is that:
color_id PK int
type varchar - the color type (bootstrap, hex color etc.) for grouping
name varchar - the color name
color_code varchar - if the color is hex i store the hex code in here
class varchar - if its a bootstrap color i store the class name in here
My code is this:
$colors = Color::find();
if(!empty($this->types))
$colors->andWhere(['type' => $this->types]);
return Select2::widget([
'name' => $this->name,
'value' => $this->value,
'model' => $this->model,
'attribute' => $this->attribute,
'data' => ArrayHelper::map(
$colors->all(),
'color_id',
// 'name',
function ($array, $default){
/** @var $array Color */
if( !isset($array->color_code) && isset($array->class) ) {
return $array->name
. ' <span class="'
. $array->class
. '"></span>';
} elseif ( isset($array->color_code) && !isset($array->class) ) {
return $array->name
. ' <span style="background-color: '
. $array->color_code
. '"></span>';
}
},
'type'
),
'options' => $this->options,
]);
I hope you could write down good, what I want. Sory my english is not the best :)