Example of using select2 library instead of select or autocomplete
Example of using select2 library instead of select or autocomplete
I have a need for a select field that is both autocomplete and multiple select and I understand that the select2 library is very good. It seems I an better oontrol the height of the select also (?) with selec2. But I can't find any good examples of setting this up, only this:
https://editor.datatables.net/plug-ins/field-type/editor.select2
I am trying to set this up and I have a test page. When I choose "New" I can not type anything, I can only select from the list. I am trying to accomplish autocomplete also on the field When I edit a record i get the value.
Here is a test page
If I could look at some working solution with select2 and Datatables it would help me.
Replies
For some reason the input for this field doesn't look like a select2 input:
Looks like you might be using an old version for the Editor's select2 plugin. Try the latest version from here.
Kevin
To add to that, there isn't currently an online demo for Select2 with Editor I'm afraid. I've focused instead on the built in
tags
andautocomplete
field types which can do nearly everything required for a Select2 like field and are fully integrated into Editor and the backend libraries.Allan
Hi asle,
if you use the plug-in version that Kevin recommends everything should work. Here is the link again:
https://editor.datatables.net/plug-ins/field-type/editor.select2
I use exactly those versions for Select2:

I copied your settings into my code:
For some reason I need to reinitialize my select2 field on "opened" - otherwise: not working. But maybe that's caused by dynamically adding the optgroups in a fairly unorthodox way?!
I tried the code above. And it gives you the search field etc. I don't see any effect of "tags", "allowClear" and "width" but I think it works well.
Thanks, the page you referred to is from 2018. Has there not been any work done on the plugin since then?
On the homepage for select2 there is a newer version 4.1.0-rc.0 that I installed. I also downloaded the editor.select2.js
I have got the select2 almost working but
- I can not edit a previous value. I have to click the "x" and empty the value and write it again from scratch.
- I can not select several options.
Here is my code:
I see Allan suggests that the functions are covered in
tags
andautocomplete
which is builtin in datatables. Is it better then to drop select2 and try to get my solution to work withautocomplete
?https://editor.datatables.net/examples/dropdown/tags-multiple.html
This looks like a pretty good fit for your requirements, doesn't it? I like it
And will probably use it for multi-select the next time.
Not sure why this is happening. I can't open you test case from your first post. Says its under maintenance.
You need to add the
multiple
option to theopts
property. See the Select2 options docs for more details.Here is a simple example:
https://live.datatables.net/guwafemu/634/edit
Note that when
multiple
is set that field will send an array of one or more selected options to the server.Kevin
Thanks. I have looked at the tags-multiple example and it is very much what I am after. The problem is that I have over 2000 records with entries in the field I am working with data saved like "option1, option2, option3" and the datatables tags example uses JOIN with the tags in a separate table. I am not sure how I would convert all the existing fields with the tag system. With tags I can not add new values like I can do with
autocomplete
. Is it possible to add newtags
with the tags approach? The best solution would be TAGS with the same ability as AUTOCOMPLETE to add new tags on the fly.So I have tried autocomplete but a problem I have is when I use
autocomplete
and long select lists, the select list goes outside the page. Sorry, the demo is up again (https://dev.mfu.no/code/admin-test.html - demo/demo ) where you can create a new post.I have several autocomplete fields, the field "Forfatter" collects distinct content on the field but it can be more than 50 lines. The select goes outside the page. I used CSS but dont know how I can target every
autocomplete
dropdown so that the top one is 300px (or 70%) height and the bottom is 150px (or 30%) height. If you create a new post you see that "Fylke" and "Kommune" should have been shorter height. Also the select2 field goes outside the page. I have used this CSS:The test case is asking for a login.
I tried this with the
tags
:In this test case and it seems to work with the Datatables
tags
type:https://live.datatables.net/bomavase/1/edit
However since the
table
list of options is a Datatable it would be nice if it used thedatatable
config property. I tried it but it doesn't seem thetags
looks for theconfig
property. Maybe @allan will comment on whether this is feasible or not.Select2 should just use standard CSS for the select2 library. This SO thread has some suggestions. I tried this:
In this test case:
https://live.datatables.net/guwafemu/635/edit
Kevin
Thanks again for your input! I wrote the username/pass for the demo page in my reply (demo/demo) but maybe it was not clear enough. I looked also at the datatables field config but not sure how to use it. If you look at the demo again you see when you create a new record that the dropdowns are all the same height. So I am not sure how to target each dropdown separately:
Suddenly the select2 is not expanding outside my page (today) but the I have max-height set to max-height:300px and that is good for the field "Forfatter" but too long for "Fylke" and "Kommune". So how to target each of these dropdowns with their own height?
@kthorngren I added
multiple: true
to the field 'publikasjon.tiltakstype' and I can add multiple but I can not save. I get this error:the code it refers to is this in the controller php:
I like the builtin tags and autocomplete so I wonder if is is possible to use multiple items like the tags example but without a mysql JOIN where the field content is saved directly as "option1, option2, option3" instead of "1,2,3" which is joined on another table with the values.
My fields with one value work fine with autocomplete since I can also edit the input. It seems like in the example that it is not possible to add a new value to the value list with select2 but it should behave like autocomplete like now when I fetch DISTINCT values (?)
For my multiple field I would if possible avoid a JOIN and only save as the text since I have over 2000 records with (comma-separated) multiple values. And I may just be overcomplicating things as I tend to do!
For the
tags
field I don't see a way specify a CSS selector to apply styling. The only option I can think of is to programmatically apply the CSS when the for is open using theopen
event. For example:Updated test case:
https://live.datatables.net/bomavase/2/edit
The key is
#DTE_Field_position
which contains thee-fields.name
value.You can probably do something similar with Select2. You will need to inspect the form elements to determine the proper selectors.
@allan or someone else familiar with the server side libraries will need to help with your questions there.
Kevin
I have a use case for adding new values on the fly. I do that using a "selectize" field (yet another field type plug-in)
https://editor.datatables.net/plug-ins/field-type/editor.selectize
The JS looks like this:
You can add new values but they may not be integer numbers (because then I can't distinguish the new value from existing ids). If you enter a new value it is preceeded by "Neu" or "Add" (depending on user language).
That is all fairly easy. It becomes more difficult when it comes to saving this on the server.
When a new value is entered (i.e. something that isn't an integer) I need to add this to the available options which are saved in a database table and save the id of the newly created record as a foreign key in "contract.gov_manual_creditor_id".
So here is the PHP code.
First the field instance which looks fairly normal. It will even try to write text to the gov_manual_creditor_id field in case of a new value entered by the user which doesn't seem to matter ... At least it hasn't caused any issues so far since the respective id field is nullable. And as you will see this value gets updated with a valid value on "writeCreate" and "writeEdit".
Then on "writeCreate" and also on "writeEdit" I insert the new "manual_creditor"_record when a new value is created by the user and update the "contract" record with the id of the newly created "manual creditor" record.