Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

initValueText does not work #66

Closed
nagyt234 opened this issue Sep 11, 2015 · 5 comments
Closed

initValueText does not work #66

nagyt234 opened this issue Sep 11, 2015 · 5 comments

Comments

@nagyt234
Copy link

I have the following code in view:

echo $form->field($searchModel, 'ingredient_id')->widget(Select2::classname(), [
    'initValueText' => $ingredientName,
    'options' => [
        'placeholder' => Module::t('app','Select an ingredient ...'),
    ],
    'pluginOptions' => [
        'allowClear' => true,
        'minimumInputLength' => 3,
        'ajax' => [
            'url' => $ajaxURL,
            'dataType' => 'json',
            'data' => new JsExpression('function(params) { return {q:params.term}; }')
        ],
        'escapeMarkup' => new JsExpression('function (markup) { return markup; }'),
        'templateResult' => new JsExpression('function(ingredient) { return ingredient.name; }'),
        'templateSelection' => new JsExpression('function(ingredient) { return ingredient.name; }'),
    ],
]);

The select2 input works well, however the initial value does not appear. I've debugged yii2-widget-select2/Select2.php and I don't see any problem. $this->data is set correctly to [ id, value] in line 130 in method init. What can be the problem?

Also $input in function renderInput seems to be OK:

<select id="ingredientnutrientsearch-ingredient_id" class="form-control" name="IngredientNutrientSearch[ingredient_id]" data-krajee-select2="select2_9e5dc55a" style="display:none"> <option value="">Wähle einen Zutat...</option> <option value="525" selected>Babyfood, cereal, rice, with honey, prepared with whole milk</option> </select>

And also the value 525 what does not appear on the screen is submitted, if I sbmit the form.

The version of the package was first 2.0.2. 2.0.3 is not propagated to the composer :-(. But I've tried also with 2.0.3, the symptom is the same.

@kartik-v
Copy link
Owner

You may need to debug your code and compare with the demos.

The latest release of the widget is higher than the versions you have.

@nagyt234
Copy link
Author

The problem was, that using of 'text' as attribute name is somehow hard coded, i.e. using ingredient.text instead of ingredient.name in templateSelection solved the issue.

@pctin
Copy link

pctin commented Jul 11, 2016

Yes, I have problem too, so how can we customize this?

Example: I have only an api to search, but each form I want use attribute 'name' or 'email' ... to display??

@jbryu
Copy link

jbryu commented Jan 27, 2017

I solved this issue by using processResults option in ajax. I think this issue belongs to Select2, but not this Yii extension.

   echo $form->field($model, 'creator_id')->widget(Select2::classname(), [
        'value' => $model->creator_id,
        'initValueText' => isset($model->creator) ? $model->creator->username : null,
        'options' => ['placeholder' => 'Search for User...'],
        'pluginOptions' => [
            'multiple' => false,
            'allowClear' => true,
            'minimumInputLength' => 2,
            'language' => [
                'errorLoading' => new JsExpression("function () { return 'Waiting for results...'; }"),
            ],
            'ajax' => [
                'url' => \yii\helpers\Url::to(['user/select2']),
                'dataType' => 'json',
                'data' => new JsExpression('function(params) { return {q:params.term}; }'),
                'processResults' => new JsExpression('
                    function (data, params) {
                        return {
                            results: $.map(data, function (item) {
                                return {
                                    id: item.id,
                                    text: item.username,
                                };
                            }),
                            pagination: false,
                        };
                    }
                '),
            ],
            'escapeMarkup' => new JsExpression('function (markup) { return markup; }'),
            'templateResult' => new JsExpression('function(item) { return item.text; }'),
            'templateSelection' => new JsExpression('function (item) { return item.text; }'),
        ],
    ]);

@seraos
Copy link

seraos commented Aug 23, 2017

try:
'templateSelection' => new JsExpression('function(ingredient) { return ingredient.name ? ingredient.name : ingredient.text; }'),
or:
'templateSelection' => new JsExpression('function (item) { return item.username ? item.username : item.text; }'),

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants