I guess something like this could work...

```
d := { 
        Dictionary
                with: #n -> 'whatever'
                with: #d -> {
                        Dictionary with: #a -> #AA with: #b -> #BB.
                        Dictionary with: #c -> #CC with: #d -> #DD
                }.

        Dictionary
                with: #n -> 'wherever'
                with: #d -> {
                        Dictionary
                                with: #a -> #AA
                                with: #b -> #BB.
                        Dictionary with: #c -> #CC with: #d -> #DD
                }.
}.

TreeModel new 
        roots: d;
        childrenBlock: [ :item |
                item isDictionary
                        ifTrue: [
                                item associations flatCollect: [ :pair |
                                        pair value isArray
                                                ifTrue: [ pair value ]
                                                ifFalse: [ {pair} ].
                                ]
                        ]
                        ifFalse: [ #() ]
        ];
        openWithSpec
```

but forcing this much behavior is a bad practice... because it's hard to read, 
hard to understand, and hard to test.

So I suggest using custom classes as nodes (so each node can simply tell what 
its children are), and then delegate the retrieval... you could also add 
extension methods to the respective classes' protocols to acheive the same...

TreeModel new
        childrenBlock: [ :item |
                item children
        ];
        displayBlock: [ :item |
                item name
        ]

Peter


On Mon, Apr 17, 2017 at 09:12:47AM -0700, chrismihaylyk wrote:
> Hello!
> 
> Could anyone help me, please?
> 
> I have faced with a problem of TreeModel childrenBlock: for more complex
> build in structure.
> 
> For example, I have anOrderedCollection of Dictionary, and in each
> dictionary, there are exist associations, which values are     
> anArray(aDictionary, aDictionary ...).
> 
> I need to make drop downs for all of these entire levels of arrays and
> dictionaries.
> For first level I made it:
> 
> collectionsList
>               ifNil: [ ^ nil ]
>               ifNotNil: [ collectionsList hasChildrenBlock: [ :item | item
> isMyDictionary. ].
>                       collectionsList
>                               childrenBlock: [ :item | item associations
>                                                               collect: [ 
> :assoc | 
>                                                                       assoc 
> key asString , '  ->  ' , assoc value asString. ] ] ]
> 
> MyDictionaly is the class, inherited from Dictionary, just to override
> method printOn:
> 
> At the first picture, it's how it looks like, how I made it. 
> At the second - how I expect it to be (taken from Inspector window).
> 
> Thanks a lot!
> <http://forum.world.st/file/n4942409/before.jpg> 
> <http://forum.world.st/file/n4942409/after.jpg> 
> 
> 
> 
> 
> --
> View this message in context: 
> http://forum.world.st/How-childrenBlock-in-TreeModel-works-tp4940884p4942409.html
> Sent from the Pharo Smalltalk Users mailing list archive at Nabble.com.
> 

Reply via email to