2021-01-29 00:20:46 +05:30
---
stage: none
group: unassigned
2021-02-22 17:27:13 +05:30
info: To determine the technical writer assigned to the Stage/Group associated with this page, see https://about.gitlab.com/handbook/engineering/ux/technical-writing/#assignments
2021-01-29 00:20:46 +05:30
---
2017-08-17 22:00:37 +05:30
2021-01-29 00:20:46 +05:30
# Ajax plugin
`Ajax` is a DropLab plugin that allows for retrieving and rendering list data
from a server.
2017-08-17 22:00:37 +05:30
## Usage
2021-01-29 00:20:46 +05:30
Add the `Ajax` object to the plugins array of a `DropLab.prototype.init` or
`DropLab.prototype.addHook` call.
2017-08-17 22:00:37 +05:30
2021-01-29 00:20:46 +05:30
`Ajax` requires 2 configuration values: the `endpoint` and `method` .
2017-08-17 22:00:37 +05:30
2021-01-29 00:20:46 +05:30
- `endpoint` : Should be a URL to the request endpoint.
- `method` : Should be `setData` or `addData` .
- `setData` : Completely replaces the dropdown with the response data.
- `addData` : Appends the response data to the current dropdown list.
2017-08-17 22:00:37 +05:30
```html
< a href = "#" id = "trigger" data-dropdown-trigger = "#list" > Toggle< / a >
< ul id = "list" data-dropdown > <!-- ... --> < ul >
```
2019-09-30 21:07:59 +05:30
2020-05-24 23:13:21 +05:30
```javascript
2019-09-30 21:07:59 +05:30
const droplab = new DropLab();
2017-08-17 22:00:37 +05:30
2019-09-30 21:07:59 +05:30
const trigger = document.getElementById('trigger');
const list = document.getElementById('list');
2017-08-17 22:00:37 +05:30
2019-09-30 21:07:59 +05:30
droplab.addHook(trigger, list, [Ajax], {
Ajax: {
endpoint: '/some-endpoint',
method: 'setData',
},
});
2017-08-17 22:00:37 +05:30
```
2021-01-29 00:20:46 +05:30
Optionally, you can set `loadingTemplate` to a HTML string. This HTML string
replaces the dropdown list while the request is pending.
2017-08-17 22:00:37 +05:30
Additionally, you can set `onError` to a function to catch any XHR errors.