|
| 1 | +## Master-Detail example using FireDAC datasets |
| 2 | + |
| 3 | +This demo shows how to create 3 levels of nested sub-grid rows. |
| 4 | + |
| 5 | + |
| 6 | + |
| 7 | +Adding a sub-grid to another sub-grid is done in the following steps: |
| 8 | + |
| 9 | +- Create a new "Expander" render |
| 10 | + |
| 11 | + This is the class that draws the "+" and "-" symbols you can click to expand or collapse a row. |
| 12 | + |
| 13 | + For the main grid: |
| 14 | + |
| 15 | + ```delphi |
| 16 | + uses Tee.Renders; |
| 17 | + var Expander : TExpanderRender; |
| 18 | + Expander:=TeeGrid1.Grid.Current.NewExpander; |
| 19 | + ``` |
| 20 | + |
| 21 | + For any sub-grid, you should do the same as above but using the "NewGroup" variable that is passed when a sub-grid is expanded: |
| 22 | + |
| 23 | + ```delphi |
| 24 | + procedure TMasterDetail.DetailNewGroup(const Sender,NewGroup:TRowGroup); |
| 25 | + begin |
| 26 | + Expander:=NewGroup.NewExpander; |
| 27 | + ... |
| 28 | + ``` |
| 29 | + |
| 30 | + |
| 31 | +- The Expander should be connected to an event that will give the detail data rows we want for a given master row: |
| 32 | + |
| 33 | + ```delphi |
| 34 | + procedure TMasterDetail.GetOrders(const Sender: TExpanderRender; const ARow:Integer; out AData:TObject); |
| 35 | + begin |
| 36 | + // Return a new Data using a clone of Orders rows for a given Customer |
| 37 | + AData:=TVirtualDBData.From(SampleData.OrdersOfCustomer(ARow+1)); |
| 38 | +
|
| 39 | + // Data should be destroyed automatically |
| 40 | + TVirtualDBData(AData).OwnsData:=True; |
| 41 | + end; |
| 42 | +
|
| 43 | + Expander.OnGetData:=GetOrders; |
| 44 | + ``` |
| 45 | + |
| 46 | +- In this example, the "OrdersOfCustomer" function is done manually using FireDAC TFDQuery components: |
| 47 | + |
| 48 | + ```delphi |
| 49 | + function TSampleData.OrdersOfCustomer(const ARecNo:Integer):TDataSet; |
| 50 | +
|
| 51 | + // Return the CustomerID for row: ARow |
| 52 | + function CustomerID:String; |
| 53 | + begin |
| 54 | + CustomersTable.RecNo:=ARecNo; |
| 55 | + result:=CustomersTable.FieldByName('CustomerID').AsString; |
| 56 | + end; |
| 57 | +
|
| 58 | + // Execute OrdersTable query for a given CustomerID |
| 59 | + procedure FilterOrders(const ACustomerID:String); |
| 60 | + begin |
| 61 | + OrdersTable.Close; |
| 62 | + OrdersTable.ParamByName('Cust').AsString:=ACustomerID; |
| 63 | + OrdersTable.Open; |
| 64 | + end; |
| 65 | +
|
| 66 | + begin |
| 67 | + FilterOrders(CustomerID); |
| 68 | + result:=CloneData(OrdersTable); |
| 69 | + end; |
| 70 | + ``` |
| 71 | + |
| 72 | + You can obtain the sub-details rows the way you like. |
| 73 | + |
| 74 | + If you use [TeeBI Datamining Library](https://github.com/Steema/TeeBI) these steps are much more simplified because TeeBI knows the relationship master-detail between datasets and prepares data automatically. |
| 75 | + There is a TeeBI and TeeGrid example showing these levels of sub-grid details: |
| 76 | + |
| 77 | + [TeeBI and TeeGrid Example](https://github.com/Steema/TeeGrid-VCL-FMX-Samples/tree/master/demos/VCL/TeeBI) |
| 78 | + |
| 79 | + |
0 commit comments