如何将其翻译成Yii2 select2 kartik小部件?我认为select2 kartik小部件中的“data”属性仅允许id - >文本。有没有办法使用小部件完成这项工作?
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;
}
});
答案 0 :(得分: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'),
],
]);