Search code examples
opentbstinybutstrong

Can I use TinyButStrong Fields to control paragraph formatting in my Word Table


I have a word table being populated from a MYSQL query and sub query via an OpenTBS template. The main query acts has a single cell merged across the row; the first column of the subquery is merged using an ope=mergecell command; the remaining columns are single cell.

Is there any way I can use openTBS fields to set paragraph 'keep with next' on all rows of the subquery except the last so that all rows of each sub record remain on the same page?

An excerpt from the template would be:

Template excerpt

So what I am essentially after after merging would be to have the green cells with 'Keep With Next' set and the red cells with that property unset.

enter image description here


Solution

  • Here is a solution

    For now TBS has no specific feature to know when a record is the last or not (TBS version 3.11.0). (there are the headergrp and footergrp feature but they do not act on the record but around the record)

    So, the first step is to get a an extra column in the data that says if its the last record or not. Let’s say the column is named is_last. Such a column can theorically be done with MySQL using a window function. Something like :

    (id = (LAST_VALUE(id) OVER (...)) AS is_last
    

    If you don’t succeed in having this column directly thought the MySQL query, then you can load all your data in a PHP array. Since your have a main block and sub-block, then you data should be structured as a recordset for the main block, and each record should have a sub-recordset stored in a sample_lst column (or whatever its name). Then you template should parameter sub1 instead of p1 in order to work with automatic subblocks. Now that your data is in a PHP variable, it is easy to visit the data and add the is_last column.

    Since you have the is_last colmun, the next steps are easy:

    1. In your template, make 2 conditional sections for the sub-block. On section with when [sub.is_last]=0, and the other with when [sub.is_last]=1. Knowing you seems to be already using conditional sections for this subblock, you have to arrange the condition in order to combine two conditions as for making an AND. See FAQ.
    2. In the section for is_last=0, turn off the Keep with next Ms Word property.
    3. In the section for is_last=1, turn on the Keep with next Ms Word property.