GetChildrenAsList (Sorting and Filtering)

<< Click to Display Table of Contents >>

Navigation:  Programming > Create text under program control > Paragraphs > TParagraph API >

GetChildrenAsList (Sorting and Filtering)

This methods moves all the children of a (typically table) paragraph into a list of type TWPParagraphList. The paragraph will then have no children anymore!

 

The list can then be processed, i.e. sorted, and then moved back into the paragraph using SetChildrenFromList.

 

WPTools 8 introduces the class TWPListOfParagraphList which is used, as the classname implies, to list TWPParagraphList. This list-of-list is used to sort rows in tables. It is filled from one TWPParagraphList by the method LoadItems which also accepts several mode flags.

Using the sub lists, it is possible to keep groups of data together so multiple rows can be sorted under "their" header row. This is used for the display of relational data in a table.

 

The lists also allow filtering. It is possible to hide certain rows when certain conditions are met or not met.

 

For better understanding we copy the code of the method TWPRichText.TableFilterSort which uses both, TWPParagraphList and TWPListOfParagraphList:

 

 

procedure TWPCustomRtfEdit.TableFilterSort(

        aTable : TParagraph;

        CompareMode : TWPParagraphCompareModes;

        RowCompare : TWPParagraphListCompareObj;

        RowFilterMethod : TWPParagraphListCheckFilter = nil;

        Mode : TWPListLoadItemsMode = [wpListSubListOnGROUPLEVEL,wpListSubListOnRowMerge];

        CompareParam : Integer=0;

        CompareContext : TObject = nil;

        TemplatePar : TParagraph = nil);

var list : TWPParagraphList;

   listlist : TWPListOfParagraphList;

begin

if aTable<>nil then

begin

       listlist := TWPListOfParagraphList.Create;

      list := aTable.GetChildrenAsList;

      try

          listlist.LoadItems(list, Mode,CompareParam);

          try

            if assigned(RowCompare) then

                   listlist.Sort( RowCompare, CompareParam, CompareMode, CompareContext, TemplatePar );

            if assigned(RowFilterMethod) then

                   listlist.Filter( CompareParam, CompareContext, RowFilterMethod, false, TemplatePar );

          except

            // try to recover from exception. Restore the table

            on e : Exception do ShowMessage(e.Message);

          end;

           listlist.WriteItems(list);

           aTable.SetChildrenFromList(list);

           ReformatAll(false, true);

      finally

         listlist.Free;

         list.Free;

      end;

end;

end;

 

 

Modes for LoadItems:

 

TWPListLoadItemsMode = set of (

   wpListIgnoreHeaderRows, // Header rows are treated like data

   wpListIgnoreFooterRows, // Footer rows are treated like data

   wpListSimpleDataRows,   // No sub lists are created, all data rows are moved to DataList

   wpListCompareRowNames,  // Subsequent rows with the same name will go into a sub list

   wpListCompareRowIDs,    // Subsequent rows with the same ParID will go into a sub list

   wpListSubListOnGROUPLEVEL, // default: rows with a higher WPAT_PAR_GROUPLEVEL go into sublist

   wpListSubListOnRowMerge,  // default: rows which contain a rowspan column go into sublist

   wpListSubListOnTextInColumn, // If the column with the given number is not a number, start a new list

   // Note: Like wpListSimpleDataRows this will handle each row individually when sorting and not maintain groups!

   wpListSameColumnCount // Sublist when row count changes

   // wpListSubListOnTextInColumn and  wpListSameColumnCount is used as "AND" condition with the other

   // conditions. The other conditions are "OR" conditions but can also be left out.

   // This is useful to sort only rows with numbers and do not change the captions in-between!

 );

 

Modes for Sort:

 

TWPParagraphCompareModes = set of (

    wpCompareSortReverse, // Sort from high to low

    wpCompareCasesensitive,

    wpCompareNumbers, // Special meaning of Param!

    wpCompareIgnoreText,

    // modifies behaviour of TWPListOfParagraphList.SortEx

    wpSortSubElements);