我有一个小问题。我想在我的 Kartik-Select2 小部件的列表项中添加一个跨度 HTML 标记。这个想法是我的数据库中有一些颜色,我想在列表中显示你可以选择什么颜色。不仅仅是nem。但我使用引导颜色和其他颜色,我有两列(color_code 和 class)。我希望你明白我想用这个实现什么。
所以我尝试创建一个匿名函数来构建列表项并在跨度之后,但在返回语句中这将是一个字符串,PHP 编译器无法解析这是一个 HTML 代码。
所以我的问题是,我该怎么做?因为在回报声明中我可以回馈一件事。所以我不能归还名字和标签。所以请帮助我。
我的颜色表是:
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
我的代码是这样的:
$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,
]);
我希望你能写好,我想要什么。对不起,我的英语不是最好的:)