1.1.9 • Published 5 years ago

ng-remote-autocomplete v1.1.9

Weekly downloads
9
License
MIT
Repository
github
Last release
5 years ago

ng-remote-autocomplete

The package is a remote autocomplete.

Installation

The package requires Angular 5+ to run.

$ npm i ng-remote-autocomplete

Usage

In order to use the remote autocomplete you need to:

  • add RemoteAutocompleteModule And FormsModule to the Module you want to use in.
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from "@angular/forms";
import { AppComponent } from './app.component';
import { RemoteAutocompleteModule } from "ng-remote-autocomplete";

@NgModule({
  imports: [
      BrowserModule,
      FormsModule,
      RemoteAutocompleteModule
  ],
  declarations: [ AppComponent ],
  bootstrap: [ AppComponent ]
})
export class AppModule { }
  • implement get method of AutocompleteSourceService or ParameterizedAutocompleteSourceService which returns array of AutocompleteItem
import { Observable } from "rxjs/Rx";
import { } from "rxjs/Rx";

import { AutocompleteItem } from "ng-remote-autocomplete";
import { AutocompleteSourceService } from "ng-remote-autocomplete";

export class ItemService implements AutocompleteSourceService {
  get(searchParam: string): Observable<AutocompleteItem[]> {
    let fakeResult:AutocompleteItem[]  = [
        {value: "A", description: " - One letter", originalObject: {}},
     ];

    return Observable.of(fakeResult).delay(150);
  }
}
  • provide the sevice to the remote-autocomplete component as a parameter
import { Component } from  '@angular/core';
import { ItemService } from  './services/itm-service';

@Component({
selector:  'app-root',
template:  `<remote-autocomplete [service]="itemService"></remote-autocomplete>`,
providers: [ ItemService ]
})
export  class  AppComponent {
constructor(public  itemService:  ItemService) {
}

Exceptions

If you don't provide the service then the module will throw and Exception that AutocompleteSourceService or ParameterizedAutocompleteSourceService must be provided. If you provide ParameterizedAutocompleteSourceService service and don't provide searchParameters then autocmplete component will throw Exception.

Api

Property NameDescriptionTypeRequiredDefault value
serviceImplementation of AutocompleteSourceService or ParameterizedAutocompleteSourceServiceAutocompleteSourceService | ParameterizedAutocompleteSourceServicetrue
minSearchLengthMinimum value length from which searching will be triggerednumberfalse1
autocompleteAttrA value for the autocomplete attribute of the inputstringfalsenull
tabindexA value for the tabindex attributenumberfalsenull
maxCharsAllowable max length of search fieldnumberfalse2147483647
pauseThe pause after which searching will be triggerednumberfalse100
inputIdThe id for the inputstringfalse-
inputNameThe name for the inputstringfalse-
inputClassThe array of string for the inputstring[]false-
placeholderThe text to be placed as placeholderstringfalse-
searchParametersThe search parameters which are passed to the search method of ParameterizedAutocompleteSourceService interface implementationanytrue only if type of service is ParameterizedAutocompleteSourceService-
disabledThe property if input should be disabledbooleanfalsefalse
needToShowNotFoundTextThe property which enable/disable showing not found text blockbooleanfalsetrue
notFoundTextThe text which will be displayed if there is no resultstringfalse"No results found"
needToShowSearchingTextThe property which enable/disable showing searching text blockbooleanfalsetrue
searchingTextThe text which will be displayed during the searchstringfalse"Searching..."
stopEnterPropagationOnSelectTrue if keydown event should not be dispatched when autocomplete item is selected using Enter keybooleanfalsefalse

Output events

Event NameTypeDescription
type-The type event on the input
highlightedAutocompleteItem or nullThe event is triggered when an option is higlighted with AutocompleteItem item or nul when it is unhighlighted
selectedAutocompleteItemThe event is triggered when an option is selected
focusFocusEventThe event is triggered when the input get focus
blurFocusEventImportant: isn't fired when option is being selected and input loses focus

Available classes

ClassDescription
.remote-autocompleteThe main class of the whole component
.autocomplete-search-wrapperThe class of the div which contains loading, not found and otptions divs
.option-list-wrapperThe class of the main div where options are placed
.autocomplete-optionThe class of an option
.autocomplete-option-activeThe class of the highlighted option
.autocomplete-option-valueThe class of the span where value is placed
.autocomplete-option-descriptionThe class of the span where description is placed
.autocomplete-loadingThe class of the span where loading text is shown
.autocomplete-not-foundThe class of the span where searching text is shown

Available methods

The local reference of the remote-autocomplete component is a type of Autocomplete and provides the following methods:

MethodDescription
focus()Set the focus to the input field
open()open the autocomplete where search parameters will be the current value, won't be open if the length of the current value is less than minSearchLength value.
close()close the autocomplete
import { Component } from  '@angular/core';
import { ItemService } from  './services/itm-service';

@Component({
selector:  'app-root',
template:  `<remote-autocomplete [service]="itemService" #autocomplete></remote-autocomplete>`,
providers: [ ItemService ]
})
export  class  AppComponent {

@ViewChild('autocomplete') autocompleteField:  Autocomplete;
constructor(public  itemService:  ItemService) {
}

focus(){
this.autocompleteField.focus();
}
}

Template

The basic template will be used as default:

 <ng-template #defaultOptionTemplate>
    <span class="autocomplete-option-value">{{item.value}}</span>
    <span *ngIf="item.description" class="autocomplete-option-description">{{item.description}}</span>
</ng-template>

If you want to use some custom template you need to put this custom template into the remote-autocomplete component with remote-autocomplete-option-tmpl directive where item is a type of AutocompleteItem.

<remote-autocomplete [service]="itemService">
	<div *remote-autocomplete-option-tmpl="let item">
		<span>{{item.value}}</span>
	</div>
</remote-autocomplete>

ngModel

You can bind the value to the remote-autocomplete component as in example. If the value is defined on the moment of the initialization of your component then the bind value will be set to the input field.

import { Component } from  '@angular/core';
import { ItemService } from  './services/itm-service';

@Component({
selector:  'app-root',
template:  `<remote-autocomplete [service]="itemService" [(ngModel)]="value"></remote-autocomplete>`,
providers: [ ItemService ]
})
export  class  AppComponent {
private value: string = 'test';

constructor(public  itemService:  ItemService) {
}

License

MIT

1.1.9

5 years ago

1.1.8

5 years ago

1.1.7

5 years ago

1.1.6

5 years ago

1.1.5

5 years ago

1.1.4

5 years ago

1.1.3

5 years ago

1.1.2

5 years ago

1.1.1

5 years ago

1.1.0

6 years ago

1.0.8

6 years ago

1.0.7

6 years ago

1.0.6

6 years ago

1.0.5

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago