9.0.1 • Published 4 years ago

ng2-completer v9.0.1

Weekly downloads
24,243
License
MIT
Repository
github
Last release
4 years ago

ng2-completer

Auto complete component for Angular 2.

This component is based on angucomplete-alt

Click for demo or plunk

Installation

npm install ng2-completer --save

Usage

The module you want to use ng2-completer in must import Ng2CompleterModule and FormsModule (to use the ngModel directive on ng2-completer). Ng2CompleterModule provides the CompleterService, and declares the ng2-completer directive.

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from "@angular/forms";
import { AppComponent } from './app.component';
import { Ng2CompleterModule } from "ng2-completer";

@NgModule({
  imports: [
      BrowserModule,
      Ng2CompleterModule,
      FormsModule,
  ],
  declarations: [ AppComponent ],
  bootstrap: [ AppComponent ]
})
export class AppModule { }

Add ng2-completer to your component and create a data source:

import { Component } from '@angular/core';
import { CompleterService, CompleterData } from 'ng2-completer';

@Component({
  selector: 'my-component',
  template: `<h1>Search color</h1>
            <ng2-completer [(ngModel)]="searchStr" [datasource]="dataService" [minSearchLength]="0"></ng2-completer>
            <h1>Search captain</h1>
            <ng2-completer [(ngModel)]="captain" [datasource]="captains" [minSearchLength]="0"></ng2-completer>`
})
export class MyComponent {

  protected searchStr: string;
  protected captain: string;
  protected dataService: CompleterData;
  protected searchData = [
    { color: 'red', value: '#f00' },
    { color: 'green', value: '#0f0' },
    { color: 'blue', value: '#00f' },
    { color: 'cyan', value: '#0ff' },
    { color: 'magenta', value: '#f0f' },
    { color: 'yellow', value: '#ff0' },
    { color: 'black', value: '#000' }
  ];
  protected captains = ['James T. Kirk', 'Benjamin Sisko', 'Jean-Luc Picard', 'Spock', 'Jonathan Archer', 'Hikaru Sulu', 'Christopher Pike', 'Rachel Garrett' ];

  constructor(private completerService: CompleterService) {
    this.dataService = completerService.local(this.searchData, 'color', 'color');
  }
}

ng2-completer uses rxjs stream as data sources. There are 2 ready made data sources that can be used to fetch local and remote data but it's also possible to provide a custom source that generates a stream of items.

System.js configuration

Add the following to System.js map configuration:

   var map = {
       ...
       'ng2-completer': 'node_modules/ng2-completer/ng2-completer.umd.js'
   }

API

ng2-completer component

AttributeDescriptionTypeRequiredDefault
datasourceAutocomplete list data source can be an array of strings or a URL that results in an array of strings or a CompleterData objectArray\<string>|string|CompleterDataYes
dataServiceDeprecated use datasource instead. Autocomplete list data source.CompleterDataYes
ngModelsee the angular forms API.stringYes
autoMatchAuto select an item if it is the only result and it is an exact match of the search text.booleanNofalse
autofocusSet input focus when the page loadsbooleanNofalse
clearUnselectedClear the input on blur if not selected.booleanNofalse
clearSelectedClear the input when a result is selected.booleanNofalse
disableInputIf true disable the input field.booleanNofalse
fieldTabindexSet the tabIndex of the input.numberNo
initialValueInitial value for the component. Value is parsed using: titleField, descriptionField and imageField and used as selected valueanyNo
inputIdid attribute of the input element.stringNo
inputNamename attribute of the input element.stringNo
inputClassclass attribute of the input element.stringNo
matchClassCSS class to apply for matching part of the title and description.stringNo
maxCharsMaximal number of characters that the user can type in the component.numberNo524288
minSearchLengthMinimal number of characters required for searching.numberNo3
overrideSuggestedIf true will override suggested and set the model with the value in the input field.booleanNofalse
openOnFocusIf true will open the dropdown and perform search when the input gets the focus.booleanNofalse
openOnClickIf true will open and close the dropdown by click.booleanNofalse
selectOnFocusIf true will select the input text upon focus.booleanNofalse
selectOnClickIf true will select the input text by click.booleanNofalse
fillHighlightedIf true will set the model with the value in the input field when item is highlighted.booleanNotrue
pauseNumber of msec. to wait before searching.numberNo250
placeholderPlaceholder text for the search field.stringNo
textNoResultsText displayed when the search returned no results. if the string is falsy it won't be displayedstringNo
textSearchingText displayed while search is active. if the string is falsy it won't be displayedstringNoSearching...
autoHighlightAutomatically highlight the best matching search result when the input changes. the "best match" is selected by: exact match, starts with and finally includesbooleanNofalse

ng2-completer events

NameDescriptionType
selectedemitted when an item is selected.(selected: CompleterItem): void
highlightedemitted when an item is highlighted.(highlighted: CompleterItem): void
focusemitted when the input gets focus(): void
bluremitted when the input looses focus(): void
openedemitted when the dropdown is opened or closed(isOpen: boolean): void
keyupemitted when the input emits keyup(event: any): void
keydownemitted when the input emits keydown(event: any): void

ng2-completer methods

MethodDescriptionParameters
open()Open the dropdown
close()Close the dropdown
focus()Set the focus to the completer input
blur()Remove the focus from the completer input
isOpen()Returns the state of the dropdown

Local data

Create local data provider by calling CompleterService.local.

Parameters

NameTypeDescriptionRequired
dataany[] | Observable<any[]>A JSON array with the data to use or an Observable that emits oneYes
searchFieldsstringComma separated list of fields to search on. Fields may contain dots for nested attributes; if empty or null all data will be returned.Yes
titleFieldstringName of the field to use as title for the list item.Yes

Attributes

NameTypeDescription
descriptionFieldstringName of the field to use as description for the list item.
imageFieldstringName of the field to use as image url for the list item.

Remote data

Create remote data provider by calling CompleterService.remote.

Parameters

NameTypeDescriptionRequired
urlstringBase url for the searchYes
searchFieldsstringComma separated list of fields to search on. Fields may contain dots for nested attributes; if empty or null all data will be returned.Yes
titleFieldstringName of the field to use as title for the list item.Yes

Attributes

NameTypeDescription
descriptionFieldstringName of the field to use as description for the list item.
imageFieldstringName of the field to use as image url for the list item.
urlFormater(term: string) => stringFunction that get's the searchterm and returns the search url before each search.
dataFieldstringThe field in the response that includes the data.
requestOptionsRequestOptions (@angular/common/http)HTTP request options that should be sent with the search request.

CSS classes

  • .completer-holder
  • .completer-input
  • .completer-dropdown-holder
  • .completer-dropdown
  • .completer-searching
  • .completer-no-results
  • .completer-row
  • .completer-image-holder
  • .completer-image
  • .completer-image-default
  • .completer-title
  • .completer-description
  • .completer-list-item-holder
  • .completer-list-item
  • .completer-selected-row

Credits

  • This product uses the TMDb API but is not endorsed or certified by TMDb
ng2-smart-table-arvindcb-masterservicecb-masterservice1cb-msngx-timesheetsibida-ng2-smart-tablenebular-angular-seed@tounes_lina/elbaladiya-corecamote@infinitebrahmanuniverse/nolb-ng2-c@everything-registry/sub-chunk-2277ptc-common-moduleportkonnectcargoqi-admin-migrationqs-ngredbox-portal@wyzerus/formsruisebastiao-ng2-smart-tableseatec-smart-table@stacket.in/angular2-smart-table@thebbsway/ng2-smart-tableeko-point-ng2-smart-tablefreeman-adminthemefabric8-plannergeneric-components-dxcgeneric-dashboardgeneric-datagridmachao-component-librarymso-ng2-smart-tablemy-test123phoenix-ui-corelantern-componentsinforius-smart-table-demonee-smart-tableng2-stable-nxtcngx-admin-backend-bundlengx-admin-cpexpertngx-aton-base-libraryng2-first-layoutng2-first-tableng2-first-treengx-first-selectngx-first-tablengx-backend-bundle-starterngx-dsmart-table-demong2-custom-smart-tableng2-spring-smart-tableng2-smart-table-ang11ng2-smart-table-customng2-smart-table-custom2ng2-smart-table-dst-customisedng2-smart-table-extendedng2-smart-table-extendedbyinforiusng2-smart-table-fixedng2-smart-table-jfng2-smart-table-jpng2-smart-table-pager-modng2-smart-table-pixelincng2-smart-table-radiong2-smart-table-sfang2-smart-table-suleiman-suleimanng2-smart-table-x2ng2-smart-table2ng2-smart-table-mhng2-smart-table-neeng-smart-artezanhd-tablejohn-ngx-libjohn-ngx-t1john-ngx-t2john-ngx-t3ngx-smart-table@zalastax/nolb-ng2-ctsogodar-ng2-smart-table-custom@harmonickey/ng2-smart-tablecormo-ng2-smart-table-customcustom-custom-ng2-smart-tablecustom-ng2-smart-tableconnectwise-manager@dometec/ng2-smart-table-demo@kazzkiq/ng2-smart-table@ktyhonov/ng2-smart-tableangular-custom-tableangular4-aspnetcore-universalaspnet-core-angular2-registration-login-example-clientvls-front-mobilealmighty-ui-demoascent24-data-table-demoyutsys-ng-core@ngx-admin-panel/components@recepkaraca/ng2-smart-table@recepkaraca/ng2-smart-table-rd
9.0.1

4 years ago

9.0.0

4 years ago

3.0.3

4 years ago

3.0.2

5 years ago

3.0.1

5 years ago

3.0.0-beta.2

6 years ago

3.0.0

6 years ago

3.0.0-beta.1

6 years ago

2.0.8

6 years ago

2.0.7

6 years ago

2.0.6

6 years ago

2.0.5

6 years ago

2.0.4

7 years ago

2.0.3

7 years ago

2.0.2

7 years ago

2.0.1

7 years ago

2.0.0

7 years ago

1.6.3

7 years ago

1.6.2

7 years ago

1.6.1

7 years ago

1.6.0

7 years ago

1.5.4

7 years ago

1.5.3

7 years ago

1.5.2

7 years ago

1.5.1

7 years ago

1.5.0

7 years ago

1.4.0

7 years ago

1.3.1

7 years ago

1.3.0

7 years ago

1.2.2

7 years ago

1.2.1

7 years ago

1.2.0

7 years ago

1.1.0

7 years ago

1.0.0

7 years ago

0.4.0

7 years ago

0.3.3

7 years ago

0.3.2

7 years ago

0.3.1

7 years ago

0.3.0

7 years ago

0.2.3

8 years ago

0.2.2

8 years ago

0.2.1

8 years ago

0.2.0

8 years ago

0.1.10

8 years ago

0.1.9

8 years ago

0.1.8

8 years ago

0.1.7

8 years ago

0.1.6

8 years ago

0.1.5

8 years ago

0.1.4

8 years ago

0.1.3

8 years ago

0.1.2

8 years ago

0.1.1

8 years ago

0.1.0

8 years ago