Rrecursively open or collapse a hierarchical list

somebody ask for a method to recursively open or collapse a hierarchical list.

it was actually more complex than I initially thought.

<code 4D>
C_LONGINT($1;$l)
C_BOOLEAN($2;$expand)
C_POINTER($3;$4;$5)

$l:=$1
$expand:=$2

C_LONGINT($itemRef;$asublist)
C_TEXT($itemText)
C_BOOLEAN($aexpanded)

If ($expand)

ARRAY LONGINT($selects;0)
$select:=Selected list items($l;$selects;*)

For ($i;1;Count list items($l;*))
	SELECT LIST ITEMS BY POSITION($l;$i)
	GET LIST ITEM($l;*;$itemRef;$itemText;$asublist;$aexpanded)
	SET LIST ITEM($l;*;$itemText;$itemRef;$asublist;$expand)
End for 

SELECT LIST ITEMS BY REFERENCE($l;$select;$selects)

Else

If (Count parameters=5)
	$lists:=$3
	$sublists:=$4
	$labels:=$5
Else 
	ARRAY LONGINT($_lists;0)
	ARRAY LONGINT($_sublists;0)
	ARRAY TEXT($_labels;0)
	$lists:=->$_lists
	$sublists:=->$_sublists
	$labels:=->$_labels
End if 

For ($i;1;Count list items($l))
	GET LIST ITEM($l;$i;$itemRef;$itemText;$asublist;$aexpanded)
	If (Is a list($asublist))
		EXECUTE METHOD(Current method name;*;$asublist;$expand;$lists;$sublists;$labels)
		If (Not($expand)) & ($aexpanded)
			APPEND TO ARRAY($lists->;$itemRef)
			APPEND TO ARRAY($sublists->;$asublist)
			APPEND TO ARRAY($labels->;$itemText)
		End if 
	End if 
End for 

If (Count parameters#5)
	For ($i;1;Size of array($_lists))
		$itemRef:=$_lists{$i}
		$asublist:=$_sublists{$i}
		$itemText:=$_labels{$i}
		SET LIST ITEM($l;$itemRef;$itemText;$itemRef;$asublist;$expand)
	End for 
End if 

End if
</code 4D>

Strange as is sounds I checked into my collection of jewels and did not find such a basic tool although this goes back to 4D version 2004 or so…

I have a few hierarchical lists with full expand-collapse but only on 2 levels…

Short and beautiful, thanks you so much

line 43
<code 4D>
EXECUTE METHOD(Current method name;*;$asublist;$expand;$lists;$sublists;$labels)
</code 4D>
is a recursive call. I named the method HierList_OpenCollapse. Why not use
<code 4D>
HierList_OpenCollapse ($asublist;$expand;$lists;$sublists;$labels)
</code 4D>
directly, omitting the execute-command, reducing a layer of abstraction?

you can rename the variables and yes, replace EXECUTE METHOD with a direct call.

I just wanted to make the method portable, ready to copy and paste. that’s all. :mrgreen: