Comments (15)
Ok, found the answer here:
http://stackoverflow.com/questions/4234455/jquery-autocomplete-not-working-with-json-data
When using jQuery-UI, the id
and text
parameters in the JSON search template must be changed to value
and label
, like this:
[{% for author in authors -%}
{{ {value: author.id, label: author.__toString}|json_encode|raw }}
{%- if not loop.last %},{% endif -%}
{%- endfor %}]
from pugxautocompleterbundle.
Oh, and by the way: the search parameter sent in the ajax request is term
form jQuery-UI, instead of q
, so that must be changed as well in the controller:
$qs = $request->query->get('term');
from pugxautocompleterbundle.
Actually, this issue breaks the autocompleter-jqueryui.js
script.
The select
callback from the autocomplete
method should read
$this.val(ui.item.value);
instead of
$this.val(ui.item.id);
from pugxautocompleterbundle.
I tried it, but I'm getting the id of the selected option, not its label.
Indeed, I also tried with $this.val(ui.item.label);
instead of $this.val(ui.item.value);
, but I keep getting the value 😞
from pugxautocompleterbundle.
Did you change the template?
from pugxautocompleterbundle.
I changed search.json.twig
, using the content you mentioned above (value
instead of id
and label
instead of text
)
from pugxautocompleterbundle.
That's weird. I don't remember changing anything else. I just tried on the demo project and it worked.
Should I push or copy the code somewhere?
from pugxautocompleterbundle.
I'm trying using https://github.com/garak/AutoCompleterSandbox, where I altered search.json.twig
(as mentioned above) and changed by hand autocompleter-jqueryui.js
in vendor (as you suggested).
You can swap to jquery UI by pasasing jqui=1
in "new" page (e.g. /new-book?jqui=1
)
from pugxautocompleterbundle.
Just to be sure: what is the expected behaviour?
With jQuery-UI, the field is a text input, so I understand we're using the hidden field to store the ID, while the "fake" input, on which the autocompleter is called, is supposed to display the text value?
Is that right?
from pugxautocompleterbundle.
Yes
from pugxautocompleterbundle.
Got it.
You need to preventDefault()
on the select
event.
$fakeInput.autocomplete({
source: settings.url_list + "?jqui=1",
select: function (event, ui) {
event.preventDefault();
// Set the real hidden input to ID number.
$this.val(ui.item.value);
// Set the fake input value to the corresponding text value.
$(this).val(ui.item.label);
if (settings.on_select_callback) {
settings.on_select_callback($this);
}
},
minLength: settings.min_length
});
from pugxautocompleterbundle.
It's even worse: it's not updating value at all 😞
from pugxautocompleterbundle.
Huh. This is confusing. Let me clean this up a bit...
from pugxautocompleterbundle.
OK, the "new" action is working for me now. Now I miss the "edit" action, where I see the id of the numeric id of the author, instead of the name
from pugxautocompleterbundle.
Found it:
There's a small difference between autocompleter-select2.js
and autocompleter-jqueryui.js
:
// autocompleter-select2.js
$.ajax({
url: (settings.url_get.substring(-1) === '/' ? settings.url_get : settings.url_get + '/') + $this.attr('value'),
success: function (name) {
val = name;
$fakeInput.select2('val', name);
}
});
// autocompleter-jqueryui.js
$.ajax({
url: settings.url_get + $this.val(),
success: function (name) {
$fakeInput.val(name);
}
});
Change the url
parameter and everything works as expected.
from pugxautocompleterbundle.
Related Issues (20)
- No form validation is triggered when a value is entered but no option is selected from the list HOT 6
- How to make the autocomplete field required ? HOT 5
- Attempted to call an undefined method named HOT 7
- A tree builder without a root node is deprecated HOT 1
- New release? HOT 1
- Documentation overhaul HOT 2
- setting options when using jquery-ui HOT 2
- [with jquery-ui] create a "response" callback function HOT 2
- Symfony 5 compatibility HOT 1
- Avoid dependencies on javascript libraries HOT 5
- Error with newAction HOT 4
- jquery-ui and related json template HOT 7
- Not working with symfony's webpack 1.0 and stimulus 2.0 HOT 2
- Can you publish your npm package on npm? HOT 2
- Confusion with LexikFormFilterBundle usage HOT 5
- Error "Cannot read properties of undefined (reading 'toUpperCase')" when using select2 version 3.5.2-browserify HOT 4
- Symfony version locked to 4.x or 5.x; need 6.x HOT 1
- New tag? HOT 1
- New tag? HOT 1
- Add support for Symfony 7 HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pugxautocompleterbundle.