Mail Merge: Conditional Data Shaping in Merge Blocks
Rows of data in merge blocks can be sorted, filtered, and rendered conditionally. This example shows how to set the required JSON data on a merge block to conditionally render child blocks.

Rows of data in merge blocks can be sorted, filtered, and rendered conditionally. This example shows how to set the required JSON data on a merge block to conditionally render child blocks.
Template with Merge Blocks
Consider the following template, which consists of a merge block named lineItems with a nested merge block details.
JSON Data Source
The data source is a JSON object with two simple product line items.
[
{
"title": "Report Title",
"lineItems": [
{
"name": "product 1",
"price": 200,
"details": [
{
"shortDescription": "short description 1",
"description": "description 1"
}
]
},
{
"name": "product 2",
"price": 100,
"details": [
{
"shortDescription": "short description 2",
"description": "description 2"
}
]
}
]
}
]
The idea of the conditional data shaping is to render a child table (details) only when certain conditions of the parent table are met. The conditions can be defined in the form of a JSON object that is stored in the Sub
The following data shaping information is defined in the template by default:
{
"Name":"details",
"DataShapingInfo":{
"BlockMergingCondition":[
{
"ColumnName":"price",
"ComparisonOperator":0,
"CompareTo":"200",
"LogicalOperator":0,
"Culture":""
}
],
"Filters":[],
"SortingInstructions":[]
}
}
The condition is basically met when the parent column name price equals 200. In this case, the child merge block is rendered and the nested merge block of the second data row is not rendered.
Demo Application
This article describes a sample that is part of the technical live demos.
Live Demo
Try this demo for yourself by launching our technical demos.
In the demo, you can now play with the conditions by editing the JSON directly. Set the CompareTo value to 100 to render the nested merge block for the second data row and click Set Condition to apply the data shaping information.
The following comparison values are supported:
Operator | Description |
0 | Equal to |
1 | Greater than |
2 | Greater than or equal |
3 | Less than |
4 | Less than or equal |
5 | Not equal to |
6 | Is blank |
8 | Is not blank |
When you click the Set Conditions button, JavaScript is used to apply the JSON data to the merge block details:
function setCondition() {
TXTextControl.subTextParts.forEach(function (stp) {
stp.getName(function (name) {
if (name == "txmb_details") {
stp.setData(editor.getValue(), function () {
$("#ribbonTabReports_btnPreviewMergeResults").click();
});
}
})
})
}
Related Posts
Mail Merge: Inserting Merge Blocks using the DataSourceManager in C#
This article shows how to insert merge blocks into a document using the DataSourceManager class in C#. Merge blocks are used to repeat a block of content for each data record in a data source. The…
Use MailMerge in .NET on Linux to Generate Pixel-Perfect PDFs from DOCX…
This article explores how to use the TX Text Control MailMerge feature in .NET applications on Linux to generate pixel-perfect PDFs from DOCX templates. This powerful combination enables…
Generating Dynamic NDAs Using TX Text Control MailMerge in C# .NET
This article demonstrates how to generate dynamic NDAs using TX Text Control MailMerge in C# .NET. It covers the process of creating a template, binding data, and generating the final document.
Designing a Maintainable PDF Generation Web API in ASP.NET Core (Linux) C#…
This article shows how to create a PDF generation Web API in ASP.NET Core on Linux using TX Text Control .NET Server. The clean architecture is used to create a maintainable and testable solution.
Getting Started: ServerTextControl and MailMerge in a .NET 8 Console…
This article shows how to create a .NET 8 console application on Linux using Docker and WSL that uses the ServerTextControl to create a document and MailMerge to merge JSON data into the document.…