2.3.0 • Published 10 months ago

ng-super-select v2.3.0

Weekly downloads
-
License
-
Repository
-
Last release
10 months ago

Angular ng-super-select - Lightweight all in one UI Select, Multiselect and Autocomplete

See Demo page.

| Angular| ng-super-select|autocomplete|multi-select|searchable-dropdown|


Getting started

Step 1: Install ng-super-select:

NPM

npm install --save ng-super-select

Step 2: Import the NgSuperSelectModule:

import { NgSuperSelectModule } from 'ng-super-select';

@NgModule({
  declarations: [AppComponent],
  imports: [NgSuperSelectModule, ...],
  bootstrap: [AppComponent]
})
export class AppModule {}

Usage

Example with array of object

Define options in your consuming component if you are using isDataObject as true:

@Component({...})
export class ExampleComponent {
    selectedEmployee!: number;
    employee =  [
		{ id:  1, name:  'Rohit Bhagat'  },
		{ id:  2, name:  'Sayon Chakraborty'  },
		{ id:  3, name:  'Pritam Paul'  },
		{ id:  4, name:  'Sumit Kumar'  },
		{ id:  6, name:  'Tamal Dutta'  },
		{ id:  7, name:  'Shivam Bhaskar'  },
		{ id:  8, name:  'Krishna Pada Jana'  }
	];
	changeEmployee(id: number){
		this.selectedEmployee = id;
	}
}

In template use ng-super-select component with your options if you use searchable & multi-selectable

<ng-super-select  [isSearchable]="true"  [optionArray]="employee" [isMultiSelectable]="true" [bindName]="'name'"   [bindValue]="'id'"  [isDataObject]="true"  (changeSuperSelect)="changeEmployee($event)"></ng-super-select>

/* 
For pre select you can use preSelectData 
<ng-super-select [preSelectData]="[1,2]" [isSearchable]="true"  [optionArray]="employee" [isMultiSelectable]="true" [bindName]="'name'"   [bindValue]="'id'"  [isDataObject]="true"  (changeSuperSelect)="changeEmployee($event)"></ng-super-select>
*/

In template use ng-super-select component with your options if you use not-searchable but multi-selectable

<ng-super-select  [isSearchable]="false"  [optionArray]="employee" [isMultiSelectable]="true" [bindName]="'name'"   [bindValue]="'id'"  [isDataObject]="true"  (changeSuperSelect)="changeEmployee($event)"></ng-super-select>

/* 
For pre select you can use preSelectData 
<ng-super-select [preSelectData]="[1,2]" [isSearchable]="false"  [optionArray]="employee" [isMultiSelectable]="true" [bindName]="'name'"   [bindValue]="'id'"  [isDataObject]="true"  (changeSuperSelect)="changeEmployee($event)"></ng-super-select>
*/

In template use ng-super-select component with your options if you use searchable but not multi-selectable

<ng-super-select  [isSearchable]="true"  [optionArray]="employee" [isMultiSelectable]="false" [bindName]="'name'"  [bindValue]="'id'"  [isDataObject]="true"  (changeSuperSelect)="changeEmployee($event)"></ng-super-select>

/* 
For pre select you can use preSelectData 
<ng-super-select [preSelectData]="1" [isSearchable]="true"  [optionArray]="employee" [isMultiSelectable]="false" [bindName]="'name'"  [bindValue]="'id'"  [isDataObject]="true"  (changeSuperSelect)="changeEmployee($event)"></ng-super-select>
*/

In template use ng-super-select component with your options if you use not-searchable and not multi-selectable

<ng-super-select  [isSearchable]="false"  [optionArray]="employee" [isMultiSelectable]="false" [bindName]="'name'"   [bindValue]="'id'"  [isDataObject]="true"  (changeSuperSelect)="changeEmployee($event)"></ng-super-select>

