ALV 列文本和颜色不会改变

ALV Column Text and Color won't change

我遇到了 SAP ALV 问题。 我想更改列的文本和颜色。 如果我直接调用该列 ('BONUS') 但当我 在 DO 子句中使用它并连接它不起作用的列的名称 因为某些原因。 虽然找到了列并且更改了属性,但至少这是我在调试时看到的。

我希望有人能帮助我。

以下是我应该更改属性的编码的一部分。

        Data: Count Type i.
  DATA: h_fieldname TYPE char30.
  DATA: col_s         TYPE scrtext_s.

  DATA: col_m         TYPE scrtext_m.

  DATA: col_l         TYPE scrtext_l.

  DATA: colname(30) TYPE c.

DATA: lr_functions TYPE REF TO cl_salv_functions_list.

 DATA: gr_columns    TYPE REF TO cl_salv_columns_table.

 DATA: gr_column     TYPE REF TO cl_salv_column_table.

DATA: alv TYPE REF TO cl_salv_table.

  lv_color_red-col = 7.
  lv_color_red-int = 1.
  lv_color_red-inv = 0.

  lv_color_blue-col = 4.
  lv_color_blue-int = 1.
  lv_color_blue-inv = 0.

  lv_color_green-col = 5.
  lv_color_green-int = 1.
  lv_color_green-inv = 0.
            DO gv_row_count TIMES.

          ADD 1 TO count.
          new = count.

          CLEAR: h_fieldname, colname.
          CONCATENATE 'JAHR' new 'P' INTO h_fieldname.
          CONCATENATE 'Jahr' new INTO colname.
          col_s = colname.
          col_m = colname.
          col_l = colname.
          gr_column ?= gr_columns->get_column( h_fieldname ).
          gr_column->set_short_text( col_s ).
          gr_column->set_medium_text( col_m ).
          gr_column->set_long_text( col_l ).
          gr_column->set_color( lv_color_blue ).

          CLEAR: h_fieldname, colname.
          CONCATENATE 'JAHR' new 'W' INTO h_fieldname.
          CONCATENATE 'Jahr' new INTO colname.
          col_s = colname.
          col_m = colname.
          col_l = colname.
          gr_column ?= gr_columns->get_column( h_fieldname ).
          gr_column->set_short_text( col_s ).
          gr_column->set_medium_text( col_m ).
          gr_column->set_long_text( col_l ).
        ENDDO.

我正在发布这两种方法的代码。第一个是构建一个 fieldcatalog 并创建一个全局 table,以便 fieldcatalog 可以在第二种方法中使用。这个应该填写字段目录,配置 ALV(如列文本和颜色)并显示结果。

EDIT: Here are also the attributes etc. used inside the methods:
  data GT_DATATAB type ref to DATA .
  class ZCL_BONUS_PROCESSING definition load .
  data GT_STANDARD type ZCL_BONUS_PROCESSING=>TTY_FINAL_STANDARD .
  data GT_ADVANCED type ZCL_BONUS_PROCESSING=>TTY_FINAL_ADVANCED .
  data GV_ROW_COUNT type I .


METHOD build_fieldcatalog.

  FIELD-SYMBOLS: <t_itab> TYPE table.
  DATA: t_fieldcat  TYPE lvc_t_fcat.
  DATA: s_fieldcat  TYPE LINE OF lvc_t_fcat.
  DATA: count TYPE i.
*  DATA: new(2) TYPE c.
  DATA: new TYPE string.
  DATA: final_count TYPE i.
  DATA: h_fieldname TYPE char30.

  " Personalnummer
  CLEAR: s_fieldcat.
  s_fieldcat-fieldname  = 'PERNR'.
  s_fieldcat-ref_table  = 'P0000'.
  s_fieldcat-ref_field  = 'PERNR'.
  s_fieldcat-key        = 'X'.
  APPEND s_fieldcat TO t_fieldcat.

  " Zielbonus
  CLEAR: s_fieldcat.
  s_fieldcat-fieldname  = 'BONUS'.
  s_fieldcat-inttype = 'C'.
  s_fieldcat-outputlen = '15'.
*  s_fieldcat-ref_table  = 'P0014'.
*  s_fieldcat-ref_field  = 'YY_BETRAG'.
  s_fieldcat-key        = 'X'.
  APPEND s_fieldcat TO t_fieldcat.

  " Bonusanspruch
  CLEAR: s_fieldcat.
  s_fieldcat-fieldname  = 'BONAN'.
  s_fieldcat-inttype = 'C'.
  s_fieldcat-outputlen = '15'.
*  s_fieldcat-ref_table  = 'P0014'.
*  s_fieldcat-ref_field  = 'YY_BETRAG'.
  s_fieldcat-key        = ''.
  APPEND s_fieldcat TO t_fieldcat.

  " Anspruch
  CLEAR: s_fieldcat.
  s_fieldcat-fieldname  = 'ANSPR'.
