Json Array and OH-Repeater

I am trying to cycle through a json array with oh-repeater and need a bit of help, probably on the syntax.

json string is stored in an item and looks like this:

[{"sName":"TestItem","sAlarm":"FIRE","sTime":"12.08. 16:45","isCritical":true},
{"sName":"TestItem","sAlarm":"OVERTEMP","sTime":"12.08. 16:46","isCritical":false}]

Widget code:

- component: f7-list
  config:
    inset: true
    mediaList: true
  slots:
    default:
      - component: oh-repeater
        config:
          for: rItem
          fragment: true
          sourceType: array
          in: =JSON.parse(@@vAlarme)
        slots:
          default:
            - component: f7-list-item
              config:
                badge: =loop.rItem.sAlarm
                badgeColor: red
                subtitle: =loop.rItem.sTime
                swipeout: true
                title: =loop.rItem.sName

The resulting list is empty. Browser inspect:

<ul>
  <!--fragment#e189b4f3d7#head-->
  <!--fragment#e189b4f3d7#tail-->
  <!--fragment#7980978def#head-->
  <!--fragment#7980978def#tail-->
  <!--fragment#fcd40f3113#head-->
  <!--fragment#fcd40f3113#tail-->
</ul>

(No matching selector or style)

That should work.

The only real issue I can see is with @@vAlarme. If vAlarme is the actual name of the item then that needs to be in quotes @@'vAlarme' otherwise the expression treats it as a variable that holds a string value representing some arbitrary item’s name.

1 Like

Arrrrrrgh!:man_facepalming:
Thanks Justin!

1 Like

We’ve all been there :wink: .

1 Like

I should have known it…

One follow up question please:
Within the widget I pass over a selected object (loop.rItem which contains {“sName”:“TestItem”,“sAlarm”:“FIRE”,“sTime”:“12.08. 16:45”,“isCritical”:true}) to a rule

action: rule
actionRule: AlarmDelete
actionRuleContext:
  deleteObject: =loop.rItem

which works fine.
Within this rule I try to find jsonArray’s index for the selected object, so that I can delete it from jsonArray.

var i = jsonArray.indexOf(deleteObject);
delete jsonArray[i];

However, i = -1 (not found) whatever other variations I have tried (for more than 2 hours).
Could you point me to the right direction?

I believe that object equivalence there is handled by reference, so the objects are not equal even though they have the same properties.

You can create you own function for object comparison and pass that to the findIndex() method of the array, but honestly, that’s a lot of work when your widget loop already knows the index of the object. Just pass a second variable to the rule:

action: rule
actionRule: AlarmDelete
actionRuleContext:
  deleteObject: =loop.rItem
  indexObject: =loop.rItem_idx

Clever.