Let us add a Common module configuration object.
- In the configuration object tree, expand the Common branch by clicking the icon to the left of the branch name.
- Right-click the Common modules branch and click the Add button in the command bar of the configuration window (fig. 4.24).
Fig. 4.24. Creating a common module in the configuration object tree
This opens the window where you can enter the module text and the module property palette. - In the property palette, name the module DocumentProcessing, select the Client (managed application) check box, and clear the Server check box.
It means that instances of this module will be compiled in the thin client context and in the web client context (fig. 4.25).
Fig. 4.25. Common module propertiesAdd the following script to the module (listing 4.2). - Listing 4.2. Procedure CalculateTotal()
Procedure CalculateTotal (TabularSectionRow) Export TabularSectionRow.Total = TabularSectionRow.Quantity * TabularSectionRow.Price; EndProcedure
Now let us examine this script. The TabularSectionRow variable that you defined in the OnChange event handler of the Quantity field is passed to the CalculateTotal() procedure. This variable stores data of the tabular section row of the Goods receipt document that is being edited.
You can use this variable to access tabular section column data and calculate the total by multiplying the price and quantity.
The Export keyword in the procedure title indicates that the procedure can be accessed from other applied solution modules.Change the handler script in the form module (listing 4.3). - Listing 4.3. Procedure MaterialsQuantityOnChange()
&AtClient Procedure MaterialsQuantityOnChange(Item) TabularSectionRow = Items.Materials.CurrentData; DocumentProcessing.CalculateTotal(TabularSectionRow); EndProcedure
You can see that the first line of the procedure remains unchanged. And in the second line instead of directly calculating the total the CalculateTotal() procedure from the DocumentProcessing common module is called. The current tabular section row is passed to the procedure as a parameter.Start 1C:Enterprise in the debug mode and ensure that nothing has changed from the user perspective. - Finally, let us implement a similar handler for the Price field. Since you have already coded the required procedure in the form module, you might want to reuse it with a different control form. However, 1C Company configuration development standards do not permit that approach.
Learn more! According to the 1C Company development standards, each event must have its own handler. If identical actions are to be performed in response to changes to different controls (for example, in response to clicking any of the several buttons), these should be handled as follows:- A dedicated procedure or function that performs the required actions is created.
- For each control, a separate handler with a default name is created.
- The procedure or function is called from each handler.
- Add the OnChange event with the following script to the Price field (listing 4.4).
Listing 4.4. Procedure MaterialsPriceOnChange()
&AtClient Procedure MaterialsPriceOnChange(Item) TabularSectionRow = Items.Materials.CurrentData; DocumentProcessing.CalculateTotal(TabularSectionRow); EndProcedure
Next page: In 1C:Enterpise mode