简体   繁体   English

结果中带有图标的Select2

[英]Select2 With Icon in Result

I'm trying to make a dropdown select2 in Yii framework, and using select2 v.3.5.2, I include the following snippet of code that I have tried. 我试图在Yii框架中创建一个下拉select2,并使用select2 v.3.5.2,其中包括以下我尝试过的代码段。

This Import Select2 Ext. 此Import Select2 Ext。

<?php Yii::import('ext.select2.Select2'); ?>

This Select2 Form : 此Select2表格:

<?php
echo $form->labelEx($model,'icon_mark');
echo Select2::activeDropDownList($model, 'icon_mark', array(), array('class' => 'form-control'));
echo $form->error($model,'icon_mark');
?>

This Html data (load in javascript) : 此HTML数据(以javascript加载):

<option value="euro">euro</option>
<option value="asterisk">asterisk</option>
<option value="plus">plus</option>

And This Javascript : 和这个Javascript:

$.ajax({
    url: "<?php echo CController::createUrl('site/loadkelurahan') ?>",
    type: 'POST',
    data: {},
    success: function (data) {
        $('#<?php echo CHtml::activeId($model, 'icon_mark') ?>').html(data);
        $('#' + '<?php echo CHtml::activeId($model, 'icon_mark') ?>').select2().select2('val', '');
        if ("<?php echo $ismode ?>" == "edit") {
            $('#' + '<?php echo CHtml::activeId($model, 'icon_mark') ?>').select2().select2('val', '<?php echo $model->icon_mark?>');
        }
    },
    error: function (jqXHR, status, err) {
        alert(err);
    }
});

with a combination of the above script select2 running normally, then I want to ask here how to add a template for select2? 结合上述脚本select2正常运行,那么我想问一下如何为select2添加模板? because I want the dropdown select2 own icon respectively. 因为我要下拉菜单分别选择自己的图标。 There anything you can give an example of properly. 有什么您可以适当举例说明的。 thanks. 谢谢。

I did the same thing but in Yii2, basically you need to do the same, tweaking the rendering functionality of the select2 widget 我做了同样的事情,但是在Yii2中,基本上你需要做同样的事情,调整select2小部件的渲染功能。

Here is my code from yii2: 这是我来自yii2的代码:

            <?php $format = new JsExpression(
                "function format(icon) {
                                return '<i class=\"'+ icon.id +'\" aria-hidden=\"true\"></i> '  + icon.text.replace('-', ' ');
                            }"
            );
            ?>
            <?= \kartik\select2\Select2::widget(
                [
                    'name'          => 'MenuItem[' . $item->id . '][icon]',
                    'id'            => 'menu-item-icon-' . $item->id,
                    'data'          => array_merge(['none' => 'None'], \rmrevin\yii\fontawesome\FA::getConstants()),
                    'value'         => $item->icon,
                    'pluginOptions' => [
                        'templateResult'    => $format,
                        'templateSelection' => $format,
                        'escapeMarkup'      => new JsExpression("function(m) { return m; }"),
                        'allowClear'        => false,
                    ],
                ]
            ); ?>

In the templateResult and templateSelection callback you need to render you icons. 在templateResult和templateSelection回调中,您需要呈现图标。 And in the escapeMarkup just return the parameter, to ensure that the template isn't escaped 并在escapeMarkup中仅返回参数,以确保不对模板进行转义

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM