KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Setting all items in a Hierarchical List to Collapsed or Expanded
PRODUCT: 4D | VERSION: 12 | PLATFORM: Mac & Win
Published On: August 26, 2010

There is no property for a 4D Hierarchical List to set it for all items to be collapsed or expanded by default. Below are two methods that can be called during the On Load event (of an object or form) to set a Hierarchical List to collapsed or expanded.

If (True)
    If (False)
         `//************************************************************************
         `//
         `// HL_CollapseAll ( $HList_L )
         `//
         `// Purpose: Collapse all items in a HList.
         `// Loops through list items until all subitems have been collapsed
         `//
         `// $1 - LONGINT - list ref to collapse
         `//
         `//************************************************************************

    End if
    C_TEXT($MethodName_T)
    $MethodName_T:=Current method name
     `//=====================Declare Variables==================================
     `//method_parameters_declarations

    C_LONGINT($HList_L;$1)
     `//---------------------------------------------------------------------------
     `//method_wide_constants_declarations
     `//---------------------------------------------------------------------------
     `//local_variable_declarations

    C_LONGINT($ItemCount;$ItemNum;$ItemRef;$SubList)
    C_TEXT($ItemText)
    C_BOOLEAN($Expanded;$Done)
    
End if
  `//======================Initialize and Setup===========================

$HList_L:=$1

  `//========================Method Actions=============================

If (Is a list($HList_L))
    $ItemCount:=Count list items($HList_L;*)
    For ($ItemNum;$ItemCount;1;-1)
        GET LIST ITEM($HList_L;$ItemNum;$itemRef;$ItemText;$SubList;$Expanded)
        If ($sublist#0)
            If ($Expanded)
                SET LIST ITEM($HList_L;$itemRef;$ItemText;$ItemRef;$SubList;False)
            End if
        End if
    End for
End if


Note that in the method above the loop is from bottom to top, and top to bottom in the mehod below. This is important because of how 4D sees the expanded state of sublists.

If (True)
    If (False)
         `// **********************************************************************
         `//
         `// HL_ExpandAll ( $HList_L )
         `//
         `// Purpose: Expand all items in a HList.
         `// Loops through list items until all subitems have been expanded
         `//
         `// $1 - C_LONGINT - list ref to expand
         `//
         `// **********************************************************************

    End if
    C_TEXT($MethodName_T)
    $MethodName_T:=Current method name
     `// =====================Declare Variables==================================
     `// method_parameters_declarations

    C_LONGINT($HList_L;$1)
     `// -------------------------------------------------------------------------
     `// method_wide_constants_declarations
     `// -------------------------------------------------------------------------
     `// local_variable_declarations

    C_LONGINT($ItemCount;$ItemNum;$ItemRef;$SubList)
    C_TEXT($ItemText)
    C_BOOLEAN($Expanded)
    
End if
  `// ======================Initialize and Setup================================

$HList_L:=$1

  `// ========================Method Actions==================================

If (Is a list($HList_L))
    $ItemCount:=Count list items($HList_L;*)
    For ($ItemNum;1;$ItemCount)
        GET LIST ITEM($HList_L;$ItemNum;$itemRef;$ItemText;$SubList;$Expanded)
        If ($sublist#0)
            If (Not($Expanded))
                SET LIST ITEM($HList_L;$itemRef;$ItemText;$ItemRef;$SubList;True)
            End if
        End if
    End for
End if