#### Index - [Using fixed `collection` or `collectionAsync`](#using-collection-or-collectionasync) - [Editor Options (`AutocompleterOption` interface)](#editor-options-autocompleteroption-interface) - [Using Remote API](#using-external-remote-api) - [Basic Usage](#remote-api-basic) - [Basic Usage with Object Result (**preferred way**)](#remote-api-basic-with-object-result) - [with `renderItem` + custom Layout (`twoRows` or `fourCorners`)](#remote-api-renderitem-callback--custom-layout-tworows-or-fourcorners) - [Custom Styling - SASS variables](https://github.com/ghiscoding/slickgrid-universal/blob/master/packages/common/src/styles/_variables.scss#L141) - [Force User Input](#autocomplete---force-user-input) - [How to change drop container dimensions?](#how-to-change-drop-container-dimensions) - [Animated Gif Demo](#animated-gif-demo) - See the [Editors - Wiki](../editors.md) for more general info about Editors (validators, event handlers, ...) ### Demo [Demo Page](https://ghiscoding.github.io/slickgrid-universal/#/example11) | [Demo Component](https://github.com/ghiscoding/slickgrid-universal/blob/master/demos/vanilla/src/examples/example11.ts) ### Introduction AutoComplete is a functionality that let the user start typing characters and the autocomplete will try to give suggestions according to the characters entered. The collection can be a fixed JSON files (collection of strings or objects) or can also be an external remote resource to an external API. For a demo of what that could look like, take a look at the [animated gif demo](#animated-gif-demo) below. We use an external lib named [Autocomplete](https://github.com/kraaden/autocomplete) (aka `autocompleter` on npm) by Kraaden. ## Using `collection` or `collectionAsync` If you want to pass the entire list to the AutoComplete (like a JSON file or a Web API call), you can do so using the `collection` or the `collectionAsync` (the latter will load it asynchronously). You can also see that the Editor and Filter have almost the exact same configuration (apart from the `model` that is obviously different). ##### Component ```typescript export class GridBasicComponent { columns: Column[]; gridOptions: GridOption; dataset: any[]; attached(): void { // your columns definition this.columns = [ { id: 'countryOfOrigin', name: 'Country of Origin', field: 'countryOfOrigin', formatter: Formatters.complexObject, dataKey: 'code', // our list of objects has the structure { code: 'CA', name: 'Canada' }, since we want to use the code`, we will set the dataKey to "code" labelKey: 'name', // while the displayed value is "name" type: 'object', sorter: Sorters.objectString, // since we have set dataKey to "code" our output type will be a string, and so we can use this objectString, this sorter always requires the dataKey filterable: true, sortable: true, minWidth: 100, editor: { model: Editors.autocompleter, customStructure: { label: 'name', value: 'code' }, collectionAsync: this.http.get('assets/data/countries.json'), // this demo will load the JSON file asynchronously onInstantiated: (instance) => console.log('instance', instance), // get instance from 3rd party lib }, filter: { model: Editors.autocompleter, customStructure: { label: 'name', value: 'code' }, collectionAsync: this.http.get('assets/data/countries.json'), onInstantiated: (instance) => console.log('instance', instance), // get instance from 3rd party lib } } ]; this.gridOptions = { // your grid options config } } } ``` ### Collection Label Render HTML By default HTML is not rendered and the `label` will simply show HTML as text. But in some cases you might want to render it, you can do so by enabling the `enableRenderHtml` flag. **NOTE:** this is currently only used by the Editors that have a `collection` which are the `MultipleSelect` & `SingleSelect` Editors. ```typescript this.columns = [ { id: 'effort-driven', name: 'Effort Driven', field: 'effortDriven', formatter: Formatters.checkmark, type: 'boolean', editor: { model: Editors.autocompleter, placeholder: '🔍 search city', // example with a fixed Collection (or collectionAsync) options: { showOnFocus: true, // display the list on focus of the autocomplete (without the need to type anything) } as AutocompleterOption, enableRenderHtml: true, // this flag only works with a fixed Collection // collectionAsync: this.http.get(COUNTRIES_COLLECTION_URL), collection: [ { value: '', label: '' }, { value: true, label: 'True', labelPrefix: ` ` }, { value: false, label: 'False', labelPrefix: ` ` } ], } } ]; ``` ### Editor Options (`AutocompleterOption` interface) All the available options that can be provided as editor `options` to your column definitions can be found under this [AutocompleterOption interface](https://github.com/ghiscoding/slickgrid-universal/blob/master/packages/common/src/interfaces/autocompleterOption.interface.ts) and you should cast your editor `options` to that interface to make sure that you use only valid options of the autocomplete library. ```ts editor: { model: Editors.autocompleter, options: { minLength: 3, } as AutocompleterOption } ``` #### Grid Option `defaultEditorOptions You could also define certain options as a global level (for the entire grid or even all grids) by taking advantage of the `defaultEditorOptions` Grid Option. Note that they are set via the editor type as a key name (`autocompleter`, `date`, ...) and then the content is the same as editor `options` (also note that each key is already typed with the correct editor option interface), for example ```ts this.gridOptions = { defaultEditorOptions: { autocompleter: { debounceWaitMs: 150 }, // typed as AutocompleterOption } } ``` ## Using External Remote API You could also use external 3rd party Web API (can be JSONP query or regular JSON). This will make a much shorter result since it will only return a small subset of what will be displayed in the AutoComplete Editor or Filter. For example, we could use GeoBytes which provide a JSONP Query API for the cities of the world, you can imagine the entire list of cities would be way too big to download locally, so this is why we use such API. ### Remote API (basic) The basic functionality will use built-in 3rd party lib styling that is to display a label/value pair item result. ##### Component ```typescript export class GridBasicComponent { columns: Column[]; gridOptions: GridOption; dataset: any[]; initializeGrid() { // your columns definition this.columns = [ { id: 'product', name: 'Product', field: 'product', filterable: true, minWidth: 100, editor: { model: Editors.autocompleter, alwaysSaveOnEnterKey: true, options: { showOnFocus: true, minLength: 1, fetch: (searchText, updateCallback) => { // assuming your API call returns a label/value pair yourAsyncApiCall(searchText) // typically you'll want to return no more than 10 results .then(result => updateCallback((results.length > 0) ? results : [{ label: 'No match found.', value: '' }])) .catch(error => console.log('Error:', error)); }, } as AutocompleterOption, }, }, ]; this.gridOptions = { // your grid options config } } } ``` ### Remote API (basic with object result) This is the preferred way of dealing with the AutoComplete, the main reason is because the AutoComplete uses an `` and that means we can only keep 1 value and if we do then we lose the text label and so using an Object Result makes more sense. Note however that you'll need a bit more code that is because we'll use the `'object'` and so we need to provide a custom `SortComparer` and also a custom `Formatters` and for them to work we also need to provide a `dataKey` (the value) and a `labelKey` (text label) as shown below. ```ts this.columns = [ { id: 'product', name: 'Product', field: 'product', dataKey: 'id', labelKey: 'name', // (id/name) pair to override default (value/label) pair editor: { model: Editors.autocompleter, alwaysSaveOnEnterKey: true, type: 'object', sortComparer: SortComparers.objectString, options: { showOnFocus: true, minLength: 1, fetch: (searchText, updateCallback) => { // assuming your API call returns a label/value pair yourAsyncApiCall(searchText) // typically you'll want to return no more than 10 results .then(result => updateCallback((results.length > 0) ? results : [{ label: 'No match found.', value: '' }])) .catch(error => console.log('Error:', error)); }, } as AutocompleterOption, } }, ]; ``` ### Remote API with `renderItem` + custom layout (`twoRows` or `fourCorners`) #### See animated gif ([twoRows](#with-tworows-custom-layout-without-optional-left-icon) or [fourCorners](#with-fourcorners-custom-layout-with-extra-optional-left-icon)) The lib comes with 2 built-in custom layouts, these 2 layouts also have SASS variables if anyone wants to style it differently. When using the `renderItem`, it will require the user to provide a `layout` (2 possible options `twoRows` or `fourCorners`) and also a `templateCallback` that will be executed when rendering the AutoComplete Search List Item. For example: ##### Component ```typescript export class GridBasicComponent { columns: Column[]; gridOptions: GridOption; dataset: any[]; initializeGrid() { // your columns definition this.columns = [ { id: 'product', name: 'Product', field: 'product', filterable: true, minWidth: 100, editor: { model: Editors.autocompleter, alwaysSaveOnEnterKey: true, customStructure: { label: 'itemName', value: 'id' }, options: { showOnFocus: true, minLength: 1, fetch: (searchText, updateCallback) => { yourAsyncApiCall(searchText) // typically you'll want to return no more than 10 results .then(result => updateCallback((results.length > 0) ? results : [{ label: 'No match found.', value: '' }])) .catch(error => console.log('Error:', error)); }, renderItem: { layout: 'twoRows', templateCallback: (item: any) => `