Select2 initialValue
Select2 initialValue

Hi!
Im still on laravel, laravel-datatables and datatables with datatables Editor.
i have gotten the first steps with select2 to work. The dataTable field ist showing:
[ 'data' => 'facility.name', 'title' => 'Facility'],
The editor field is initialised:
{
label: "Facility",
name: "facility.name",
type: "select2",
opts: {
ajax: {
url: '{{ route("api.facilities.search") }}',
dataType: 'json',
initialValue: true
},
allowClear: false,
language: "de",
minimumInputLength: 0
}
}
The select2's option lists gets populated:
class FacilitiesApiController extends Controller
{
public function search(Request $request)
{
if ($request->input('initialValue') == true) return '{ "id": 20, "text": "foobar" }';
$facilities = Facility::where('name', 'LIKE', '%'.$request->input('term', '').'%')
->get(['id', 'name as text']);
return ['results' => $facilities];
}
}
showing all 27 entries.
However, the initialValue is not set. When I click on the field in my table, the text changes to the folded (i.e. just one single line) selectbox, but is not showing 'foobar' as I would expect.
Console shows:
Request: search?initialValue=true&value=%22Mack%22
Response: { "id": 20, "text": "foobar" }
What am I doing wrong?
TIA & cu,
ada
Replies
I have refined the ajax response:
So now I have a valid result for the initialValue's response as well. Still it is not working :-(
I think the issue is the response from the server for the initial value. As the docs note:
So
return $facilities[0];
I think should do it on line 7 in the above.Allan
Hi Allan!
Thank you very much for your reply, but I already got the result as you are proposing in the first version
(Response: { "id": 20, "text": "foobar" })
. I modified this to your proposal and toreturn '{ "id": 21, "text": "Singer" }'
.Still, the selectbox doesnt show the text 'Singer' in its edit line.
Please do you have a working example of 2 joined tables with a select box?
cu,
ada
Yes, there is an example here but it doesn't use Select2, just a regular Select.
Can you give me a link to your page so I can debug it directly please?
Allan
I managed to get a tiny little bit further.
In the first case, i got a single Select2-field operating on a foreign key. When the modal dialog is opened, it fires
search?initialValue=true&value=%222%22
i.e.:
search?initialValue=true&value="2"
2 is the content of the foreign key, my serverside responds with
{ "id": "2", "text": "some text" }
effectively repeating the id from the query (2) and responding with the text for that id. If the responded id doesn't match the queried id, nothing is displayed.
Now, I have a multiple Select2. This one sends:
search?initialValue=true&value=[{%22id%22:%226%22,%22name%22:%22UnitUser%22,%22created_at%22:%222019-10-26%2017:26:19%22,%22updated_at%22:%222019-10-26%2017:26:19%22,%22pivot%22:{%22user_id%22:%2219%22,%22role_id%22:%226%22,%22created_at%22:%222019-11-17%2012:44:54%22,%22updated_at%22:%222019-11-17%2012:44:54%22}}]
ie.:
in case of a single selected item.
I tried responding with (notice, that you defintely do have to repeat the complete, otherwise nothing gets displayed):
I don't understand why there is a need for the initialValue query at all, since, it does know the initial value already: id:6, text: "Text1"
But alas, this initial value doesn't affect the initial value at all. it just displays the initial value – it's not in any way set to selected. For example you can still select "Text1" a second time and it will appear a second time. Also, this preselected valu throws an error, wehen trying to save it:
Please: how does this plugin work? And no, an example without this plugin used at all is not an example.
Anyone?
Could you try making:
an array please - e.g.:
As you'll see from the "general" Select2 response it uses an array of values.
The field only has a single value - that will be
6
or[6]
or similar depending on the configuration. Select2 doesn't see the data from the rest of the row, hence why it needs to query the server to get the remainder of the data. It could optionally have some kind of callback to get it from the client-side data (i.e. the DataTable row) - but the server-side call was the way I implemented it.Thanks,
Allan
Hi!
Since your are refering to
{ "id": "2", "text": "some text" }
I assume, you are talking about the multiple: false case.
It is pretty simple. I can respond to the initialValue request with either
{"results":[{"id":2,"text":"Fast Banana"}]}
or
or
[{"id":2,"text":"Fast Banana"}]
or
Yes, they all setup the topline of the select2 correctly as long as the queried id is part of the result set.
However, when the select2 gets a click, DTE sends a second query to populate the dropdown list. The response to this second query needs the
wrapped around to function properly.
For code hygenic reasons, I decided to implement it (in laravel) like so:
Any reasons I shouldn't do this?
Now, how about the multiple:true case?
Now, I'd need the
part for multiple initial values.
TIA!
Anyone?
No - I don't see any. It isn't ideal, I know. We are going to work on better integration with Select2 in the new year and publish working examples for it.
Basically the same thing, but you'd need to take account of the fact that
value
for theinitialValue
request will be a stringified array of values. So you'd need to JSON decode it and then loop over the resulting array to build up thewhere
condition.Allan