Widget - identify if item is a group or not

In my custom widgets I have a list and a repeater under which I have group members

If the group member is itself a group then I want one control - a custom widget, which will expand for its group members. If it’s not a group, then I need a different widget. I therefore need to be able to test whether the item is a group. The item object in widgets, as has been discussed elsewhere, is only guaranteed to have a state property (it might have a displaystate property).

I realise I might have to have two controls and select between them by switching a visible property on or off but has anyone come up with a means to test whether the item is a group or not to provide a true or false value for the visible property?

At the moment I’m doing a very complex workaround with controls overwriting others in the same absolute position, which is a bit clunky.

Help gratefully received

Easiest way will be to add something to the name to indicate it’s a Group. That’s the only way I can think of to do this in a widget. Maybe have all Group Items start with our end with a “g”. Then you can test the name.

This sounds like you are using the itemsInGroup source for the repeater. In that case, the repeater actually returns the full API response which is not just a list of the item names but a full json description of that item, so, you already have the information you need.

Just like you get the name of the item from the loop variable using loop.variable.name you can get the item type using loop.variable.type

Here’s an example of a (now rather old) widget that does exactly what you are describing and selects which component to display based on the item type:

It is worth pointing out that visible isn’t the only way to do this, however. The widget component key now also accepts expressions so you can just do something like:

component: =(loop.variable.type == 'Group')?'widget:my_group_widget':'widget:my_standard_widget'

Thanks Justin

That did the trick. I didn’t think of that. I was focussed on the Item object only having a state property.

Actually rather than using type, I am testing members=="", because that gives me more specifically what I need, which is whether the group has any members or not rather than whether it is actually a Group (just in case I forget to remove the Group: from my item definition).

Your component tip is useful too but I can’t use it for reason discussed elsewhere - I can’t use my custom sub-widget because variables don’t propagate, so I am having to reproduce all my custom widget code in the parent widget.

Interestingly I am also using your very helpful tip on running a repeater twice, once to get the details of the array of members and then again to actually repeat the component. An interesting wrinkle is that I can’t get it to work on a nested list. The logic is as follows:

I have a single item list, being, say House; I run a repeater to give me an array of floors to populate an accordion list. I can use the information about that array to drive a 3-way toggle at the house level, which will respond to the floor members being ON, OFF or MIXED and will also control the floor members (i.e. if I switch the house level toggle ON, then all the floor members will also switch ON).

However, each Floor is also an accordion list of Rooms. The toggle at floor level needs to know about the members of each array of rooms. The problem is if I run an extra repeater outside this list, it will ignore any floor that doesn’t have any members because the repeater doesn’t run at all. OK floors and rooms are a bad example but they might be say rooms, groups of lights and individual lights. Within a room I might want to show ceiling lights and table lights in my first accordion. The ceiling light is not a group - there is only one - but table lights might be a group of several. I might want to control all the table lights at once or expand the accordion sub-list if I want to control them individually.

So, I’ve done it a different way. Now I can identify which items are groups and which are not, I can have an accordion sub-list for groups and a single item control for items.

Many thanks for your help on this.