GithubHelp home page GithubHelp logo

Comments (15)

vctls avatar vctls commented on September 28, 2024

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.

vctls avatar vctls commented on September 28, 2024

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.

vctls avatar vctls commented on September 28, 2024

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.

garak avatar garak commented on September 28, 2024

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.

vctls avatar vctls commented on September 28, 2024

Did you change the template?

from pugxautocompleterbundle.

garak avatar garak commented on September 28, 2024

I changed search.json.twig, using the content you mentioned above (value instead of id and label instead of text)

from pugxautocompleterbundle.

vctls avatar vctls commented on September 28, 2024

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.

garak avatar garak commented on September 28, 2024

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.

vctls avatar vctls commented on September 28, 2024

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.

garak avatar garak commented on September 28, 2024

Yes

from pugxautocompleterbundle.

vctls avatar vctls commented on September 28, 2024

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.

garak avatar garak commented on September 28, 2024

It's even worse: it's not updating value at all 😞

from pugxautocompleterbundle.

vctls avatar vctls commented on September 28, 2024

Huh. This is confusing. Let me clean this up a bit...

from pugxautocompleterbundle.

garak avatar garak commented on September 28, 2024

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.

vctls avatar vctls commented on September 28, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.