du4629 2016-03-03 07:46
浏览 116

Yii2 - Select2跨度HTML标签插入到下拉列表

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 :)

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
    • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
    • ¥16 mybatis的代理对象无法通过@Autowired装填
    • ¥15 可见光定位matlab仿真
    • ¥15 arduino 四自由度机械臂
    • ¥15 wordpress 产品图片 GIF 没法显示
    • ¥15 求三国群英传pl国战时间的修改方法
    • ¥15 matlab代码代写,需写出详细代码,代价私
    • ¥15 ROS系统搭建请教(跨境电商用途)
    • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。