*  s_fieldcat-ref_table  = 'P0008'.
*  s_fieldcat-ref_field  = 'TRFGR'.
  s_fieldcat-inttype = 'C'.
  s_fieldcat-outputlen = '1'.
  s_fieldcat-key        = ''.
  APPEND s_fieldcat TO t_fieldcat.

  " Sonderregelung
  CLEAR: s_fieldcat.
  s_fieldcat-fieldname  = 'SONDE'.
  s_fieldcat-inttype    = 'I'.
  s_fieldcat-outputlen  = '1'.
  s_fieldcat-key        = ''.
  APPEND s_fieldcat TO t_fieldcat.

  " Kommentar
  CLEAR: s_fieldcat.
  s_fieldcat-fieldname  = 'COMM'.
  s_fieldcat-inttype    = 'C'.
  s_fieldcat-outputlen  = '50'.
  s_fieldcat-key        = ''.
  APPEND s_fieldcat TO t_fieldcat.

  " Mitarbeiterkreis
  CLEAR: s_fieldcat.
  s_fieldcat-fieldname  = 'PERSK'.
  s_fieldcat-ref_table  = 'P0001'.
  s_fieldcat-ref_field  = 'PERSK'.
  s_fieldcat-key        = 'X'.
  APPEND s_fieldcat TO t_fieldcat.

  count = 0.

  DO gv_row_count TIMES.

    ADD 1 TO count.
    new = count.

    " Lohnart (Jahr) Prozent
    CLEAR: h_fieldname.
    CONCATENATE 'JAHRP' new INTO h_fieldname.
    CONDENSE h_fieldname NO-GAPS.
    CLEAR: s_fieldcat.
    s_fieldcat-fieldname  = h_fieldname.
*    s_fieldcat-ref_table  = 'PZI02'.
*    s_fieldcat-ref_field  = 'ANZHL'.
    s_fieldcat-inttype    = 'C'.
    s_fieldcat-outputlen  = '15'.
    s_fieldcat-coltext    = h_fieldname.
    s_fieldcat-seltext    = h_fieldname.
    s_fieldcat-key        = ''.
    APPEND s_fieldcat TO t_fieldcat.

    " Lohnart (Jahr) Währung
    CLEAR: h_fieldname.
    CONCATENATE 'JAHRW' new INTO h_fieldname.
    CONDENSE h_fieldname NO-GAPS.
    CLEAR: s_fieldcat.
    s_fieldcat-fieldname  = h_fieldname.
*    s_fieldcat-ref_table  = 'P0014'.
*    s_fieldcat-ref_field  = 'YY_BETRAG'.
    s_fieldcat-inttype    = 'C'.
    s_fieldcat-outputlen  = '15'.
    s_fieldcat-coltext    = h_fieldname.
    s_fieldcat-seltext    = h_fieldname.
    s_fieldcat-key        = ''.
    APPEND s_fieldcat TO t_fieldcat.

  ENDDO.

  " Probezeit
  CLEAR: s_fieldcat.
  s_fieldcat-fieldname  = 'PROBE'.
  s_fieldcat-inttype    = 'D'.
  s_fieldcat-outputlen  = '10'.
  s_fieldcat-key        = ''.
  s_fieldcat-col_opt    = 'X'.
  APPEND s_fieldcat TO t_fieldcat.

* Datentabelle aus Feldkatalog erstellen
  CALL METHOD cl_alv_table_create=>create_dynamic_table
    EXPORTING
      it_fieldcatalog = t_fieldcat
    IMPORTING
      ep_table        = gt_datatab.

ENDMETHOD.


METHOD fill_and_display_alv.

  DATA: lstab     TYPE REF TO data.
  DATA: lv_offset TYPE i.
  DATA: dat       TYPE begda.
*  DATA: new(2) TYPE c.
  DATA: new TYPE string.
  DATA: count TYPE i.
  DATA: h_fieldname TYPE char30.
  DATA: col_s         TYPE scrtext_s.
  DATA: col_m         TYPE scrtext_m.
  DATA: col_l         TYPE scrtext_l.
  DATA: colname TYPE char30.
  DATA: lr_functions TYPE REF TO cl_salv_functions_list.
  DATA: gr_columns    TYPE REF TO cl_salv_columns_table.
  DATA: gr_column     TYPE REF TO cl_salv_column_table.
  DATA: alv TYPE REF TO cl_salv_table.
  DATA: exc TYPE REF TO cx_salv_msg.
*  DATA: t_standard TYPE TABLE OF sty_standard.
  DATA: s_standard TYPE zcl_bonus_data_collect=>sty_final_standard.
