1C:Enterprise 8.3. Practical Developer’s Guide. Lesson 27 (2:10). Form development techniques. Opening a list form with the specified filter

Opening a list form with the specified filter

Finally, let us review an example of opening a list form with a filter that is specified in advance. This example shows the difference between the fixed (1C:Enterprise script-based) user settings and dynamic list settings specified in Designer.

Suppose that you need to open the list of Service documents from the list of employees, filtered by technician selected in that list.

You have solved a similar task earlier using a filter criterion and the generation feature (see section Getting generated objects). And now let us solve it using 1C:Enterprise script.

In Designer mode

So you need to open the Services document list form with a filter by technician (the Technician field value is a reference to the employee selected in the list of employees).

  1. Create a list form of the Employees catalog.
  2. In this form, create the Services command and drag it to the form command bar.
  3. In the command property palette, in the Action field, create a client command handler (fig. 27.40).

    Lesson 27 (2:10). Form development techniques / Opening a list form with the specified filter / In Designer mode
    Fig. 27.40. List form of the Employees catalog
  4. In the form module, fill the command handler as shown in listing 27.7.

    Listing 27.7. Services command handler

    &AtClient
    Procedure Services(Command)
     
        FormParameters = New Structure("Filter", New Structure("Technician",
            Items.List.CurrentRow));
        OpenForm("Document.Services.ListForm", FormParameters,, True);
     
    EndProcedure
    This script is similar to the previous listing 27.6 where a report form is opened with a filter by parameterized command value. But here a reference to the current item of the Employees catalog is passed to the Technician field filter. This reference is stored in the CurrentRow property of the List form table, which displays dynamic list data. The filter is passed to the form parameters when the Services document list form is opened.

In 1C:Enterprise mode

Let us test the new form.

  1. Start 1C:Enterprise in the debug mode.
  2. Open the list of employees.

    You can see that the Services button is now available.
  3. Click the Services button.

    This opens the list of Services documents with technician equal to the selected employee (fig. 27.41).

    Lesson 27 (2:10). Form development techniques / Opening a list form with the specified filter / In 1C:Enterprise mode
    Fig. 27.41. Filter by technician in the Services document list.

However, it is not that simple. To create filters and other dynamic list settings, you have to know the settings types and their priorities.

As we have mentioned earlier, dynamic lists are based on the data composition system. When a form is opened, the list settings are stored to the SettingsComposer property of the dynamic list. 

Three types of dynamic list settings are available:

  • Fixed settings, which are specified using 1C:Enterprise script and stored to the SettingsComposer.FixedSettings dynamic list property.
  • Settings specified in Designer, which are stored to the SettingsComposer.Settings dynamic list property.
  • User settings, which are specified in 1C:Enterprise mode and stored to the SettingsComposer.UserSettings dynamic list property.

The user settings are loaded from the system storage and added to the settings specified in Designer. If a settings conflict occurs, the user settings override the settings specified in Designer.

Then the fixed settings are added to the resulting settings. If a settings conflict occurs, an error is displayed. For example, when a list form is opened with a filter (in this case it is a filter by Technician field), the filter is stored to the fixed settings. Therefore, users cannot set a list filter by this field.

So, if you want to give users the option to filter the list by field included in the fixed list settings, you have to remove the filter from the collection of fixed settings (SettingsComposer.FixedSettings.Filter) and then add it to the main list settings (SettingsComposer.Settings.Filter).

In Designer mode

Let us implement opening the list with a filter.

  1. Open the list form of the Services document.
  2. In the form editor, add the following form attributes:
    • ScriptFilter. Type: Boolean.
    • FilterField. Type: Arbitrary.
    • FilterValue. Type: Arbitrary.
    These attributes will store the script filter data in the list form.
  3. Create the OnCreateAtServer event handler and fill it as shown in listing 27.8.

    Listing 27.8. OnCreateAtServer event handler

    &AtServer
    Procedure OnCreateAtServer(Cancel, StandardProcessing)
            
        If Parameters.Filter.Property("Technician") Then
            ScriptFilter = True;
            FilterField = New DataCompositionField("Technician");
            Parameters.Filter.Property("Technician",FilterValue);
        EndIf
     
    EndProcedure
    During the creation of the list form the procedure uses a collection of form parameters to check whether a filter by Technician field is set. If the filter is set, the procedure sets the flag showing that the form is opened using a script filter, sets FilterField to Technician data composition field, and assigns the appropriate filter value from the collection of form parameters to FilterValue.
  4. Create the OnOpen form event handler and fill it as shown in listing 27.9.

    Listing 27.9. OnOpen event handler

    &AtClient
    Procedure OnOpen(Cancel)
     
        If ScriptFilter = True Then
            ScriptSettings = List.SettingsComposer.FixedSettings;
            For Each SettingsItem In ScriptSettings.Filter.Items Do         
                If SettingsItem.LeftValue = FilterField Then             
                    ScriptSettings.Filter.Items.Delete(SettingsItem);
                EndIf;
            EndDo;
     
            Settings = List.SettingsComposer.Settings;
     
            FilterItem = Settings.Filter.Items.Add(Type("DataCompositionFilterItem"));
            FilterItem.LeftValue = FilterField;
            FilterItem.ComparisonType = DataCompositionComparisonType.Equal;
            FilterItem.RightValue = FilterValue;
     
            List.SettingsComposer.LoadSettings(Settings);
        EndIf;
     
    EndProcedure
    The handler script is executed if the form is opened with the script filter.

    First the procedure iterates through the collection of filter items of the fixed settings of dynamic list settings composer. If the collection includes a filter item where LeftValue is equal to the FilterField attribute value (a script filter by Technician field), this item is deleted.

    Then this filter is added to the dynamic list settings collection (SettingsComposer.Settings.Filter) and the modified settings are loaded to the settings composer.

    The only thing left is to delete the created filter item when the Services document list form is closed.
  5. Create the OnClose form event handler and fill it as shown in listing 27.10.

    Listing 27.10. OnClose event handler

    &AtClient
    Procedure OnClose(Cancel, StandardProcessing)
     
        If ScriptFilter = True Then
            Settings = List.SettingsComposer.Settings;
            For Each SettingsItem In Settings.Filter.Items Do
                If SettingsItem.LeftValue = FilterField Then
                    Settings.Filter.Items.Delete(SettingsItem);
                EndIf;
            EndDo;
            List.SettingsComposer.LoadSettings(Settings);
        EndIf;
     
    EndProcedure

    The handler script is executed if the form is opened with the script filter.

    The procedure iterates through the collection of filter items of the settings of dynamic list settings composer (SettingsComposer.Settings.Filter). If the collection includes a filter item where LeftValue is equal to the FilterField attribute value, this item is deleted. Then the modified settings are loaded to the settings composer.

In 1C:Enterprise mode

Let us test the changes.

  1. Start 1C:Enterprise in the debug mode.
  2. Open the list of employees and click the Services button.

    This opens the list of Services documents with technician equal to the selected employee.

    Let us view the list settings.
  3. On the More menu, click Configure list.

    The settings window includes a filter by Technician field, and you can edit or clear this filter (fig. 27.42).

    Lesson 27 (2:10). Form development techniques / Opening a list form with the specified filter / In 1C:Enterprise mode
    Fig. 27.42. List settings window with a filter specified

    Once you close the form, the filter is deleted.

    So you learned how to open a list form with a specified filter and how to give users the option to edit this filter.

Leave a Reply

Your email address will not be published. Required fields are marked *

1C:Enterprise Developer's Community