cancel
Showing results for 
Search instead for 
Did you mean: 

Add new row in table

Former Member
0 Kudos

Hi,

I have a table UI element with data in it.

When i click on Add Row button, an empty row should get added in to the table UI element.

Can anybody help me out as to how to do this?

Regards,

Kanakaraj V A

Accepted Solutions (1)

Accepted Solutions (1)

uday_gubbala2
Active Contributor
0 Kudos

Hi Kanakaraj,

In order to realise this functionality you would just have to bind an empty work area to your context node.

For example: I have a node by name SFLIGHT which am using to bind to a table on my layout. Try check the code snippet below for my button's action handler:

method ONACTIONADD_ROW .
  data: wd_node type ref to if_wd_context_node,
        wa_data type wd_this->element_sflight.
  wd_node = wd_context->get_child_node( name = 'SFLIGHT' ).
  wd_node->bind_structure( new_item             = wa_data
                           SET_INITIAL_ELEMENTS = ABAP_FALSE ).
endmethod.

Its important to set SET_INITIAL_ELEMENTS to ABAP_FALSE otherwise all the rows of the table would get deleted & it would just contain the newly added row.

Regards,

Uday

Former Member
0 Kudos

Hi Uday,

Thanks for the code. But when i click on the Add Row now, the row is added as the last row of the table and we have to manually navigate to that row to fill in the data.

Can i automatically change the focus to the newly added row?

Thanks,

Kanakaraj V A

Former Member
0 Kudos

Hi,

Use this code to set the lead selection to the newly added row.


Data : size type i.

size = node_table->get_element_count(  ).
node_table->set_lead_selection_index( index = size ).

uday_gubbala2
Active Contributor
0 Kudos

Hi Kandha,

Setting the lead selection doesn't help in getting focus to the last row. The user would still have to scroll down to the newly added record. Kanakaraj I am checking out & will let you know how you can scroll down to the last record.

Regards,

Uday

Former Member
0 Kudos

Hi Kandha,

I did this. The focus goes to the newly added element. but if i have to view the element then i have to traverse manually.

Regards,

Kanakaraj V A

uday_gubbala2
Active Contributor
0 Kudos

Hi Kanakaraj,

As a workaround you can try add the new record at the beginning of the table if you want by specifying the index while binding the structure:

wd_node->bind_structure( new_item             = wa_data
                                            index               = 1
                           SET_INITIAL_ELEMENTS  = ABAP_FALSE ).

I would in the meantime search and tell you know how you can append the row and also have the focus set over it.

Regards,

Uday

uday_gubbala2
Active Contributor
0 Kudos

Hi Kanakaraj,

You can try proceed as follows. Your table has a property called FirstVisibleRow. This determines as to which row of the table should be visible to the user. By default it has a value of zero. So try as:

1) Bind the tables FirstVisibleRow to a context attribute which has a value 0 by default

2) When the user clicks on the add row button:

a) Get the count of total number of rows in the table

b) Set the value of attribute to which FirstVisibleRow is bound to count + 1

Check the code snippet below:

method ONACTIONADD_ROW .
  data: wd_node type ref to if_wd_context_node,
        wa_data type wd_this->element_sflight,
        count type i.
  wd_node = wd_context->get_child_node( name = 'SFLIGHT' ).
" Get the count of rows in the table
  count = wd_node->get_element_count( ).
  count = count + 1.
  wd_node->bind_structure( new_item             = wa_data
                           SET_INITIAL_ELEMENTS = ABAP_FALSE ).
" Set the value of context attribute to which FirstVisibleRow is bound to
  wd_node = wd_context->get_child_node( name = 'SCROLL' ).
  wd_node->set_attribute( exporting name  = 'FIRSTVISIBLEROW'
                                    value = count ).
endmethod.

Regards,

Uday

Former Member
0 Kudos

Hi Uday,

This is what i wanted. ie: I wanted to add a new row in the beginning of the table.

I tried the code which u sent.

I am getting an error: "Dynamic type conflict when assigning references".

I am not able to find out whts the problem.

Can you help me out in this.

Regards,

Kanakaraj V A

uday_gubbala2
Active Contributor
0 Kudos

Hi Kanakaraj,

This is the complete code that you should use for adding a record at the beginning of the table:

method ONACTIONADD_ROW .
  data: wd_node type ref to if_wd_context_node,
        wa_data type wd_this->element_sflight,
        count type i.
  wd_node = wd_context->get_child_node( name = 'SFLIGHT' ).
  count = wd_node->get_element_count( ).
  count = count + 1.
  wd_node->bind_structure( new_item             = wa_data
                           index                = 1
                           SET_INITIAL_ELEMENTS = ABAP_FALSE ).
endmethod.

This code is working fine for my component. Just check it out.

Regards,

Uday

Former Member
0 Kudos

Hi Uday,

This piece of code solved the problem.

Thanks a lot for the help.

Thanks and Regards,

Kanakaraj V A

Answers (2)

Answers (2)

Former Member
0 Kudos

Hi,

Step1: read the Table properties (Table name) by using if_wd_view try it in wdDoModifyView.

Step2: by using class cl_wd_table you can add new row to your table. (in SE24 you can check class properties)

  • Put this code in your conditions for example when you click the button a new row has to add to the table

Best Regards

Ravi

Former Member
0 Kudos

Hi kanakaraj,

Try out this...


Data : node_table type ref to if_wd_context_node,
       elem_table type ref to if_wd_context_element.
node_table = wd_context->get_child_node( 'TABLE' ).
elem_table = node_table->create_element( ).
node_table->bind_element(  new_item = elem_table set_initial_elements = abap_false ).

Hope this helps..

Thanks,

Mugundhan