/* 
For pre select you can use preSelectData 
<ng-super-select [preSelectData]="1" [isSearchable]="false"  [optionArray]="employee" [isMultiSelectable]="false" [bindName]="'name'"   [bindValue]="'id'"  [isDataObject]="true"  (changeSuperSelect)="changeEmployee($event)"></ng-super-select>
*/

Example with array of string

Define options in your consuming component if you are using isDataObject as false:

@Component({...})
export class ExampleComponent {
    selectedEmployee!: string;
    employee =  [
		'Rohit Bhagat',
		'Sayon Chakraborty',
		'Pritam Paul',
		'Sumit Kumar',
		'Tamal Dutta',
		'Shivam Bhaskar',
		'Krishna Pada Jana'
	];
	changeEmployee(name: string){
		this.selectedEmployee = name;
	}
}

In template use ng-super-select component with your options if you use searchable & multi-selectable

<ng-super-select  [isSearchable]="true"  [optionArray]="employee" [isMultiSelectable]="true"   [isDataObject]="false"  (changeSuperSelect)="changeEmployee($event)"></ng-super-select>

/* 
For pre select you can use preSelectData 
<ng-super-select [preSelectData]="['Rohit Bhagat', 'Sayon Chakraborty']" [isSearchable]="true"  [optionArray]="employee" [isMultiSelectable]="true"   [isDataObject]="false"  (changeSuperSelect)="changeEmployee($event)"></ng-super-select>
*/

In template use ng-super-select component with your options if you use not-searchable but multi-selectable

<ng-super-select  [isSearchable]="false"  [optionArray]="employee" [isMultiSelectable]="true"   [isDataObject]="false"  (changeSuperSelect)="changeEmployee($event)"></ng-super-select>

/* 
For pre select you can use preSelectData 
<ng-super-select  [preSelectData]="['Rohit Bhagat', 'Sayon Chakraborty']" [isSearchable]="false"  [optionArray]="employee" [isMultiSelectable]="true"   [isDataObject]="false"  (changeSuperSelect)="changeEmployee($event)"></ng-super-select>
*/

In template use ng-super-select component with your options if you use searchable but not multi-selectable

<ng-super-select  [isSearchable]="true"  [optionArray]="employee" [isMultiSelectable]="false"   [isDataObject]="false"  (changeSuperSelect)="changeEmployee($event)"></ng-super-select>

/* 
For pre select you can use preSelectData 
<ng-super-select [preSelectData]="'Rohit Bhagat'"  [isSearchable]="true"  [optionArray]="employee" [isMultiSelectable]="false"   [isDataObject]="false"  (changeSuperSelect)="changeEmployee($event)"></ng-super-select>
*/

In template use ng-super-select component with your options if you use not-searchable and not multi-selectable

<ng-super-select  [isSearchable]="false"  [optionArray]="employee" [isMultiSelectable]="false"   [isDataObject]="false"  (changeSuperSelect)="changeEmployee($event)"></ng-super-select>

/* 
For pre select you can use preSelectData 
<ng-super-select [preSelectData]="'Rohit Bhagat'" [isSearchable]="false"  [optionArray]="employee" [isMultiSelectable]="false"   [isDataObject]="false"  (changeSuperSelect)="changeEmployee($event)"></ng-super-select>
*/

Example with api

Add a function to listen change event in your component if you are using apiUrl:

@Component({...})
export class ExampleComponent {
	changeEmployee(data: any) {    
    this.selectedEmployee = data;
  }
}

In template use ng-super-select component if you are using api for showing option and in response you get array

<ng-super-select [isSearchable]="true" [isMultiSelectable]="true" [isDataObject]="true" [bindValue]="'id'"
  [bindName]="'title'" [apiUrl]="'https://jsonplaceholder.typicode.com/posts'"
  (changeSuperSelect)="changeEmployee($event)"></ng-super-select>

