[reportlab-users] Keeping rows of a table together?

Erik Westra reportlab-users@reportlab.com
Tue, 17 Jun 2003 07:19:54 +1200


Hi Andy,

Following on from my previous E-Mail...

> > Could I, perhaps, create a subclass of platypus.Table and
> > override the split() method in some way?
>
>If you were brave, yes :-)

I started digging into Table.split(), and I was able to add this logic very 
easily.  All that was required was adding two lines of code to 
Table._splitRows(), like this:

     def _splitRows(self,availHeight):
         h = 0
         n = 0
         lim = len(self._rowHeights)
         while n<lim:
             hn = h + self._rowHeights[n]
             if hn>availHeight: break
             h = hn
             n = n + 1

 >>>     while (n > 0) and self._dontSplitAtRow[n]:
 >>>         n = n - 1

Of course, defining the contents of the _dontSplitRow list requires a bit 
more work, but I achieved this by adding the following to the end of the 
Table constructor:

         self._dontSplitAtRow = [False] * len(data)

and adding an extra method to let the user specify where not to split the 
table at a particular row:

     def dontSplitAtRow(self, row):
         """ Prevent the table from doing a page break at the given row.
         """
         self._dontSplitAtRow[row] = True

That's all it took!  I can't believe how easy this was...so I thought I'd 
pass this on in case anyone else found it useful.  I'd also be happy to 
submit this as a patch to the reportlab source if you think it'd be 
worthwhile and won't cause any unwanted side-effects.

Cheers,

  - Erik.