<< Click to Display Table of Contents >> Navigation: Programming > TableProducer - Dynamic Tables > Populate columns in code |
In the TableProducer demo we use a property grid to let the user select the master/client table, the connecting UID fields and the field names which should be listed in the created table. Further more the user can change the attributes of the cells with the field names (easily done due to flag EditOptionsEx2 wpAttrChangeOperateOnParAttr). The properties will be used when the table is created.
This is the grid which is read by code:
var editors, fields, fields_master, fields_detail : TWPInplaceEditorList;
ed : TWPToolsBaseWinControlInplaceEditor;
i : Integer;
template : TWPBlockTemplateDB;
begin
WPTableProducerDB1.Active := false;
DataModule1.DMASTER.Active := false;
DataModule1.DDETAIL.Active := false;
DataModule1.DMASTER.TableName := '';
DataModule1.DDETAIL.TableName := '';
editors := TWPInplaceEditorList.Create;
fields_master := TWPInplaceEditorList.Create;
fields_detail := TWPInplaceEditorList.Create;
try
InplaceEditorGetList is a function which extracts certain inplace editor objects. Here only the check boxes on the first level are extracted.
We find the master and the first client and extract the fields which were selected for each. To do soe we simply further examine the parent table of the selected field.
WPRichText1.InplaceEditorGetList(editors, false, true,true, [wpOnlyIfChecked]);
if editors.Count=0 then
raise Exception.Create('Please select master and detail tables.');
for i := 0 to editors.Count-1 do
begin
if editors[i].Editor is TWPToolsBaseWinControlInplaceEditor then
begin
ed := TWPToolsBaseWinControlInplaceEditor(editors[i].Editor);
if (ed.group=1) and (DataModule1.DMASTER.TableName='') then
begin
fields := TWPInplaceEditorList.Create;
try
Here we find the checkboxes in the parent table of the field which selected the table: editors[i].Par.ParentTable
WPRichText1.InplaceEditorGetList( fields, true, false, editors[i].Par.ParentTable,
[wpParAndChildren, wpUseGroupFilter,wpOnlyIfChecked], 3);
DataModule1.DMASTER.TableName := ed.Name;
if fields.Count>0 then
DataModule1.DDETAIL.MasterFields :=
fields.SecondNamePart(0);
finally
fields.Free;
end;
// Now reads the fields
WPRichText1.InplaceEditorGetList( fields_master, true, false, editors[i].Par.ParentTable,
[wpParAndChildren, wpUseGroupFilter,wpOnlyIfChecked], 4);
end;
if (ed.group=2) and (DataModule1.DDETAIL.TableName='') then
begin
fields := TWPInplaceEditorList.Create;
try
WPRichText1.InplaceEditorGetList( fields, true, false, editors[i].Par.ParentTable,
[wpParAndChildren, wpUseGroupFilter,wpOnlyIfChecked], 3);
if fields.Count>0 then
DataModule1.DDETAIL.IndexFieldNames :=
fields.SecondNamePart(0);
finally
fields.Free;
end;
DataModule1.DDETAIL.TableName := ed.Name;
// Now reads the fields
WPRichText1.InplaceEditorGetList( fields_detail, true, false, editors[i].Par.ParentTable,
[wpParAndChildren, wpUseGroupFilter,wpOnlyIfChecked], 4);
end;
end;
end;
if DataModule1.DMASTER.TableName='' then
raise Exception.Create('Please select at least a master tables.');
Now we populate the TWPTableProducer in code:
WPTableProducerDB1.Blocks.Clear;
template := WPTableProducerDB1.Blocks.Add('MASTER') as TWPBlockTemplateDB;
// this name links to the DataSourceLinks which were defined in the WPTableProducerDB1
template.DataSourceName :=
WPTableProducerDB1.DataSourceLinks[0].Name;
The last parameter in AddColumn is a TWPTextStyle, it contains the text attributes which should be used for this column. Since TParagraph (whgich is the clas which holds a paragraph or a cell) inherits from TWPTextStyle, this can be used directly. So the parent cells of the field names are also used as template for the cells.
for I := 0 to fields_master.Count-1 do
template.AddColumn( fields_master.SecondNamePart(i), fields_master[i].par.FirstSibling );
if DataModule1.DDETAIL.TableName<>'' then
begin
template.ClientName := 'DETAIL';
template := WPTableProducerDB1.Blocks.Add('DETAIL') as TWPBlockTemplateDB;
template.DataSourceName :=
WPTableProducerDB1.DataSourceLinks[1].Name;
for I := 0 to fields_detail.Count-1 do
template.AddColumn( fields_detail.SecondNamePart(i), fields_detail[i].par.FirstSibling );
end;
finally
editors.Free;
fields_detail.Free;
fields_master.Free;
end;
DataModule1.DMASTER.Active := true;
if DataModule1.DDETAIL.TableName<>'' then
DataModule1.DDETAIL.Active := true;
// Create the table
WPRichText2.Clear;
WPRichText2.Header.SetPageWH(-1,-1,300,300,300,300);
WPTableProducerDB1.Active := true;
end;