cancel
Showing results for 
Search instead for 
Did you mean: 

restoreViewState with HTMLB-TREES/TREENODES

Former Member
0 Kudos

Hello,

we want to achieve that our dynamic tree will

be built up in the state it was before we jumped onto another page. When navigating backwards, the tree

does not remember any more it treenodes properties.

Just the root is displayed, not any more the information which node was open and which not.

Also restoreViewState = "TRUE" did not help.

Any ideas ?

Thanks.

Accepted Solutions (1)

Accepted Solutions (1)

thomas_jung
Developer Advocate
Developer Advocate
0 Kudos

Have you tried the following.

When you create a <htmlb:tree> and use the restoreViewState, the application will generate hidden input fields for each of the tree nodes. It uses these input fields to persist the view state.

Now in your inputProcessing before you do the navigation, you could use a call to request->get_form_fields to get a listing of all these hidden input fields and their values.

DATA: fields TYPE tihttpnvp.
request->get_form_fields( CHANGING fields = fields ).
IF sy-subrc NE 0.
ENDIF.

You could persist these values yourself using a server cookie perhaps. Then on the Initialization of the original page when you navigate back, you check for this server cookie. If you find it, use the values from the hidden input fields to set the inital state of each tree node manually.

This should have the effect that you are looking for. Let me know if you need code samples for the server cookie or other parts of this solution.

thomas_jung
Developer Advocate
Developer Advocate
0 Kudos

Here is a complete (yet simplistic) example that proves that the serialization idea works.

Stateless Application

Page One called tree.htm

Page Attributes (Auto Flag is NOT set)

h1_isopen TYPE STRING

h2_isopen TYPE STRING

view_state TYPE SHSVALTAB

OnInitialization

* event handler for data retrieval
 DATA: istream TYPE string,
         xslt_err TYPE REF TO cx_xslt_exception.

**** Read Server cookie
 CALL METHOD cl_bsp_server_side_cookie=>get_server_cookie
   EXPORTING
     name                  = 'view_state'
     application_name      = runtime->application_name
     application_namespace = runtime->application_namespace
     username              = sy-uname
     session_id            = runtime->session_id
     data_name             = 'view_state'
   CHANGING
     data_value            = istream.

**** deserialize the Class
 IF istream IS NOT INITIAL.
   TRY.
       CALL TRANSFORMATION id
       SOURCE XML istream
       RESULT view_state = view_state.
     CATCH cx_xslt_exception INTO xslt_err.
   ENDTRY.
 ENDIF.

 CHECK view_state IS NOT INITIAL.
 FIELD-SYMBOLS: <wa_state> LIKE LINE OF view_state.
 LOOP AT view_state ASSIGNING <wa_state>.
   IF <wa_state>-key CS 'Header1'.
     IF <wa_state>-value = 'O'.
       h1_isopen = 'TRUE'.
     ENDIF.
   ENDIF.
   IF <wa_state>-key CS 'Header2'.
     IF <wa_state>-value = 'O'.
       h2_isopen = 'TRUE'.
     ENDIF.
   ENDIF.
 ENDLOOP.

OnInputProcessing

* event handler for checking and processing user input and
* for defining navigation
DATA: fields TYPE tihttpnvp.
request->get_form_fields( CHANGING fields = fields ).
FIELD-SYMBOLS: <wa_field> LIKE LINE OF fields,
               <wa_state> LIKE LINE OF view_state.
LOOP AT fields ASSIGNING <wa_field>.
  IF <wa_field>-name CS 'test'. "My Tree ID
    APPEND INITIAL LINE TO view_state ASSIGNING <wa_state>.
    <wa_state>-key   = <wa_field>-name.
    <wa_state>-value = <wa_field>-value.
  ENDIF.
ENDLOOP.

DATA: ostream TYPE string,
        xslt_err TYPE REF TO cx_xslt_exception.

***** serialize model class
TRY.
    CALL TRANSFORMATION id
    SOURCE view_state = view_state
    RESULT XML ostream.

****Write cookie it into the Server Cookie
    cl_bsp_server_side_cookie=>set_server_cookie( name = 'view_state'
      application_name = runtime->application_name
      application_namespace = runtime->application_namespace
      username = sy-uname
      session_id = runtime->session_id
      data_name = 'view_state'
      data_value = ostream
      expiry_time_rel = '1200' ).
  CATCH cx_xslt_exception INTO xslt_err.
ENDTRY.

navigation->goto_page( 'Navigate.htm' ).

Layout of tree.htm

<%@page language="abap" %>
<%@extension name="htmlb" prefix="htmlb" %>
<htmlb:content design="design2003" >
  <htmlb:page title=" " >
    <htmlb:form>
      <htmlb:tree id               = "test"
                  restoreViewState = "TRUE" >
        <htmlb:treeNode id     = "Header1"
                        text   = "Header1"
                        isOpen = "<%= h1_isopen %>" >
          <htmlb:treeNode id     = "Header2"
                          text   = "Header2"
                          isOpen = "<%= h2_isopen %>" >
            <htmlb:treeNode id   = "Item1"
                            text = "Item1" />
            <htmlb:treeNode id   = "Item2"
                            text = "Item2" />
          </htmlb:treeNode>
          <htmlb:treeNode id   = "Item3"
                          text = "Item3" />
          <htmlb:button text    = "test"
                        onClick = "TEST" />
        </htmlb:treeNode>
      </htmlb:tree>
    </htmlb:form>
  </htmlb:page>
</htmlb:content>

Second Page Navigate.htm (Just a dummy page to simulate Navigation)

OnInputProcessing

* event handler for checking and processing user input and
* for defining navigation
navigation->goto_page( 'tree.htm' ).

Layout for Navigate.htm

<%@page language="abap"%>
<%@extension name="htmlb" prefix="htmlb"%>

<htmlb:content design="design2003">
  <htmlb:page title = " ">
    <htmlb:form>

      <htmlb:textView     text          = "Hello World!"
                          design        = "EMPHASIZED" />

      <htmlb:button       text          = "Press Me"
                          onClick       = "myClickHandler" />

    </htmlb:form>
  </htmlb:page>
</htmlb:content>

Answers (2)

Answers (2)

athavanraja
Active Contributor
0 Kudos

restoreViewState = "TRUE" would work if you are in the same page and making server round trips. if you naviagate away from the page and coming back ot it, restoreViewState = "TRUE" wont help. As Thomas suggested, you should write the state of the tree to server side cookie and read it back to set it to last state of the tree.

Regards

Raja

Former Member
0 Kudos

Check out this weblog to get you started:

/people/durairaj.athavanraja/blog/2004/11/21/bsphow-to-build-performance-efficient-dynamic-htmlbtree

You should also do a search in this forum for treeNode because this has been a topic covered quite a bit but I think the weblog will be enough help to get you to where you need.