kartik-v / yii2-widget-select2

Enhanced Yii2 wrapper for the Select2 jQuery plugin (sub repo split from yii2-widgets).
http://demos.krajee.com/widget-details/select2
Other
323 stars 145 forks source link

Allow for "id" + 2 fields ("text" and "html") instead of just "id" and "text" fields #238

Closed lmsmartins closed 6 years ago

lmsmartins commented 6 years ago

Prerequisites

Steps to reproduce the issue

  1. Add a value with "Mário" or "João" - The search filter won't work

UPDATE: There isn't any bug on the search filtering by default. The issue was I was trying to add a json string as the "text" element so I could use the formatTemplateResult option and put HTML on the select2 options.

UPDATE 2: Encoding issues when parsing the json - "M\u00e1rio" instead of "Mário".

UPDATE 3: Solved the filtering issue on the PHP encoding: json_encode( $text, JSON_UNESCAPED_UNICODE ), however, search also filters stuff like "

" or "

", etc... Perhaps the data array could allow for a sub array for each element with a "text" and "html" option? The select2 native widget allows this. In this way, we would have a field to present the select options and another field to filter the select options.

Would this be possible to implement?

Expected behavior and actual behavior

When I follow those steps, I see...

When adding HTML to the select options, the search also filters stuff like "div" or "p", etc... Perhaps the data array could allow for a sub array for each element with a "text" and "html" option? See this example of the select2 (https://jsfiddle.net/the94air/awzqtd4w/)

<select style="width: 50%"></select>

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;
   }
});

Environment

Browsers

  • [x] Google Chrome
  • [x] Mozilla Firefox
  • [x] Internet Explorer
  • [x] Safari

Operating System

  • [x] Windows
  • [x] Mac OS X
  • [ ] Linux
  • [ ] Mobile

Libraries

  • jQuery version: 3.2.1
  • yii2-widget-select2 version: 2.1.0

Isolating the problem

  • [ ] This bug happens on the widget demos page
  • [x] The bug happens consistently across all tested browsers
  • [x] This bug happens when using yii2-widget-select2 without other plugins
  • [ ] I can reproduce this bug in a jsbin
lmsmartins commented 6 years ago

UPDATE: There isn't any bug on the search filtering by default. The issue was I was trying to add a json string as the "text" element so I could use the formatTemplateResult option and put HTML on the select2 options.

UPDATE 2: Encoding issues when parsing the json - "M\u00e1rio" instead of "Mário".

Perhaps the data array could allow for a sub array for each element with a "text" and "html" options? The select2 native widget allows this. Would this be possible to be implemented?

lmsmartins commented 6 years ago

When adding HTML to the select options, the search also filters stuff like "div" or "p", etc... Perhaps the data array could allow for a sub array for each element with a "text" and "html" option? See this example of the select2 (https://jsfiddle.net/the94air/awzqtd4w/)

<select style="width: 50%"></select>

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;
   }
});

Would this be possible to implement?

kartik-v commented 6 years ago

Not sure why you are trying those various methods. This is possible by setting the widget options and pluginOptions intelligently to what you need. I would suggest you to try setting pluginOptions['data'] directly to the right associative array for your use case and try:

For example you can set for your use case:

use kartik\select2\Select2;
use yii\web\JsExpression;

echo Select::widget([
    'name' => 'custom-select',
    'data' => [], // empty dropdown options set by default (you can set it to what you need),
    'value' => '', // any initial value
    'pluginOptions' => [
        '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>'
            ],
        ],
        'templateResult' => new JsExpression("function template(data){return data.html}"),
        'escapeMarkup' => new JsExpression("function(m){return m}");
    ]
]);

Closing this. Let know if it solves your use case.

lmsmartins commented 6 years ago

Your suggestion does work. Thanks.

Having the "data" option on the widget, would make one easily believe it would override the pluginOptions's data field, but as you said, these are separate things.