SAP ABAP Advanced Cookbook
上QQ阅读APP看书,第一时间看更新

Enable Adding Layout toolbar buttons

Up to this point, the output of the ALV does not allow the user to save changes made to layout. In this recipe, we will add coding that will allow saving user-specific layouts, load layouts, as well as specify a default layout.

How to do it...

For enabling layout saving, follow these steps:

  1. We declare two variables and also, an object reference to the class cl_salv_layout. In addition, a key is defined based on the type salv_s_layout_key.
  2. The get_layout method is then used to get the layout object for the ALV.
  3. The set_key method is called for the layout object mylayout. The mykey structure having the report name sy-repid is passed to this method.
  4. Next, the set_save_restriction method is called. It is passed the static attribute RESTRICT_USER_DEPENDANT of the interface if_salv_c_layout.
  5. Finally, the set_default layout method is called with the value 'X', as shown in the next screenshot.
    How to do it...
  6. The code is added before the ALV display method call shown in the first screenshot of this chapter.

How it works...

In this recipe, we declared a layout variable based on the cl_salv_layout class. An essential step is to set the key of the layout object and passing the name of the program. This is done using the set_key method.

Next, for enabling the Save layout button, the set_save_restriction method is used. Based on the value passed on to the method, the system determines whether the user is allowed to save layout as user-specific, user-unspecific, or without any restrictions. Three possible constant values may be passed.

How it works...

Since our requirement was to enable user to store layout as User-Specific, we used RESTRICT_USER_DEPENDANT constant attribute of the interface if_salv_c_layout.

Finally, we wanted the Default setting checkbox to be enabled so that the user may save a particular layout as his or her default. For this reason, the set_default method was called with the value 'X'.

How it works...

The next time the user executes the report, his or her default layout is loaded and data displayed in that layout format.