【问题标题】:Yii 2 Select2 kartik with custom html带有自定义 html 的 Yii 2 Select2 kartik
【发布时间】:2017-11-18 19:14:41
【问题描述】:

如何将它翻译成 Yii2 select2 kartik 小部件?我认为 select2 kartik 小部件中的“数据”属性只允许 id -> text。有没有办法使用小部件完成这项工作?

var data = [{
   id: 0,
   text: 'enhancement',
    html: '<div style="color:green">enhancement</div>'
}, {
   id: 1,
   text: 'bug',
    html: '<div style="color:red">bug</div><div><small>This is some small text on a new line</small></div>'
}];

function template(data) {
    return data.html;
}

$("select").select2({
   data: data,
   templateResult: template,
   escapeMarkup: function(m) {
      return m;
   }
});

【问题讨论】:

  • 您找到了解决方案还是仍在寻找解决方案?
  • 是的,请看下面我的回答。

标签: yii2 jquery-select2


【解决方案1】:

解决方案是使用“pluginOptions”数组中的“data”属性:

echo $form->field($model, 'id_customer')->label(false)->widget(Select2::classname(), [
    'data' => [],
    'options' => ['placeholder' => Yii::t('app', 'Select a customer')],
    'pluginOptions' => [
        'allowClear' => true,
        'data' => $customerList,
        'escapeMarkup' => new JsExpression('function (markup) { return markup; }'),
        'templateResult' => new JsExpression('formatTemplateResult'),
        'templateSelection' => new JsExpression('formatSelection'),
    ],
]);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-10-16
    • 1970-01-01
    • 1970-01-01
    • 2014-08-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-29
    • 1970-01-01
    相关资源
    最近更新 更多