Part 1
Part 2
Part # 3
Part # 4
In the previous part of the cycle, we became more familiar with the principles of operation of the mechanism for linking LiveBinding objects based on expressions. Thanks to the capabilities of the Visual LiveBinding subsystem, the developer is free from the need to program these expressions manually. Today we will continue to develop our application and get acquainted with new techniques for working with databases in FireMonkey using FireDAC and LiveBinding.
In the Data module (DataModule) we will create another data set using the
TFDTable component. Associate it with the
tblIngredientes table. Let's call it FDTIngredientes. By analogy with the previous data sets, create a list of fields. We make changes to the ConnectToDB procedure.
')
function TDM.ConnectToDB: Boolean; begin try FDConnection1.Connected := True;
According to the logic of the application, we need to make it so that when you move through the list of recipes on the main form the list of ingredients is updated. For each recipe, your list of ingredients should be displayed on the "Ingredients" tab. It is easiest to implement such functionality using the
Master-Details mechanism at the component link level.
First of all, you need to set the
MasterSource property for the subordinate data set (FDTIngredientes). For communication, we will have to use the TDataSource component. “Tie” it to the FDTRecipe component, which is responsible for outputting data from the tblRecipe table, and call it dsRecipe. Set the
MasterSource property of the
FDTIngredientes component (select from the drop-down list) the value of dsRecipe. Using the property editor, set the value of the
MasterField property - Id. And finally, choose from the list of indexes the value of the
IndexName property - idxRecipe. The data index indexes the tblIngredientes table across the IdCB field, which is responsible for linking to the recipe table.
In fact, at the data access component level, everything works the same as in the VCL.
On the main form of the application, go to the “Ingredients” tab and call
LiveBindings Designer .
Place the
TGrid and
TBindNavigator tabs , and “tie” these components to FDTIngredientes, in the way we did in the last part by “dragging” or “drawing links” on the visual diagram. Now, if you run the application, then the logic of displaying the ingredients at this stage will probably seem unconvincing to you. Indeed, the grid displays not the names of products and units of measurement, but their codes (Id).
Now we need to correctly display the values ​​of product names and units of measure in the recipe. For this we use the so-called. “
Lookup ” - fields (“viewing” fields). Let's call the list of fields of the FDTIngredientes component and create a new field (in the context menu, select the New Field item ...). Fill in the data in the field editor as shown in the figure:
In the Product field, the name of the product will be displayed, from the Foodstuff table, whose Id will correspond to the value of the IdFs field in the FDTIngredientes table. Note the value of the
Field type .
Similarly, create a
Lookup to display data from the Units table.
Now, we need to, firstly, correctly display the Lookup field values ​​in the “grid” component (grid), as well as create a data entry mechanism.
Let's return to the main form and place on it two components TComboBox (let's call them cbProd and cbUnit) and one TEdit, as shown in the figure.
In
LiveBindings Designer, we associate the properties of these components with data sources, as shown in the figure:
The rules for connecting the
TComboBox component to the data source are quite simple.
Item.Text is associated with a field whose values ​​appear in the drop-down list.
Item.LookupData is associated with a field whose value is inserted into an editable data set. Appropriately, the SelectedValue is associated with the editable field of the data set being edited.
Now configure the grid. Double-click on the BindingsList1 component (the name of the component was assigned automatically, you can change it) open the link editor and select the
TLinkGridToDataSourceBindSourceDB component. In Object Inspector, call the Columns property editor. Let's create all the fields (the Add All Fields button) and configure for each of them the displayed title and visibility (the Visible property) by analogy with the way we did in the previous part.
Run the application. When you move through the list of recipes, on the "Ingredients" tab, the list of products is updated, which indicates that the
Master-Details connection is working correctly. The value of the product name and unit of measurement in the recipe we can specify using the drop-down list (ComboBox). Selected values ​​are correctly displayed in the grid.
In the next part, we will transfer the newly implemented functionality to the mobile platform.
Until Friday!