lekoala / bootstrap5-autocomplete

Autocomplete for Bootstrap 5 (and 4!)
MIT License
87 stars 16 forks source link

question: nested serverDataKey #34

Closed stadskle closed 11 months ago

stadskle commented 1 year ago

Nice plugin! I am trying to use this against a third party company lookup, but I am not able to get serverDataKey to get the data from the nested structure. Is that not supported or have I misunderstod?

Autocomplete.init("#input-id", {
            fullWidth:true,
            valueField: "id",
            labelField: "name",
            highlightTyped: true,
            onSelectItem: console.log,
            liveServer: true,
            suggestionsThreshold:3,
            queryParam:'q',
            server: 'https://no.search.two.inc/search',
            serverParams: {'limit':10, 'offset': 0},
            noCache: false,
            serverDataKey: 'items'
        });

The server response looks like this:

 {
    "status": "success",
    "data": {
        "total": 3,
        "items": [
            {
                "id": "981617517",
                "name": "EQUINOR BIL OSLO",
                "highlight": "\\u003cmark\\u003e\\u003cb\\u003eEQUINOR\\u003c/b\\u003e\\u003c/mark\\u003e \\u003cmark\\u003e\\u003cb\\u003eB\\u003c/b\\u003eIL\\u003c/mark\\u003e OSLO",
                "code": "FLI",
                "score": 46.73977867834532
            },
            {
                "id": "993015253",
                "name": "EQUINOR BIL BERGEN",
                "highlight": "\\u003cmark\\u003e\\u003cb\\u003eEQUINOR\\u003c/b\\u003e\\u003c/mark\\u003e \\u003cmark\\u003e\\u003cb\\u003eB\\u003c/b\\u003eIL\\u003c/mark\\u003e \\u003cmark\\u003e\\u003cb\\u003eB\\u003c/b\\u003eERGEN\\u003c/mark\\u003e",
                "code": "FLI",
                "score": 46.581858857682334
            },
            {
                "id": "999145671",
                "name": "EQUINOR BERGEN BÅTLAG",
                "highlight": "\\u003cmark\\u003e\\u003cb\\u003eEQUINOR\\u003c/b\\u003e\\u003c/mark\\u003e \\u003cmark\\u003e\\u003cb\\u003eB\\u003c/b\\u003eERGEN\\u003c/mark\\u003e \\u003cmark\\u003e\\u003cb\\u003eB\\u003c/b\\u003eÅTLAG\\u003c/mark\\u003e",
                "code": "FLI",
                "score": 44.4037772735859
            }
        ]
    }
}

I have tried serverDataKey: 'items' andserverDataKey: 'data.items'. But I just getCannot read properties of undefined (reading 'items')`. etc.

lekoala commented 11 months ago

sorry for the late response i seem to have missed it

yes indeed i need to add nested support, good catch

lekoala commented 11 months ago

Fixed in https://github.com/lekoala/bootstrap5-autocomplete/releases/tag/1.1.24