非常不错ALV GRID CONTROL 教材

An
Easy Reference
for
ALV GRID CONTROL


 
Table of Contents
 
Purpose..............................................................................1
Prerequisites.....................................................................1
A. Introduction.................................................................1
B. Building Blocks............................................................1
B.1. General Scheme.....................................................2
B.2. Building Field Catalog.............................................7
B.2.1. Structure of a Field Catalog.............................7
B.2.2. Building Field Catalog Manually.......................9
B.2.3. Building Field Catalog Semi-Automatically.....10
B.3. Layout Adjustments..................................................12
B.4. Printing Adjustments................................................14
B.5. Excluding Unwanted Standard Function Buttons.....15
C. Non-Event Based Additional Functionalities.............16
C.1. Changing Field Catalog or Layout after First Display..16
C.2. Setting Sort Conditions..............................................16
C.3. Filtering...........................................................................17
C.4. Making Selections............................................................18
C.5. Retrieving and Setting Scroll Status Info..........................19
C.6. Coloring............................................................................19
C.6.1. Coloring an Entire Column.......................................20
C.6.2. Coloring an Entire Row............................................20
C.6.3. Coloring Individual Cells...........................................21
C.7. Inserting Hyperlinks.........................................................22
C.8. Making Fields as Dropdown Menus.............................23
C.9. Managing variants............................................................24
D. Event Based Additional Functionalities...........................25
D.1. General Scheme for the Event Handler Class...............26
D.2. Hotspot Clicking.............................................................28
D.3. Double Clicking....................................................................29
D.4. Pushbuttons On The List...........................................29
D.5. Adding Your Own Functions...................................30
D.6. Overriding Standard Functions............................32
D.7. Context Menus.........................................................32
D.8. About printing..............................................................33
D.9. Making ALV Grid Editable......................................34
D.10. Controlling Data Changes....................................35
D.11. Linking F1 Help to Fields.......................................36
D.12. Linking F4 Help to Fields..........................................37
E. A Piece of Troubleshooting......................................38
TRADEMARKS.................................................


 
Purpose
The purpose of this tutorial is to provide an easy and quick reference that may be used as a guide while coding to build lists using ALV Grid Control. Actually, there is easy-to-reach information about implementing ALV Grid lists. However, it is generally required to find the information sought in a quicker way. This tutorial handles this, being a condensed source that can be used as a “guide.” It will not deal with the technical infrastructure on which ALV lays. Some of the tables are taken from the online SAP Library, which is the most trustable source about the topic. Shortly, this will be a booklet summarizing major capabilities of the ALV Grid Control.
To get deep into the ALV Grid control, you can refer to the standard SAP course “BC412 – ABAP Dialog Programming Using EnjoySAP Controls” and the book “Controls Technology”. Also you had better inspect demo programs.
Prerequisites
To use ALV Grid Control in a simple manner, it will be sufficient just having experience on dialog programming. However, to make use of more capabilities, it is required some knowledge on object-oriented perspective of ABAP programming. A general knowledge on control framework is supposed to exist.
         A. Introduction
 
Here is the definition for ALV from SAP Help:
“The ALV Grid control is a flexible tool for displaying lists. The tool provides common list operations as generic functions and can be enhanced by self-defined options.”
The ALV Grid control is used to build non-hierarchical, interactive, and modern-design lists. As a control, it is a component that is installed on the local PC.
The ALV Grid control provides typical list functions as sorting, filtering, summing, etc.,while also gives the opportunity to develop user functions where needed. It presents numerous interfaces like Excel Inplace and Crystal Reports.
The wrapper class implemented to encapsulate ALV Grid functionality is “CL_GUI_ALV_GRID”. There is another way to display lists with ALV utilizing “REUSE_ALV...” functions. However, that way is not comprised in this tutorial.
         B. Building Blocks
 
