AutoComplete Source from API

Forums Autocomplete AutoComplete Source from API

This topic contains 1 reply, has 2 voices, and was last updated by  Alexander Bautz 2 months, 3 weeks ago.

  • Author
    Posts
  • #25436

    Kasey
    Participant

    Alex,

    I’m wondering if it would be possible to use AutoComplete with a datasource of an external REST API instead of a SharePoint list? If not I’ll go down the jQuery UI AutoComplete path but I like yours better.

    • This topic was modified 2 months, 3 weeks ago by  Kasey. Reason: Wrong forum
  • #25462

    Alexander Bautz
    Keymaster

    I haven’t added any official method to override this, but you can hack it by overriding the default function for getting the items like this:

    // Copy default function
    spjs.ac.getAllItemsREST_old = spjs.ac.getAllItemsREST;
    // Override default function
    spjs.ac.getAllItemsREST = function (argObj) {
        // Add your custom code here and ensure the output is in this format - including the fields you have specified as "ShowField", "SearchField" and "optionDetailFields":
        spjs.ac.data.dataObj[argObj.applyTo] = {
            "done": true,
            "items": [
                {
                    "ID": 1,
                    "Title": "Item 1"
                },
                {
                    "ID": 2,
                    "Title": "Item 2"
                }
            ]
        };
        setTimeout(function () {
            jQspjs("#spjs_acspinner_" + argObj.applyTo).hide().next().show();
            spjs.ac.data.loadFinished = new Date().valueOf();
        }, 100);
    };

    This function must be placed ABOVE the call to spjs.ac.textField({…}); and you must use the following settings:

    ...
    "useREST":true,
    "preloadData":true,
    ...

    If you use autocomplete for other fields and want to use the built in function for those, you must reset the function back to the default after you have finished calling the one using the custom function – do it like this:

    // Reset default function
    spjs.ac.getAllItemsREST = spjs.ac.getAllItemsREST_old;

    Let me know how this works out.

    Alexander

You must be logged in to reply to this topic.