- Binding local data
- Binding remote data
- See Also
Contact Support
Working with data in Angular Mention component
27 Apr 202412 minutes to read
The Mention loads the data either from local data sources or remote data services using the dataSource property. It supports the data type of either array
or DataManager
.
The Mention also supports different kinds of data services such as OData V4 and Web API, and data formats such as XML, JSON, and JSONP with the help of DataManager
adaptors.
Fields | Type | Description |
---|---|---|
text | string |
Specifies the display text of each list item. |
value | number or string |
Specifies the hidden data value mapped to each list item that should contain a unique value. |
groupBy | string |
Specifies the category under which the list item has to be grouped. |
iconCss | string |
Specifies the icon class of each list item. |
When binding complex data to the Mention, fields should be mapped correctly. Otherwise, the selected item remains undefined.
Binding local data
Local data can be represented in three ways as described in the following.
Array of simple data
The Mention has provided support to load an array of primitive data such as strings and numbers. Here, both the value and text fields act the same.
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { FormsModule, ReactiveFormsModule } from '@angular/forms'
import { MentionModule } from '@syncfusion/ej2-angular-dropdowns'
import { Component } from '@angular/core';
@Component({
imports: [
FormsModule, ReactiveFormsModule, MentionModule
],
standalone: true,
selector: 'app-root',
template: `<label id="comment" >Comments</label>
<div id="mentionElement" placeholder = "Type @ and tag user"></div>
<ejs-mention [dataSource]='userData' [target]='mentionTarget' ></ejs-mention>`,
})
export class AppComponent {
constructor() {
}
// Defines the array of data.
public userData: string[] = ['Selma Rose', 'Garth', 'Robert', 'William', 'Joseph'];
// Defines the target in which the mention component is rendered.
public mentionTarget: string = "#mentionElement";
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));
Array of JSON data
The Mention can generate its list items through an array of complex data. Therefore, the appropriate columns should be mapped to the fields property.
In the following example, ID
column and Game
column from complex data have been mapped to the value
field and text
field, respectively.
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { FormsModule, ReactiveFormsModule } from '@angular/forms'
import { MentionModule } from '@syncfusion/ej2-angular-dropdowns'
import { Component } from '@angular/core';
import { FieldSettingsModel } from '@syncfusion/ej2-dropdowns';
@Component({
imports: [
FormsModule, ReactiveFormsModule, MentionModule
],
standalone: true,
selector: 'app-root',
template: `<label id="comment" >Comments</label>
<div id="mentionElement" placeholder = "Type @ and tag sport"></div>
<ejs-mention [dataSource]='sportsData' [target]='mentionTarget' [fields]='fields' ></ejs-mention>`,
})
export class AppComponent {
constructor() {
}
public sportsData: { [key: string]: Object }[] = [
{ ID: 'game1', Game: 'Badminton' },
{ ID: 'game2', Game: 'Football' },
{ ID: 'game3', Game: 'Tennis' },
{ ID: 'game4', Game: 'Hockey' },
{ ID: 'game5', Game: 'Basketball' }
];
public mentionTarget: string = "#mentionElement";
public fields: Object = { text: 'Game', value: 'ID' }
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));
Array of Complex data
The Mention can generate its list items through an array of complex data. For this, the appropriate columns should be mapped to the fields property.
In the following example, Code.ID
column and Country.Name
column from complex data have been mapped to the value
field and text
field, respectively.
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { FormsModule, ReactiveFormsModule } from '@angular/forms'
import { MentionModule } from '@syncfusion/ej2-angular-dropdowns'
import { Component } from '@angular/core';
import { FieldSettingsModel } from '@syncfusion/ej2-dropdowns';
@Component({
imports: [
FormsModule, ReactiveFormsModule, MentionModule
],
standalone: true,
selector: 'app-root',
template: `<label id="comment" >Comments</label>
<div id="mentionElement" placeholder = "Type @ and tag country"></div>
<ejs-mention [dataSource]='countriesData' [target]='mentionTarget' [fields]='fields' ></ejs-mention>`,
})
export class AppComponent {
constructor() {}
public countriesData: { [key: string]: Object }[] = [
{ Country: { Name: 'Australia' }, Code: { ID: 'AU' } },
{ Country: { Name: 'Bermuda' }, Code: { ID: 'BM' } },
{ Country: { Name: 'Canada' }, Code: { ID: 'CA' } },
{ Country: { Name: 'Cameroon' }, Code: { ID: 'CM' } },
{ Country: { Name: 'Denmark' }, Code: { ID: 'DK' } },
{ Country: { Name: 'France' }, Code: { ID: 'FR' } },
];
public mentionTarget: string = '#mentionElement';
public fields: Object = { text: 'Country.Name',value: 'Code.ID'};
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));
Binding remote data
The Mention supports retrieval of data from remote data services with the help of DataManager
component. The query property is used to fetch the data from the database and bind it to the Mention component.
OData v4 adaptor - Binding OData v4 service
The ODataV4 is an improved version of OData protocols, and the DataManager
can also retrieve and consume OData v4 services. For more details on OData v4 services, refer to the odata documentation. To bind OData v4 service, use the ODataV4Adaptor
.
The following sample displays the first 6 contacts from Customers
table of the Northwind
Data Service.
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { FormsModule, ReactiveFormsModule } from '@angular/forms'
import { MentionModule } from '@syncfusion/ej2-angular-dropdowns'
import { Component } from '@angular/core';
import { Query, DataManager, ODataV4Adaptor } from '@syncfusion/ej2-data';
import { FieldSettingsModel } from '@syncfusion/ej2-dropdowns';
@Component({
imports: [
FormsModule, ReactiveFormsModule, MentionModule
],
standalone: true,
selector: 'app-root',
template: `<label id="comment" >Comments</label>
<div id="mentionElement" placeholder = "Type @ and tag user"></div>
<ejs-mention [dataSource]='searchData' [query]='query' [fields]='fields' [popupWidth]='popupWidth' [target]='mentionTarget'></ejs-mention>`,
})
export class AppComponent {
constructor() {}
public searchData: DataManager = new DataManager({
url: 'https://services.odata.org/V4/Northwind/Northwind.svc/',
adaptor: new ODataV4Adaptor,
crossDomain: true
});
public mentionTarget: string = '#mentionElement';
public query: Query = new Query().from('Customers').select(['ContactName', 'CustomerID']).take(6);
public fields: Object = { text: 'ContactName', value: 'CustomerID' };
public popupWidth:string = '250px'
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));
Web API adaptor
You can use WebApiAdaptor
to bind mention with Web API created using OData endpoint.
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { FormsModule, ReactiveFormsModule } from '@angular/forms'
import { MentionModule } from '@syncfusion/ej2-angular-dropdowns'
import { Component } from '@angular/core';
import { Query, DataManager, WebApiAdaptor } from '@syncfusion/ej2-data';
@Component({
imports: [
FormsModule, ReactiveFormsModule, MentionModule
],
standalone: true,
selector: 'app-root',
template: `<label id="comment" >Comments</label>
<div id="mentionElement" placeholder = "Type @ and tag user"></div>
<ejs-mention [dataSource]='searchData' [query]='query' [fields]='fields' [popupWidth]='popupWidth' [target]='mentionTarget'></ejs-mention>`,
})
export class AppComponent {
constructor() {}
public searchData: DataManager = new DataManager({
url: 'https://services.syncfusion.com/angular/production/api/Employees',
adaptor: new WebApiAdaptor,
crossDomain: true
});
public mentionTarget: string = '#mentionElement';
public query: Query = new Query().select(['FirstName', 'EmployeeID']).take(7);
public fields: Object = { text: 'FirstName', value: 'EmployeeID' };
public popupWidth: string = '250px';
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));