1

I currently have a JSON callback containing both an id an a name as below:

[{"id":"1","name":"Wired Motors"},
{"id":"2","name":"pragma innovations"},
{"id":"3","name":"University of Femme Fatales"}]

I am using jQuery UI autocomplete which needs to match the name section of the returned result and display this in the suggest drop down and also be able to drag in the id into a html data element.

So far I am not having much luck in matching the user input to the name section of the callback. I can do this with a clean array but not sure how to do this with a JSON array

1

1 Answer 1

1

I believe the jQuery Autocomplete uses a standardised Name => Value JSON Array format of:

[{ "label" : "1", "value" : "Wired Motors" },
 { "label" : "2", "value" : "pragma innovations"},
 { "label" : "3", "value" : "University of Femme Fatales"}]

Soruce: jQuery Autocomplete Source API

You can then add them to the Autocomplete Source/Suggestions:

$('#myautocomplete').autocomplete({
    minLength : 2,
    source : function( request, response ) {
       $.ajax({
           url: 'path-to-your-script.php',
           dataType: 'json',
           data: '?variablestodb=somedata',
               success: function( data ) {
               response( $.map( data, function( item ) {
                   return {
                      label: item.label,
                      value: item.value
                     }
                  }));
                }
            });
        },
        select: function( event, ui ) {

        }
    });
Sign up to request clarification or add additional context in comments.

7 Comments

The array is actually where the autocomplete gets its values. This will be cached due to the size of the database after the initial request. I think I can use your logic though :)
Ahhh sorry! :D So using one of the callbacks of the Autocomplete, you're wishing to re-use the selected option elsewhere? Could you show some code you're attempting to do this with?
It works exactly the same as getting the data from a database - just, doing this on keyup will put a lot of pressure on the db. Therefore on keyup on change request will check the cached results hopefully matching the 'name' values. I would then have to drag in the id - The problem is I don't seem to be matching the name sections :(
Must be a pretty big Db.. How long does it take to get a result after a keypress up? x) Would sound like some Db Optimisation would help more - also, surely after a keypress, the previous cache results would become.. redundant anyway!
The data is unlikely to change and I am probably going to set the cache to timeout after an hour. I am just testing this process at the moment (junior dev) to see which option (if any) will be suitable. I do think the cache method could cause some unwanted problems to the end user though if they need to re-edit a passed record via the search facility. Currently we are only working on the test database which only holds a tiny amount of data. Data migration is the job for next week - not fun 30yr old database schema with much redundant and duplicated data :(
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.