While preparing a list to be displayed via an ALV grid control, we have some basic components to prepare. These are;
 i. List data: Obviously, this is the data in an internal table to be listed. Standard ALV functions except sorting makes just read access to the list data. However, sorting changes state of the internal table. The internal table holding list data may be of any flat type. Deep types are only allowed when set for some functionalities of ALV Grid.
 ii. Field Catalog: We use another internal table to define specifications on how the fields of our list will be displayed. This internal table is called the


 
 “field catalog”. The field catalog must comprise some technical and additional information about display options for each column to be displayed. There are three procedures to generate the field catalog as “Automatic generation”, “Semi-automatic generation”, and “Manual generation”. The internal table for the field catalog must be referenced to the dictionary type “LVC_T_FCAT”.
 iii. Layout Structure: We fill a structure to specify general layout options for the grid. With this structure we can set general display options, grid customizing, totals options, color adjustments etc... The layout structure must be of type “LVC_S_LAYO”.
 iv. Event Handler: We should define and implement an event handler class if we want to handle events triggered by the ALV Grid instance. After creating ALV Grid instance, we must register an instance of this event handler class to handle ALV Grid events.
 v. Additional Data: To trigger some additional features of ALV Grid we can have some additional data to pass as parameters. For example, initial sorting criteria, buttons to be deactivated, etc...
 
B.1. General Scheme
Now, we can figure out a primitive scheme to prepare our ALV Grid. As a control object, ALV Grid instance requires a container to be linked to the screen. Generally, an instance of the class “cl_gui_custom_container” is used for this purpose. Instances of some other container classes such as “cl_gui_docking_container”, “cl_gui_dialogbox_container” may also be used. In our example we take a custom container. To create a custom container instance, we need a custom control area on the screen.
Step 1? Add a custom control on the screen which will be related to the custom container. Let’s give it the name ‘CC_ALV’.
Step 2 ? Declare global variables to be used for ALV Grid.
Code Part 1 – Global data definitions for ALV
Step 3 ? Declare your internal table which is supposed to hold the list data. Let’s name it “gt_list”. Here is an example declaration.
 
*-- Global data definitions for ALV
*--- ALV Grid instance reference
DATA gr_alvgrid TYPE REF TO cl_gui_alv_grid .
*--- Name of the custom control added on the screen
DATA gc_custom_control_name TYPE scrfname VALUE ‘CC_ALV’ .
*--- Custom container instance reference
DATA gr_ccontainer TYPE REF TO cl_gui_custom_container .
*--- Field catalog table
DATA gt_fieldcat TYPE lvc_t_fcat .
*--- Layout structure
DATA gs_layout TYPE lvc_s_layo .


 
Code Part 2 – Declaration of the internal table that will hold the list data
Step 4 ? Somewhere in your program before calling list display, fill your list data as you want. Here, it is not our concern what the data are. We assume the internal table is filled reasonably. We will use the data of table SFLIGHT as our list data.
Step 5 ? Call the screen which comprises the ALV Grid control. At PBO of this screen we will deal with creating the ALV Grid instance.
Code Part 3 – PBO of the flow logic for the screen containing ALV Grid control
Code Part 4 – Inside the module
Step 6 ? Now, it is high time we wrote something to play. So, this piece will be the one we will deal mainly. What we do is, checking whether an instance of the container (or ALV Grid) exists. If it exists, refreshing it, and if not, creating and setting ALV for the first display.
 
*--- Internal table holding list data
DATA BEGIN OF gt_list OCCURS 0 .
INCLUDE STRUCTURE SFLIGHT .
*--In further sections, some additional fields will added here
*--for some functionality
DATA END OF gt_list .
 
*--PBO
PROCESS BEFORE OUTPUT .
...
MODULE display_alv .
...
 
...
MODULE display_alv OUTPUT .
PERFORM. display_alv .
ENDMODULE .
 
FORM. display_alv .
IF gr_alvgrid IS INITIAL .
*----Creating custom container instance
CREATE OBJECT gr_ccontainer
EXPORTING
container_name = gc_custom_control_name
EXCEPTIONS
cntl_error = 1
cntl_system_error = 2
create_error = 3
lifetime_error = 4
lifetime_dynpro_dynpro_link = 5
others = 6 .
IF sy-subrc <> 0.
*--Exception handling
ENDIF.


 
Code Part 5 – Form. checking instance existence, creating instance, setting for first display and refreshing
 