/* 
For pre select you can use preSelectData 
<ng-super-select [preSelectData]="[1, 2]" [isSearchable]="true" [isMultiSelectable]="true" [isDataObject]="true"
  [bindValue]="'id'" [bindName]="'title'" [apiUrl]="'https://jsonplaceholder.typicode.com/posts'"
  (changeSuperSelect)="changeEmployee($event)"></ng-super-select>
*/

In template use ng-super-select component if you are using api for showing option and in response you get array and you want api should call on every input

<ng-super-select [isSearchable]="true" [isMultiSelectable]="true" [isDataObject]="true" [bindValue]="'id'"
  [bindName]="'title'" [apiOnSearch]="true" [apiUrl]="'https://jsonplaceholder.typicode.com/posts?search='"
  (changeSuperSelect)="changeEmployee($event)">
</ng-super-select>

/* 
For pre select you can use preSelectData 
<ng-super-select [preSelectData]="[1, 2]" [isSearchable]="true" [isMultiSelectable]="true" [isDataObject]="true"
  [bindValue]="'id'" [bindName]="'title'" [apiOnSearch]="true"
  [apiUrl]="'https://jsonplaceholder.typicode.com/posts?search='" (changeSuperSelect)="changeEmployee($event)">
</ng-super-select>
*/

In template use ng-super-select component if you are using api for showing option and in response you get object and and inside that object there is array for showing option

entries=>response['entries']=>response.entries

<ng-super-select [responseKey]="'entries'" [isSearchable]="true" [isMultiSelectable]="true" [isDataObject]="true"
  [bindValue]="'Link'" [bindName]="'Description'" [apiUrl]="'https://api.publicapis.org/entries'"
  (changeSuperSelect)="changeEmployee($event)"></ng-super-select>

/* 
For pre select you can use preSelectData 
<ng-super-select [preSelectData]="['https://alexwohlbruck.github.io/cat-facts/']" [responseKey]="'entries'" [isSearchable]="true" [isMultiSelectable]="true" [isDataObject]="true"
  [bindValue]="'Link'" [bindName]="'Description'" [apiUrl]="'https://api.publicapis.org/entries'"
  (changeSuperSelect)="changeEmployee($event)"></ng-super-select>
*/

In template use ng-super-select component if you are using api for showing option and in response you get object and and inside that object there is array for showing option

source.0.measures=>response['source'][0]['measures']=>response.source[0].measures

<ng-super-select [responseKey]="'source.0.measures'" [isSearchable]="true" [isMultiSelectable]="true"
  [isDataObject]="false" [apiOnSearch]="true"
  [apiUrl]="'https://datausa.io/api/data?drilldowns=Nation&measures=Population'"
  (changeSuperSelect)="changeEmployee($event)"></ng-super-select>

/* 
For pre select you can use preSelectData 
<ng-super-select [preSelectData]="['Population']" [responseKey]="'source.0.measures'" [isSearchable]="true" [isMultiSelectable]="true"
  [isDataObject]="false" [apiOnSearch]="true"
  [apiUrl]="'https://datausa.io/api/data?drilldowns=Nation&measures=Population'"
  (changeSuperSelect)="changeEmployee($event)"></ng-super-select>
*/

Example with Add New Record

@Component({...})
export class ExampleComponent {

  @ViewChild('ngSuperSelect1')
  ngSuperSelect1!: NgSuperSelectComponent;
  @ViewChild('ngSuperSelect2')
  ngSuperSelect2!: NgSuperSelectComponent;
  @ViewChild('NgSuperSelectComponent')
  ngSuperSelect3!: NgSuperSelectComponent;
  @ViewChild('NgSuperSelectComponent')
  ngSuperSelect4!: NgSuperSelectComponent;
  
	changeEmployee(id: number | number[]) {
    this.selectedEmployee = id;
    alert(id);
  }

