Autocomplete select
Autocomplete select
Hi,
What is the easiest way to add an autocomplete select?
Has anyone got an example?
Using Select2?
Just found this:
https://editor.datatables.net/plug-ins/field-type/editor.autoComplete
The list of options needs to come from a MySQL table.
Thanks,
Mick
EDIT: Almost got this working now, just need to change the source to get the data from the DB.
Something like this maybe.
"opts": {
"source": function( request, response ) {
$.getJSON("/autocomplete/faults", {
term: extractLast(request.term)
}, response);
}
}
This question has accepted answers - jump to:
This discussion has been closed.
Answers
Good to hear you've been able to make progress with this. You are exactly right - the
source
option is the way to do this with the jQuery UI AutoComplete library. The code above should be making an Ajax request to/autocomplete/faults
which needs to query the database and return the options available.That later part is not something that the Editor PHP or .NET libraries will do for you I'm afraid, but a simple
SELECT
statement should be all that is needed for it.Allan
Thanks Allan,
PHP bit is easy:
Now the tricky bit.
I want this to work like a select. so I need to save the id.
I am editing the fault_type_process_type table, which is used to associate process types with possible faults.
The autocomplete displays the fault.description from my faults table.
What I want to do is save the id to the fault_type_process_type table.
My back-end looks like this:
So, I don't think this is correct, I am treating the autocomplete like a select.
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'dev_selection.faults.description' in 'field list'
Mick
I'm thinking Select2?
Just not sure how do that via Ajax?
Mick
Hi Mick,
I think you can use either Select2 or jQuery UI - its really up to yourself which one you prefer working with. If you are using jQuery UI else where, then it makes sense to reuse it. If you aren't, then the smaller Select2 would probably be a better bet.
Regarding the id - your auto complete PHP script is currently only returning the description column. You need it to return both the description (i.e. the label that the end user sees) and the id (i.e. the value that will be saved).
So basically you want it to return something like:
Then we can tell whichever library you land to use those parameters.
If you are using an Ajax auto complete, its probably not worth using the
->options()
method in the Editor PHP since the data for the options is going to come from your own PHP script for it anyway.Regards,
Allan
Thanks Alan,
OK. I think I am 90% there. I have a couple of problems still. The dropdown is working and I can select a new item.
1, faults.description is not displayed when the Editor opens.
2, when I select a new option and click Update, the new value is not saved. The AJAX for the Update still has the original value.
Aha,
The update is now working!
Just need to sort the description when Editor loads.
Should this happen automatically, or do I need to manually set the initial value for Select2?
Mick
Looks similar to this:
https://datatables.net/forums/discussion/comment/91842/#Comment_91842
debug code: iwebim
Trying to debug and val is null here:
It should happen automatically! Are you using the lightbox editing here, or inline editing?
The fact that it doesn't automatically show up suggests that the value that is being edited isn't in the list of values that Select2 knows about.
Allan
Lightbox.
When I first edit an existing record, the description is blank.
I can select a new description (autocomplete works) and Update it.
Then, if I open this record again, the description is displayed correctly.
Do I need to set the
"options": [
as well as fetching them results in the AJAX?Is this correct?:
I want to display the description but save the id.
Mick
Interesting one - yes I absolutely see your point.
I've just been experimenting with the Select2 examples. The Ajax data load example (
.js-example-data-ajax
) is the one of interest here. If we open it and type ana
then select "angular" from the list - the value is:So the label here is not the same as the value - perfect, that matches what you are doing.
Now reload the page and in the console do:
That sets the select element to the value for the Angular entry as we had selected it above, but the label doesn't populate - exactly as you are seeing.
Interestingly the Select2 FAQs mention exactly this issue and link to this SO post which says to call the
change
event. Trying that on the Select2 page doesn't actually work! Indeed, Editor does achange
trigger itself when the value is set.This thread has some discussion on the topic as does this one.
It appears that there isn't a nice simple way of doing this with Select2 frustratingly. I'll keep digging.
Allan
Damn,
Is this easy to do with a different autocomplete? I don't mind swapping.
I guess it might be worth trying to fix this, in case other users come across the same problem.
Mick
To be honest I don't know as this is in the scope of the third party libraries rather than in Editor itself.
I'm starting to think that it might be worth raising this in the Select2 issues list, but there are over 600 issues there at the moment, so I'm not sure how quickly it would be resolved.
Allan
Its not proven to be trivial I'm afraid, but this is the change that is needed to make it work on the client-side - in the Select2 integration plug-in for Editor replace the
set
function with:This will cause an Ajax request to be made to the server with the data requested for the value selected. The parameters sent are:
The server needs to respond with:
Editor will then show the label returned.
This is the little PHP script I've been using to test it:
I'm not happy with that frankly, but I don't see any other way with the Select2 API (which isn't documented) to do this.
I'm going to spend some time with the various auto complete plug-ins and tie a single one more tightly into Editor. Just need to pick the most suitable.
Regards,
Allan
Thanks Allan.
I will drop that in tomorrow.
Yes, I think it would be great if one of these components was encapsulated in the Editor.
Please keep me updated.
Mick
Hey Allan,
I think I have stumbled across a solution without your fix:
I changed the field names in my query:
and updated in the templates:
Does this work for you? I'm not sure if I am going mad or not. :-)
Mick
I'm not sure why that would work without an Ajax request to be honest, and the problem was that Select2 wasn't triggering an Ajax request to get the data to render.
Does that trigger an Ajax request for you?
Allan
No, the only AJAX is when the Datatable first loads.
The next is when I have typed in the select and it is doing the autocomplete.
I added some logging:
This is the result (which is where I first saw the text field):
Object {selected: true, disabled: false, text: "Clay Pluck", id: "10", title: ""…}
faults:270 selection
This shows that templateSelection gets called, when I click the Edit button on the Datatable.
Mick
Awesome. If that works go with it!
Allan
****~~1. hjygvyujgjh* # ~~****