Transform (auto-completion)

Description

Transform function is used to map response data returned by a web service (program) into a default structure used by Typeahead and auto-completion widget:

[{
    label: '...',
    value: '...',
}, {
    label: '...',
    value: '...',
}]

where:

  • label: displayed in dropdown list as a suggestion; see also template rendering

  • value: used to set the value of a input field after a suggestion is selected; see also item label

optional fields:

  • category: used to group a list of suggestions by; see also group parameter

Configuration

Transform function will be run on each element in the array returned by the web server (program)

The arguments passed to the function are:

  • dataset: a list of the current dataset settings,

  • suggestion: a element from the array returned from a web service (program),

  • index: the index of the suggestion into the array returned by a web service (program),

  • datasetName: a name of the current dataset,

  • query: a value of a input field that has triggered auto-completion

Default value

Default filter function map /s/suggest.json output:

[{
    "key": "and",
    "disp": "and",
    "disp_t": "T",
    "wt": "5.568",
    "cat": "",
    "cat_t": "",
    "action": "",
    "action_t": "S"
}]

into:

[{
    label: 'and'
    value: 'and'
    extra: {
        "key": "and",
        "disp": "and",
        "disp_t": "T",
        "wt": "5.568",
        "cat": "",
        "cat_t": "",
        "action": "",
        "action_t": "S"
    },
    category: '',
    rank: 1
}]
transform: function(set, suggestion, i, name, query) {
    var value = suggestion.key, label = suggestion.key;
    if (suggestion.action_t == 'Q') value = suggestion.action;
    if (suggestion.action_t == 'S') value = suggestion.disp;
    if (suggestion.disp_t == 'C') label = eval(suggestion.disp);
    else if (suggestion.disp) label = suggestion.disp;

    return {
        label    : label,
        value    : value,
        extra    : suggestion,
        category : suggestion.cat ? suggestion.cat : '',
        rank     : i + 1,
        dataset	 : name,
        query    : query
    };
}}

Examples

Map below response returned by search program:

[{
    'custom_label': 'suggestion label 1',
    'custom_value': 'suggestion value 1',
    'custom_category': 'Suggestions'
}, {
    'custom_label': 'suggestion label 2',
    'custom_value': 'suggestion value 2',
    'custom_category': 'Suggestions'
}]

into required format using custom filter function:

transform: function(set, suggestion, i, name, query) {
    return {
        label   : suggestion['custom_label'],
        value   : suggestion['custom_value'],
        category: suggestion['custom_category'],
        rank    : i + 1,
        dataset : name,
        query   : query
    };
}