  addNewRecord(value: string, example: number) {
    alert(`We need to add a new record: ${value}`); // this will your api call for saving the new record
    switch (example) {
      case 1:
        let newDbValue1 = this.example1.length + 1;
        this.example1 = [...this.example1, { id: newDbValue1, name: value }]
        this.ngSuperSelect1.updateNewlyAddedRecord(newDbValue1, this.example1);
        break;
      case 2:
        let newDbValue2 = this.example2.length + 1;
        this.example2 = [...this.example2, { id: newDbValue2, name: value }]
        this.ngSuperSelect2.updateNewlyAddedRecord(newDbValue2, this.example2);
        break;
      default:
        break;
    }
  }
  
}
<!-- Example for multi selectable & array of object -->
<ng-super-select [isSearchable]="true" [optionArray]="example1" [isMultiSelectable]="true" [bindName]="'name'"
  [bindValue]="'id'" #ngSuperSelect1 [addNewRecordRequired]="true" (addNewRecord)="addNewRecord($event, 1)"
  [isDataObject]="true" (changeSuperSelect)="changeEmployee($event)"></ng-super-select>
<!-- Example for single selectable & array of object -->
<ng-super-select #ngSuperSelect2 [isSearchable]="true" [optionArray]="example2" [isMultiSelectable]="false"
  [bindName]="'name'" [bindValue]="'id'" [addNewRecordRequired]="true" (addNewRecord)="addNewRecord($event, 2)"
  [isDataObject]="true" (changeSuperSelect)="changeEmployee($event)"></ng-super-select>
<!-- Example for single selectable & array of string/number -->
<ng-super-select [isSearchable]="true" [optionArray]="example3" [isMultiSelectable]="true" [addNewRecordRequired]="true"
  (addNewRecord)="addNewRecord($event, 3)" [isDataObject]="false"
  (changeSuperSelect)="changeEmployee($event)"></ng-super-select>
<!-- Example for single selectable & array of string/number -->
<ng-super-select [isSearchable]="true" [optionArray]="example4" [isMultiSelectable]="false"
  [addNewRecordRequired]="true" (addNewRecord)="addNewRecord($event, 4)" (addNewRecord)="addNewRecord($event, 4)"
  [isDataObject]="false" (changeSuperSelect)="changeEmployee($event)"></ng-super-select>

API

Inputs

InputTypeDefaultRequiredDescription
placeholderstringif searchable then Type here to search otherwise Please select a optionnoWhat label you want to show
preSelectDatanumber[] or string[] or string or numbernull if isMultiSelectable true otherwise []noIf you want to pre select any option(s)
noOptionMessagestringNo data availablenoIf there is no data in then what message you wnat to show
optionArrayarray[]noTo show options
isSearchablestringfalsenoIs search required for the select or not
bindValuestringnullyes (if you didn't pass isDataObject or isDataObject=true)What value you want, after select the data
bindNamestringnullyes (if you didn't pass isDataObject or isDataObject=true)What value you want to display in option
isMultiSelectablebooleanfalsenoMore than one option can be choosen
isDataObjectbooleantruefalseIf your arary is array of object then pass true or leave it otherwise pass false
showDoneButtonbooleantruenoIt will shown only for multi selectable dropdowns and if you want to hide then pass false
disablebooleanfalsenoIt will make the select disable
apiUrlstringnoIf you provide this parameter then option will shown based on the api response
responseKeystringnoProvide if you are using apiUrl & array is coming inside the object in api response
apiOnSearchbooleanfalsenoIf you want api call only one time then use false or didn't pass it and if you want api call on search(Typing) then pass true
delaynumber400noHow much time(milisecond) it should wait to check whether typing is complete or not
addNewRecordRequiredstringfalsenoTo show add new record option instead of no record option
addNewRecordLabelstringAdd new record:noTo give custom label

Outputs

OutputDescription
(changeSuperSelect)This will triggered when selection value will change & you get the selected value(s)
(addNewRecord)This will triggered when you clicked on the add new record option it will also provide the searched value as a parameter