*----Creating ALV Grid instance
CREATE OBJECT gr_alvgrid
EXPORTING
i_parent = gr_ccontainer
EXCEPTIONS
error_cntl_create = 1
error_cntl_init = 2
error_cntl_link = 3
error_dp_create = 4
others = 5 .
IF sy-subrc <> 0.
*--Exception handling
ENDIF.
*----Preparing field catalog.
PERFORM. prepare_field_catalog CHANGING gt_fieldcat .
*----Preparing layout structure
PERFORM. prepare_layout CHANGING gs_layout .
*----Here will be additional preparations
*--e.g. initial sorting criteria, initial filtering criteria, excluding
*--functions
CALL METHOD gr_alvgrid->set_table_for_first_display
EXPORTING
* I_BUFFER_ACTIVE =
* I_CONSISTENCY_CHECK =
* I_STRUCTURE_NAME =
* IS_VARIANT =
* I_SAVE =
* I_DEFAULT = 'X'
is_layout = gs_layout
* IS_PRINT =
* IT_SPECIAL_GROUPS =
* IT_TOOLBAR_EXCLUDING =
* IT_HYPERLINK =
CHANGING
it_outtab = gt_list[]
it_fieldcatalog = gt_fieldcat
* IT_SORT =
* IT_FILTER =
EXCEPTIONS
invalid_parameter_combination = 1
program_error = 2
too_many_lines = 3
OTHERS = 4 .
IF sy-subrc <> 0.
*--Exception handling
ENDIF.
ELSE .
CALL METHOD gr_alvgrid->refresh_table_display
* EXPORTING
* IS_STABLE =
* I_SOFT_REFRESH =
EXCEPTIONS
finished = 1
OTHERS = 2 .
IF sy-subrc <> 0.
*--Exception handling
ENDIF.
ENDIF .
ENDFORM. .


 
From ABAP objects, we are familiar with “CREATE OBJECT” statement which instantiate classes. In this snippet of code, we used two instance methods of “cl_gui_alv_grid”. First is “set_table_for_first_display” whose name implies for what it is used. After creating the ALV Grid instance we call this method to make our list displayed. We pass list data table, field catalog table, layout structure and additional information. Here are parameter definitions taken from SAP Library.
Parameter
Meaning
I_BUFFER_ACTIVE
Flag to be set by the application if the method call is static. This means the method is always called with the same field catalog. In this case, the field catalog can be held in a special buffer. This accelerates the display of small lists, in particular.
I_STRUCTURE_NAME
Name of the DDIC structure (for example, 'SFLIGHT') for the data in the output table. If you specify this parameter, the field catalog is generated automatically.
IS_VARIANT
Determines the layout to be used for displaying the output table. If you use this parameter, you must at least fill field REPORT of the structure of type DISVARIANT .
I_SAVE
Determines the options available to the user for saving a layout:
              • 'X': global saving only
              • 'U': user-specific saving only
              • 'A': corresponds to 'X' and 'U'
              • SPACE: no saving
 
I_DEFAULT
This parameter determines if the user is allowed to define default layouts:
              • 'X': Default layouts allowed (default setting)
              • SPACE: Default layouts not allowed
 
If default layouts are allowed and if such a layout exists and no other layout is specified in IS_VARIANT , the default layout is automatically loaded when this method is called.
IS_LAYOUT
Determines properties of the grid control. The layout structure has nothing to do with the layout for saving filter, sort, and column properties.
IS_PRINT
Parameter for printing on the backend
IT_SPECIAL_GROUPS
If in the field catalog the columns were grouped together with field SP_GROUP , you must pass a table with texts for these groups. On the current layout window, it is then possible to use a list box to restrict column selection to one of these groups.
IT_TOOLBAR_EXCLUDING
This table contains function codes of the toolbar that you want to hide for the lifetime of the ALV Grid Control. The function codes are constant attributes and are prefixed with MC_FC_ .
IT_HYPERLINK
This table assigns a hyperlink address (field HREF of LVCSHYPE )toeachhandle(fieldHANDLEof


 
LVC_S_HYPE ). Using this handle, you can then include hyperlinks in the grid.
IT_ALV_GRAPHICS
Settings for displaying the ALV list as a diagram (for example, axis labels). The row type of the table has two fields (variables/value pairs):
              PROP_ID: Assign a constant attribute of the class CL_ALV_GRAPHICS_CU with prefix CO_PROPID_ to this field to determine the changes to be made to the graphic. Use the CL_ALV_GRAPHICS_CU=>CO_PROPID_TITLE attribute, for example, to refer to the title of the diagram.
              PROP_VAL: The value of the relevant topic, for example, 'My Title'.
 
