4D Blog

Home Product 4D Write Pro: Dynamically Fill Tables with Entity Selections & Collections

4D Write Pro: Dynamically Fill Tables with Entity Selections & Collections

July 14, 2022

Product

As developers, we often create complex and advanced 4D Write Pro documents. We may generate tables that need to be filled from a data source, and we may not be sure of the exact rows and columns. Manually filling the tables with data every time is a laborious task. We heard you, and here is where data context comes as an invaluable option, opening the world of ORDA and object-oriented development to 4D Write Pro documents.

It is now possible to define a “datasource” for the tables of 4D Write Pro. Whether it is an entity selection or a collection, this “datasource” will automatically feed the rows of these tables.

Let’s delve into the details!

Dynamically Fill Tables with Entity Selections & Collections

 

A picture is worth 1000 words.

This feature enables you to create documents just like this one:

Once completed, it can be exported (as PDF, as Docx, or any other available export format.), printed, modified, etc. Let’s see, step by step, how to proceed!

 

Table, header row, “Template” row, Other rows

In a 4D Write Pro document, a table can now contain one or more header rows. The row that immediately follows the header will be considered a template row that will be repeated and filled by the content of the datasource elements. The following rows (if any) will be automatically “pushed” downwards and can contain more information, possibly also based on the table datasource. The original document used to generate the above report looks like this:

blank

The first frame, at the top, is filled with information provided by the data context. If you’re not familiar with this concept, we strongly recommend reading this blog post first. Let’s see how the table itself is defined.

In this case, the table contains three parts (detailed later):

  • The first row has been defined as a header.
  • The second row is the template row.
  • The following rows contain extra information based on the table datasource and calculations.

 

ASSOCIATION OF A DATA SOURCE TO A TABLE

As its name suggests, a data source provides data to a table. Therefore, this data source should be a formula whose result is either an entity selection or a collection. If it is not the case (after all, a formula can return anything), the result of the formula can still be accessed in the table (see below), but no row will be used as a template.

Two ways to proceed

#1: DEFINE THE DATASOURCE WITH THE User interface

The user interface has been modified so that a formula can be directly associated with a table.

Let’s imagine a “Company” table connected to an “Employee” table via an “employees” link. If a Company entity is accessible via a context in the document, then “employees” will be the datasource of the table.

The formula associated with the table can be This.data.company.employees.

 

blank

blank

… where This.data (the context) is going to be provided (later on) by programming:

$context:=New object("company";$company) // $company is a company entity
WP SET DATA CONTEXT(WParea;$context)

#2: DEFINE THE DATASOURCE BY PROGRAMMING

The datasource can also be set using a single line of code.
Each table can now hold a new attribute which can be defined with the WP SET ATTRIBUTES command.

WP SET ATTRIBUTES($table;wk data source;Formula(This.data.company.employees))

HOW TO DEFINE THE CONTENT OF THE ROWS?

Once the table datasource has been defined, it’s time to fill the rows!

New attributes are available thanks to the This keyword which is based on the datasource items.

In the template row, you can use This.item.xxx where xxx (lastname, address, zipCode…) will be the attributes of the current entity you want to access like This.item.name or This.item.age. You can also access the item’s index in the selection (or collection) with the attribute This.itemIndex …

blank

Once computed (using WP COMPUTE FORMULAS), the calculated rows can be displayed as seen in the introduction.

More table attributes!

Some other attributes can also be used anywhere in the table, usually in the following rows.

This.table.datasource returns the formula associated with the table. Still, it will probably be better to use This.tableData, whose value is the already evaluated result of this formula (usually a collection or a selection entity).

This optimizes the processing, of course, and ensures consistency in the document.

From this last attribute (tableData), it becomes easy to calculate sums, averages, and minimum or maximum values thanks to formulas like these:

This.tableData.min(age)
This.tableData.average(salary)

blank

Even more attributes?

New attributes have been added that can be reached in any table (even without a defined data source). They will allow having access to:

  • The table itself uses This.table,
  • The current row with This.row,
  • The current row number via This.rowIndex.

 

