spantaleev / sijax-python

An easy to use AJAX library for Python based on jQuery.ajax

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Call autocomplete method on DOM element with values

Midnighter opened this issue · comments

I used to employ ajax to generate an auto-complete list in the following way (using jQuery-UI):

        $.ajax({
            url: '{{ url_for("autocomplete") }}',
            data: {
                term: $('select#table').val()
            }
        }).done(function(data) {
            $('input#query').autocomplete({
                source: data.choices,
                minLength: 1,
            });
        });

Now that I've switched to Sijax, my solution was to obj_response.call a new javascript function:

    function auto_fill(data) {
        $('input#query').autocomplete({
            source: data.choices,
            minLength: data.length,
        })
    }

Or is there a way to directly call autocomplete on the input#query element with values?

There's no good way to call back your code, unless you make the function public (like you've supposedly done with auto_fill()).

As for your 2nd question, you can do obj_response.script("$('input#query').autocomplete(...)") to directly call autocomplete.

If the .script() way of doing things is fine by you, it may be the best option in this case. I would refrain from using Sijax in such cases, however. It's not a bad idea to use jQuery AJAX for certain things and only use Sijax whenever it makes your life easier.

Thank you for your response. I did see the .script method for the response object but then it was not clear to me how to pass in the JSON as an argument from the Python side.

Is it clear now?

You either write the JSON string manually (not encouraged) or you build a dictionary and use json.dumps() to turn that into JSON (recommended way).

OK, that simple 😄