IT_OUTTAB
Output table with the data to be displayed
IT_FIELDCATALOG
Determines the structure of the output table and the format of the data to be displayed
IT_SORT
Table with sort properties for columns that are to be sorted initially
IT_FILTER
Table with filter properties for columns for which a filter is to be set initially
 
The second method we used in our code snippet was “refresh_table_display” which, as implied from the name again, is used to refresh the ALV display. We do not want our ALV Grid to be created every time the PBO triggers. The first pass should create it and others should just refresh. However, we may require some changes about the appearance, layout, field catalog etc…. In that case, we will try to use other ALV methods to make changes. The parameter definition table of this method is as follows.
Parameter
Meaning
IS_STABLE
If the row or column field of this structure is set, the position of the scroll bar for the rows or columns remains stable.
I_SOFT_REFRESH
This parameter is used only in exceptional cases. If you set this parameter, any totals created, any sort order defined and any filters set for the data displayed remain unchanged when the grid control is refreshed. This makes sense, for example, if you have not modified the data of the data table and want to refresh the grid control only with regard to layout or field catalog changes.
 
OK! Seeing a simple but whole picture we are now ready to advance to build our basic components which we just point as form. calls in the scheme.


 
B.2. Building Field Catalog
As mentioned earlier, there are three procedures to build a field catalog. The simplest way applies if our list structure is similar to a dictionary table. To do this, we simply eliminate the form. call and pass the name of dictionary structure (in our example, ‘SFLIGHT’) to the parameter ‘I_STRUCTURE_NAME’. Before explaining other procedures, let’s see what a field catalog has in its structure.
B.2.1. Structure of a Field Catalog
The row structure of a field catalog is of dictionary type ‘LVC_S_FCAT’. There are many fields providing adjustment of display options for our list columns. Here are the basic ones.
FIELDNAME
You use this field to assign a field name of your output table to a row of the field catalog. All settings that you make in this row refer to the corresponding column of the output table.
REF_FIELD
You must fill this field if:
              the output table field described by the current entry in the field catalog has a corresponding field in the Data Dictionary and
              the field name in the output table is not identical to the field name of the field in the Data Dictionary.
 
If the field names are identical, it is sufficient to specify the DDIC structure or table in field REF_TABLE of the field catalog.
REF_TABLE
You must fill this field only if the output table field described by the current entry in the field catalog has a corresponding entry in the Data Dictionary. Using this assignment, the ALV Grid Control can copy the text for the column header from the Dictionary, for example.
CHECKBOX
Outputting a checkbox. The checkbox cannot be modified by the user.
COL_POS
Relevant only if the relative column positions should not be identical to the sequence of fields in the field catalog when the list is displayed for the first time. The parameter determines the relative column position of the field for list output. The user can interactively modify the order of the columns. If this parameter is initial for each field catalog entry, the order of the columns corresponds to the sequence of fields in the field catalog.
DO_SUM
If this field is set, the ALV uses this field to calculate the total (this corresponds to the generic totals function in the toolbar.)

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/166523/viewspace-200353/,如需转载,请注明出处,否则将追究法律责任。

user_pic_default.png
请登录后发表评论 登录
全部评论
<%=items[i].createtime%>

<%=items[i].content%>

<%if(items[i].items.items.length) { %>
<%for(var j=0;j
<%=items[i].items.items[j].createtime%> 回复

<%=items[i].items.items[j].username%>   回复   <%=items[i].items.items[j].tousername%><%=items[i].items.items[j].content%>

<%}%> <%if(items[i].items.total > 5) { %>
还有<%=items[i].items.total-5%>条评论 ) data-count=1 data-flag=true>点击查看
<%}%>
<%}%> <%}%>

转载于:http://blog.itpub.net/166523/viewspace-200353/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值