My table has around 300 columns and I want to display only 10 out of them with specifying which ones. I am working with CL_SALV_TABLE.
Can anybody help me with this problem or give me a hint? Thank you very much in advance!
You need to use the method SET_VISIBLE of the Column object (class CL_SALV_COLUMN). For more information, see the chapter "Set the Visibility of the Column" of page "Columns (General)".
This Minimal reproducible example shows only the columns SPRAS and LAISO from the table T002, all other ones are hidden:
DATA: t002_lines TYPE TABLE OF t002,
salv TYPE REF TO cl_salv_table,
columns TYPE salv_t_column_ref.
FIELD-SYMBOLS <column> TYPE salv_s_column_ref.
SELECT * FROM t002 INTO TABLE t002_lines.
CALL METHOD cl_salv_table=>factory
IMPORTING
r_salv_table = salv
CHANGING
t_table = t002_lines.
LOOP AT salv->get_columns( )->get( ) ASSIGNING <column>.
CASE <column>-columnname.
WHEN 'SPRAS' OR 'LAISO'.
<column>-r_column->set_visible( if_salv_c_bool_sap=>true ).
WHEN OTHERS.
<column>-r_column->set_visible( if_salv_c_bool_sap=>false ).
ENDCASE.
ENDLOOP.
salv->display( ).
Related
I'm trying to change the date format from 2020.11.20 to 11/2020
My objective is to remove the day and leave just month/year.
If I change the type of the field EDATU from vbep-EDATU to string it doesn't work.
Any tips on how to achieve my goal?
DATA: GR_COLUMNS TYPE REF TO CL_SALV_COLUMNS_TABLE,
GR_TABLE TYPE REF TO CL_SALV_TABLE.
TYPES: BEGIN OF IT_STR,
EDATU TYPE VBEP-EDATU,
vbeln type vbep-vbeln,
END OF IT_STR.
DATA: IT_FINAL TYPE STANDARD TABLE OF IT_STR.
FIELD-SYMBOLS: <F_DAT> TYPE IT_STR.
SELECT EDATU vbeln FROM VBEP INTO TABLE IT_FINAL up to 10 rows.
LOOP AT IT_FINAL ASSIGNING <F_DAT>.
<F_DAT>-EDATU = <F_DAT>-EDATU+4(2) && '/' && <F_DAT>-EDATU(4).
ENDLOOP.
TRY.
CALL METHOD CL_SALV_TABLE=>FACTORY
EXPORTING
LIST_DISPLAY = IF_SALV_C_BOOL_SAP=>FALSE
IMPORTING
R_SALV_TABLE = GR_TABLE
CHANGING
T_TABLE = IT_FINAL.
CATCH CX_SALV_MSG .
ENDTRY.
GR_COLUMNS = GR_TABLE->GET_COLUMNS( ).
CALL METHOD GR_TABLE->DISPLAY.
Even though a type D is really just a char based data type of length 8 it has some special behavior when outputting it. If you check in the debugger you will see the data in IT_FINAL is what you want, it's just that the ALV processes this data as a date regardless of the value in it.
So, for example date 20221019 got changed in IT_FINAL-EDATU to '10/2022'. Now, when you display it in the ALV it gets interpreted as a date and gets displayed depending on your user settings, assuming yours are yyyy.mm.dd the output would be: '10/2.02.2'.
You can get around this in two ways:
Add a new field to your table with a char-like type to hold the month value as suggested by Skin, and suppress the display of the EDATU field in the ALV
gr_columns->get_column( columnname = 'EDATU' )->set_technical( abap_true ).
Change the behavior of the EDATU column in the ALV by changing the edit mask with:
gr_columns->get_column( columnname = 'EDATU' )->set_edit_mask('_______').
Option 1 is clearly the better way to avoid unintended mishaps in the future.
I created a new colum type C and changed the loop to
LOOP AT IT_FINAL ASSIGNING <F_DAT>.
data(month) = <F_DAT>-EDATU+4(2).
data(year) = <F_DAT>-EDATU(4).
<f_dat>-char = month && '/' && year.
ENDLOOP.
Thanks for everyone who helped me.
I have a program which displays a simple table based on the class CL_SALV_TABLE, and I'd like to position the column CURRCODE at the fifth position, instead of 4 currently (default order defined in database table SCARR), as shown here:
How can I do that?
Here is the current source code (compiles with ABAP version 7.40) :
SELECT * FROM scarr INTO TABLE #DATA(scarr_s).
cl_salv_table=>factory(
IMPORTING
r_salv_table = DATA(salv)
CHANGING
t_table = scarr_s ).
salv->display( ).
NB: if you want to reproduce and the table SCARR is empty, execute the program SAPBC_DATA_GENERATOR to fill it.
You have to do it in two steps, before the call of the method display :
Call the method GET_COLUMNS of the ALV instance (of class CL_SALV_TABLE), to get the instance of the class CL_SALV_COLUMNS_TABLE corresponding to all columns.
The latter class has a method SET_COLUMN_POSITION to change the position of a given column whose name is passed as an argument.
Here is the source code:
SELECT * FROM scarr INTO TABLE #DATA(scarr_s).
cl_salv_table=>factory(
IMPORTING
r_salv_table = DATA(salv)
CHANGING
t_table = scarr_s ).
salv->get_columns( )->set_column_position( columnname = 'CURRCODE' position = 5 ). " <== ADD THIS LINE
salv->get_columns( )->set_optimize( ).
salv->display( ).
Result:
NB: I also use the method SET_OPTIMIZE so that the width of all columns is automatically adjusted to their contents (but it's not related to the question).
I have to read some data from a table and display it. The program starts but I don't know how to display any of the data I've selected. I want to put it out as a table.
I honestly don't even know if the following code is correct.
REPORT ZT_THIEMANN_TEST.
types : begin of ts_output,
object_id type CRMD_ORDERADM_H-object_id,
created_by type CRMD_ORDERADM_H-created_by,
end of ts_output,
tt_output type table of ts_output.
PARAMETERS Mel_Nr TYPE CRMD_ORDERADM_H-Object_ID obligatory.
data gt_output type tt_output.
START-OF-SELECTION.
SELECT cm~object_id cm~created_by
from CRMD_ORDERADM_H as cm
into corresponding fields of table gt_output
where cm~object_id like Mel_Nr.
As Sandra said, you can check if your code/the select works by using the debugger.
You can output data different ways, but the easiest is using the class CL_SALV_TABLE. Without adding any additional features (such as a title, toolbar buttons, sorting, hotspots, etc.), the below code is how you can display your data using the oo alv grid.
...
DATA: go_alv TYPE REF TO cl_salv_table,
gx_salv_msg TYPE REF TO cx_salv_msg.
...
TRY.
cl_salv_table=>factory(
IMPORTING
r_salv_table = go_alv
CHANGING
t_table = gt_output ).
CATCH cx_salv_msg INTO gx_salv_msg.
MESSAGE 'error' TYPE 'E'.
ENDTRY.
go_alv->display( ).
If you need a real one-liner, just use ABAP demo output standard class cl_demo_output that can handle any type including internal tables:
SELECT *
FROM scarr
INTO TABLE #DATA(carriers).
cl_demo_output=>display( carriers ).
I am pasting this program for example but i will never know the type of the table (here vbap and vbak).
My goals is to display my field symbol without knowing the types.
Is it possible ?
Here is my code :
REPORT ZTEST_FME_FOL.
type-pools slis .
FIELD-SYMBOLS : <mytable> TYPE ANY TABLE.
DATA : lv_alv_table TYPE REF TO cl_salv_table,
lv_funct TYPE REF TO cl_salv_functions,
lv_columns TYPE REF TO cl_salv_columns_table,
lv_column TYPE REF TO CL_SALV_COLUMN_table.
SELECT * from vbap INNER JOIN VBAK ON vbap~vbeln = vbak~vbeln UP TO 10 ROWS INTO TABLE <mytable>.
TRY.
cl_salv_table=>factory(
IMPORTING
r_salv_table = lv_alv_table
CHANGING
t_table = <mytable> ).
CATCH cx_salv_msg .
ENDTRY.
lv_funct = lv_alv_table->get_functions( ).
lv_funct->set_all( Abap_True ).
lv_columns = lv_alv_table->get_columns( ).
lv_alv_table->display( ).
Thanks in advance !
Depending on what you 're trying to do there's going to be more validation required than what I've done, but in essence this is what you need.
Using (dynamic) joins may be particularly tricky.
report zevw_test_dynamic_alv.
parameters: p_table type string obligatory.
field-symbols: <gt_table> type standard table.
data: gt_data type ref to data.
start-of-selection.
create data gt_data type table of (p_table).
assign gt_data->* to <gt_table>.
select * from (p_table) up to 10 rows
into table <gt_table>.
perform display_results using <gt_table>. "Your ALV stuff will be in here
You may even have to build the fieldcat manually and then use
call method cl_alv_table_create=>create_dynamic_table
exporting
it_fieldcatalog = gt_fieldcat[]
importing
ep_table = gt_data.
to get the data reference
I have one doubt. May I know what the difference between LIKE and LIKE LINE OF in ABAP is? I have seen somewhere that while declaring the work area they are declaring.
wa LIKE it_one
wa LIKE LINE OF it_one
LIKE LINE OF means that the variable will be of the table line type.
LIKE means that the variable will be exactly of the same type as the one sitting after this key word.
Example
TYPES: BEGIN OF t_my_example_structure,
my_example_field1 TYPE i,
my_example_field2 TYPE n,
END OF t_my_example_structure.
TYPES tt_my_example_structure TYPE STANDARD TABLE OF t_my_example_structure.
DATA: l_tab_my_example TYPE tt_my_example_structure.
* has structure of row of l_tab_my_example so in this case t_my_example_structure.
DATA: l_str_my_example LIKE LINE OF l_tab_my_example.
* is exactly the same table type as l_tab_my_example so in this case tt_my_example_structure.
DATA: l_tab_like_my_example LIKE l_tab_my_example.
* I use it often for LOOP AT <tab> ASSIGNING <fs>.
FIELD-SYMBOLS: <fs_str_my_example> LIKE LINE OF l_tab_my_example.
Well, the difference is when you pass table into subroutine with USING or TABLES.
In 1st case you will get a table without headerline, thus WA_LIKE will be a table too.
In 2nd case IT_DATA will be a table with headerline: this causes IT_DATA actually means IT_DATA as structure or IT_DATA[] as table, depending on context. Particulary, DATA ... LIKE IT_DATA will refer to headerline, and not entire internal table.
You may check this using a debugger:
DATA T_DATA TYPE STRING_TABLE.
PERFORM TEST_01 USING T_DATA.
PERFORM TEST_02 TABLES T_DATA.
FORM TEST_01 USING IT_DATA TYPE STRING_TABLE.
DATA : WA_LIKE LIKE IT_DATA "This is a Table
, WA_LINE LIKE LINE OF IT_DATA.
BREAK-POINT.
ENDFORM.
FORM TEST_02 TABLES IT_DATA TYPE STRING_TABLE.
DATA : WA_LIKE LIKE IT_DATA "This is a String
, WA_LINE LIKE LINE OF IT_DATA.
BREAK-POINT.
ENDFORM.