If the JSON data is using some different structure (as using an ActiveQuery to query data), and JSON result contains something like:
{"results":[{"id":"ID1","description":"DESCRIPTION 1"},{"id":"ID2","description":"DESCRIPTION 2"}, ...
The result is that initialValueText "is not correctly used" and an empty string is assigned (when the value assigned to the attribute is selected an "undefined" is returned).
That's because on initial settings the "templateSelection" model has a "text" value initialized and not a "description" initialized.
using this solves the problem:
'templateSelection' => new JsExpression('function (model) { if (model.description!=undefined) return model.description; else return model.text; }'),
It would be nice to have some setting which would simply rename the "text" and also the "id" value used for the Select2 (something like displayValue ...)
hope it's clear enough :-)
---
Want to back this issue? **[Post a bounty on it!](https://www.bountysource.com/issues/44952018-initvaluetext-if-json-result-has-element-text-it-is-not-working-as-expected?utm_campaign=plugin&utm_content=tracker%2F530581&utm_medium=issues&utm_source=github)** We accept bounties via [Bountysource](https://www.bountysource.com/?utm_campaign=plugin&utm_content=tracker%2F530581&utm_medium=issues&utm_source=github).
If the JSON data is using some different structure (as using an ActiveQuery to query data), and JSON result contains something like: {"results":[{"id":"ID1","description":"DESCRIPTION 1"},{"id":"ID2","description":"DESCRIPTION 2"}, ...
I would be using something like this:
The result is that initialValueText "is not correctly used" and an empty string is assigned (when the value assigned to the attribute is selected an "undefined" is returned).
That's because on initial settings the "templateSelection" model has a "text" value initialized and not a "description" initialized. using this solves the problem:
It would be nice to have some setting which would simply rename the "text" and also the "id" value used for the Select2 (something like displayValue ...)
hope it's clear enough :-)