*  DATA: t_advanced TYPE TABLE OF sty_advanced.
  DATA: s_advanced TYPE zcl_bonus_data_collect=>sty_final_advanced.

  DATA: lv_color_red TYPE lvc_s_colo.
  DATA: lv_color_blue TYPE lvc_s_colo.
  DATA: lv_color_green TYPE lvc_s_colo.

  lv_color_red-col = 7.
  lv_color_red-int = 1.
  lv_color_red-inv = 0.

  lv_color_blue-col = 4.
  lv_color_blue-int = 1.
  lv_color_blue-inv = 0.

  lv_color_green-col = 5.
  lv_color_green-int = 1.
  lv_color_green-inv = 0.

  FIELD-SYMBOLS: <ls_itab> TYPE ANY,
                 <ls_comp> TYPE ANY.
  FIELD-SYMBOLS: <t_itab> TYPE table.


* Datentabelle füllen
  ASSIGN gt_datatab->* TO <t_itab>.
  cl_salv_table=>factory( IMPORTING r_salv_table = alv  CHANGING t_table = <t_itab> ).

* Zeilenstruktur erzeugen
  CREATE DATA lstab LIKE LINE OF <t_itab>.
  ASSIGN lstab->* TO <ls_itab>.

  LOOP AT gt_standard INTO s_standard.

    CLEAR: s_advanced, <ls_itab>.

    " Personalnummer
    lv_offset = 1.
    ASSIGN COMPONENT lv_offset OF STRUCTURE <ls_itab> TO <ls_comp>.
    IF sy-subrc EQ 0.
      <ls_comp> = s_standard-pernr.
    ENDIF.

    " Zielbonus
    lv_offset = 2.
    ASSIGN COMPONENT lv_offset OF STRUCTURE <ls_itab> TO <ls_comp>.
    IF sy-subrc EQ 0.
      <ls_comp> = s_standard-ziel_bonus.
    ENDIF.

    " Höhe des Anspruchs auf Zielbonus
    lv_offset = 3.
    ASSIGN COMPONENT lv_offset OF STRUCTURE <ls_itab> TO <ls_comp>.
    IF sy-subrc EQ 0.
      <ls_comp> = s_standard-ziel_bonus_anspruch.
    ENDIF.

    " Anspruch
    lv_offset = 4.
    ASSIGN COMPONENT lv_offset OF STRUCTURE <ls_itab> TO <ls_comp>.
    IF sy-subrc EQ 0.
      <ls_comp> = s_standard-anspruch.
    ENDIF.

    " Sonderregelung
    lv_offset = 5.
    ASSIGN COMPONENT lv_offset OF STRUCTURE <ls_itab> TO <ls_comp>.
    IF sy-subrc EQ 0.
      <ls_comp> = s_standard-sonderreg.
    ENDIF.

    " Kommentar
    lv_offset = 6.
    ASSIGN COMPONENT lv_offset OF STRUCTURE <ls_itab> TO <ls_comp>.
    IF sy-subrc EQ 0.
      <ls_comp> = s_standard-comm.
    ENDIF.

    " Mitarbeiterkreis
    lv_offset = 7.
    ASSIGN COMPONENT lv_offset OF STRUCTURE <ls_itab> TO <ls_comp>.
    IF sy-subrc EQ 0.
      <ls_comp> = s_standard-persk.
    ENDIF.

    " Beginn Zeitintervalle
    LOOP AT gt_advanced INTO s_advanced WHERE pernr = s_standard-pernr.

      ADD 1 TO lv_offset.

      " Beschäftigungsgrad
      ASSIGN COMPONENT lv_offset OF STRUCTURE <ls_itab> TO <ls_comp>.
      IF sy-subrc EQ 0.
        <ls_comp> = s_advanced-year_percent.
      ENDIF.

      ADD 1 TO lv_offset.

      " Bonuswert (Basiswert)
      ASSIGN COMPONENT lv_offset OF STRUCTURE <ls_itab> TO <ls_comp>.
      IF sy-subrc EQ 0.
        <ls_comp> = s_advanced-year_quantity.
      ENDIF.

    ENDLOOP.

    lv_offset = gv_row_count * 2 + 7.


    ADD 1 TO lv_offset.

    " Probezeit
    ASSIGN COMPONENT lv_offset OF STRUCTURE <ls_itab> TO <ls_comp>.
    IF sy-subrc EQ 0.
      <ls_comp> = s_standard-probez.
    ENDIF.

    ADD 1 TO lv_offset.

* Struktur dem Feldkatalog übergeben
    APPEND <ls_itab> TO <t_itab>.

    TRY .

** Funktionsleiste
        lr_functions = alv->get_functions( ).
        lr_functions->set_all( if_salv_c_bool_sap=>true ).
        lr_functions->set_print( if_salv_c_bool_sap=>true ).