PRINT full reports in four lines of code!

Once this kind of document is created, it can be used as a template.

For each ($company; $companies)
  WP SET DATA CONTEXT($template; New object("company"; $company))
  WP PRINT($template) // (before printing, expressions will be recomputed by default)
End for each 

CUSTOMIZE REPORTS, LISTS, ETC.

You can also use a template whose tables have been filled with values from the datasource. You will be able to highlight rows, change colors and styles, and even remove or add empty rows!

The best way to do this is to freeze the document first so that all the inserted data is now regular rows and text. Freezing the document is not mandatory,  but you must be aware all your modifications will be lost if formulas are recomputed.

Finally, if your document has to be saved (not only printed or exported), then you should use a copy of your templates so that you don’t lose the templates themselves.

MyWorkingDocument :=WP New($template))

Conclusion

This new feature allows you to generate documents from templates. Their content can be based on ORDA with a minimum of programming lines.

These documents can be exported as PDF, Docx, eMail.  They can also be used as improvements to list forms and Print selection.

Automatic document generation is now greatly facilitated, whether it’s for invoices, catalogs, or inventories.

 

More details on the documentation center (here and there). And as always, feel free to get in touch on the 4D Forums.

 

Discuss

Tags 4D Write Pro, Data context, Formula, ORDA, Table, v19 R6, v20, Word processor

Latest related posts

  • June 16, 2025

    Generate, share and use web sessions One-Time Passcodes (OTP)

  • May 20, 2025

    Automatic Refresh of OEM Build Licenses

  • May 16, 2025

    New class to handle incoming TCP connections

Roland Lannuzel
Roland Lannuzel
• Product Owner & 4D Expert •After studying electronics, Roland went into industrial IT as a developer and consultant, building solutions for customers with a variety of databases and technologies. In the late 80’s he fell in love with 4D and has used it in writing business applications that include accounting, billing and email systems.Eventually joining the company in 1997, Roland’s valuable contributions include designing specifications, testing tools, demos as well as training and speaking to the 4D community at many conferences. He continues to actively shape the future of 4D by defining new features and database development tools.
  • Deutsch
  • Français
  • English
  • Português
  • Čeština
  • Español
  • Italiano
  • 日本語

Categories

Browse categories

  • 4D View Pro
  • 4D Write Pro
  • 4D for Mobile
  • Email
  • Development Mode
  • 4D Language
  • ORDA
  • User Interface / GUI
  • Qodly Studio
  • Server
  • Maintenance
  • Deployment
  • 4D Tutorials
  • Generic
  • 4D Summit sessions and other online videos

Tags

4D-Analyzer 4D for Android 4D for iOS 4D NetKit 4D Qodly Pro 4D View Pro 4D Write Pro 20 R8 20 R9 Administration Authentication Build application CI/CD Class Client/Server Code editor Collections Compatibility settings Formula Google Listbox Logs Mail Network Objects ORDA PDF Pictures Preemptive Programming Qodly Studio REST Scalability Security Session Source control Speed Spreadsheet Tutorial UI User Experience v20 vscode Web Word processor

Tags

4D-Analyzer 4D for Android 4D for iOS 4D NetKit 4D Qodly Pro 4D View Pro 4D Write Pro 20 R8 20 R9 Administration Authentication Build application CI/CD Class Client/Server Code editor Collections Compatibility settings Formula Google Listbox Logs Mail Network Objects ORDA PDF Pictures Preemptive Programming Qodly Studio REST Scalability Security Session Source control Speed Spreadsheet Tutorial UI User Experience v20 vscode Web Word processor
Subscribe to 4D Newsletter

© 2025 4D SAS - All rights reserved
Terms & Conditions | Legal Notices | Data Policy | Cookie Policy | Contact us | Write for us


Subscribe to 4D Newsletter

* Your privacy is very important to us. Please click here to view our Policy

Contact us

Got a question, suggestion or just want to get in touch with the 4D bloggers? Drop us a line!

* Your privacy is very important to us. Please click here to view our Policy