You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: ej2-javascript/rich-text-editor/smart-editing/mail-merge.md
+90-58Lines changed: 90 additions & 58 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,30 +3,32 @@ layout: post
3
3
title: Mail Merge in ##Platform_Name## Rich Text Editor Control | Syncfusion
4
4
description: Learn all about Mail Merge in the Syncfusion ##Platform_Name## Rich Text Editor control, part of Essential JS 2.
5
5
platform: ej2-javascript
6
-
control: Mail Merge
6
+
control: Mail Merge
7
7
publishingplatform: ##Platform_Name##
8
8
documentation: ug
9
9
domainurl: ##DomainURL##
10
10
---
11
-
11
+
12
12
# Mail merge in ##Platform_Name## Rich Text Editor Control
13
-
13
+
14
14
The Rich Text Editor can be customized to implement **Mail Merge** functionality by inserting placeholders into the editor using custom toolbar items. These placeholders are later replaced with actual data to generate personalized content such as letters, invoices, and reports.
15
-
15
+
16
16
This feature simplifies the creation of dynamic documents by allowing users to insert merge fields that are automatically populated with real data during content generation.
17
-
17
+
18
18
## Adding custom toolbar items for inserting merge fields
19
-
19
+
20
20
To enable mail merge functionality, the Rich Text Editor toolbar is extended with two custom buttons: `Insert Field` and `Merge Data`. These buttons are added using the `template` property in [toolbarSettings.items](https://ej2.syncfusion.com/documentation/api/rich-text-editor/toolbarsettings#items), which points to custom HTML elements (#insertField and #merge_data).
21
-
21
+
22
22
-**Insert Field:** Opens a dropdown list of merge fields for inserting placeholders like `{{FirstName}}` into the editor.
23
23
-**Merge Data:** Replaces all placeholders in the editor with actual values from a predefined data source.
24
-
24
+
25
25
{% if page.publishingplatform == "typescript" %}
26
-
26
+
27
27
{% tabs %}
28
28
{% highlight ts tabtitle="app.ts" %}
29
-
29
+
30
+
{% raw %}
31
+
30
32
const mailMergeEditor: RichTextEditor = new RichTextEditor({
31
33
toolbarSettings: {
32
34
items: [
@@ -39,15 +41,19 @@ const mailMergeEditor: RichTextEditor = new RichTextEditor({
var mailMergeEditor = new ej.richtexteditor.RichTextEditor({
52
58
toolbarSettings: {
53
59
items: [
@@ -60,20 +66,24 @@ var mailMergeEditor = new ej.richtexteditor.RichTextEditor({
60
66
},
61
67
});
62
68
mailMergeEditor.appendTo('#mailMergeEditor');
63
-
69
+
70
+
{% endraw %}
71
+
64
72
{% endhighlight %}
65
73
{% endtabs %}
66
74
{% endif %}
67
-
75
+
68
76
## Using DropDownButton for selecting placeholders
69
-
77
+
70
78
The **DropDownButton** component displays a list of merge fields such as First Name, Last Name, and Company Name. When a user selects an item, the corresponding placeholder (e.g., {{FirstName}}) is inserted at the current cursor position using the `insertHTML` command.
71
-
79
+
72
80
{% if page.publishingplatform == "typescript" %}
73
-
81
+
74
82
{% tabs %}
75
83
{% highlight ts tabtitle="app.ts" %}
76
-
84
+
85
+
{% raw %}
86
+
77
87
let insertField: DropDownButton = new DropDownButton({
78
88
items: [
79
89
{ text: 'First Name' },
@@ -90,7 +100,7 @@ let insertField: DropDownButton = new DropDownButton({
90
100
select: onItemSelect,
91
101
});
92
102
insertField.appendTo('#insertField');
93
-
103
+
94
104
function onItemSelect(args: MenuEventArgs): void {
95
105
const value = textToValueMap[args.item.text];
96
106
mailMergeEditor.executeCommand(
@@ -99,15 +109,19 @@ function onItemSelect(args: MenuEventArgs): void {
var insertField = new ej.splitbuttons.DropDownButton({
112
126
items: [
113
127
{ text: 'First Name' },
@@ -123,7 +137,7 @@ var insertField = new ej.splitbuttons.DropDownButton({
123
137
select: onItemSelect,
124
138
});
125
139
insertField.appendTo('#insertField');
126
-
140
+
127
141
function onItemSelect(args) {
128
142
if (args.item.text != null) {
129
143
var value = textToValueMap[args.item.text];
@@ -138,85 +152,101 @@ function onItemSelect(args) {
138
152
);
139
153
}
140
154
}
141
-
155
+
156
+
{% endraw %}
157
+
142
158
{% endhighlight %}
143
159
{% endtabs %}
144
160
{% endif %}
145
-
161
+
146
162
## Populating merge fields using Mention
147
-
163
+
148
164
The **Mention** control provides an alternative way to insert placeholders by typing the <code>{{</code> character inside the editor. A popup list of merge fields appears, allowing quick selection without using the toolbar.
## Replacing placeholders with actual data dynamically
192
-
216
+
193
217
When the **Merge Data** button is clicked, the editor content is processed to replace all placeholders with actual values from the `placeholderData` object. This is done using a regular expression in the `replacePlaceholders()` function.
0 commit comments