** Optimierung der Spaltengröße
        gr_columns = alv->get_columns( ).
        gr_columns->set_optimize( abap_true ).


        count = 0.
        DO gv_row_count TIMES.

          ADD 1 TO count.
          new = count.

          CLEAR: h_fieldname, colname.
          CONCATENATE 'JAHRP' new INTO h_fieldname.
          CONDENSE h_fieldname NO-GAPS.
          CONCATENATE 'Jahr' new INTO colname.
          CONDENSE colname NO-GAPS.
          col_s = colname.
          col_m = colname.
          col_l = colname.
          gr_column ?= gr_columns->get_column( columnname = h_fieldname ).
          gr_column->set_short_text( col_s ).
          gr_column->set_medium_text( col_m ).
          gr_column->set_long_text( col_l ).
          gr_column->set_color( lv_color_blue ).

          CLEAR: h_fieldname, colname.
          CONCATENATE 'JAHRW' new INTO h_fieldname.
          CONDENSE h_fieldname NO-GAPS.
          CONCATENATE 'Jahr' new INTO colname.
          CONDENSE colname NO-GAPS.
          col_s = colname.
          col_m = colname.
          col_l = colname.
          gr_column ?= gr_columns->get_column( columnname = h_fieldname ).
          gr_column->set_short_text( col_s ).
          gr_column->set_medium_text( col_m ).
          gr_column->set_long_text( col_l ).
        ENDDO.

      CATCH cx_salv_msg INTO exc.
        MESSAGE exc TYPE 'I' DISPLAY LIKE 'E'.

    ENDTRY.

  ENDLOOP.

  cl_salv_table=>factory( IMPORTING r_salv_table = alv  CHANGING t_table = <t_itab> ).

* Funktionsleiste
  lr_functions = alv->get_functions( ).
  lr_functions->set_all( if_salv_c_bool_sap=>true ).
  lr_functions->set_print( if_salv_c_bool_sap=>true ).

* Optimierung der Spaltengröße
  gr_columns = alv->get_columns( ).
  gr_columns->set_optimize( abap_true ).

* Titel
  gr_column ?= gr_columns->get_column( 'BONUS' ).
  gr_column->set_short_text( 'Zielbonus' ).
  gr_column->set_medium_text( 'Zielbonus' ).
  gr_column->set_long_text( 'Aliq. Zielbonus' ).
  gr_column->set_color( lv_color_red ).

  gr_column ?= gr_columns->get_column( 'BONAN' ).
  gr_column->set_short_text( 'B.anspruch' ).
  gr_column->set_medium_text( 'Bonusanspruch' ).
  gr_column->set_long_text( 'Bonusanspruch' ).

  gr_column ?= gr_columns->get_column( 'ANSPR' ).
  gr_column->set_short_text( 'Anspruch' ).
  gr_column->set_medium_text( 'Anspruch' ).
  gr_column->set_long_text( 'Anspruch' ).

  gr_column ?= gr_columns->get_column( 'SONDE' ).
  gr_column->set_short_text( 'Sond.reg.' ).
  gr_column->set_medium_text( 'Sonderregel.' ).
  gr_column->set_long_text( 'Sonderregelung' ).

  gr_column ?= gr_columns->get_column( 'COMM' ).
  gr_column->set_short_text( 'Komm.' ).
  gr_column->set_medium_text( 'Kommentar' ).
  gr_column->set_long_text( 'Kommentar' ).

  gr_column ?= gr_columns->get_column( 'PROBE' ).
  gr_column->set_short_text( 'Probez.' ).
  gr_column->set_medium_text( 'Probezeit' ).
  gr_column->set_long_text( 'Probezeit' ).
  gr_column->set_color( lv_color_green ).


  alv->display( ).

ENDMETHOD.

如果它适用于一列,则没有理由不适用于另一列。

像往常一样对您的代码进行故障排除。确保列名写得好,并且是ALV中已有列的名称table.

嗯...您没有指定变量的类型 new 但我假设它是 STRING。如果它正在做这样的赋值

new = count.

不会去掉count变量末尾的space。

这个简单的程序展示了它。

REPORT ZZZ.

DATA: gv_row_count TYPE i VALUE 30.
DATA: fieldname TYPE char30.
DATA: new TYPE string.

START-OF-SELECTION.
   new = gv_row_count.
   CONCATENATE 'JAHR' new 'P' INTO fieldname.
   WRITE fieldname.

输出是 JAHR30 P 而不是 JAHR30P

要解决此问题,您可以在 CONCATENATE 之后添加以下语句。

CONDENSE h_fieldname NO-GAPS.

甚至更整洁,只需使用字符串模板而不是连接,那么您将不需要变量 new.

h_fieldname = |JAHR{ count }P|.