-
-
Notifications
You must be signed in to change notification settings - Fork 243
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
actionVariableKey not working properly for nested arrays #2467
Comments
As an addendum, I was playing around with workaround solutions until this is fixed. The issue is also there if the variable is created in one operation. For example: If actionVariableKey is left unused but actionVariable is set to ["anything",["Item 1.0","Item 1.1"]] then the item "anything" cannot be changed by a subsequent variable action. actionVariable is a very powerful solution to give much more flexibility and control in what the UI does, most importantly allowing the user to make various changes before committing these to actual live Items. This bug is making it seriously difficult to achieve what I want in my custom widgets and I am having to write a lot of unnecessary, slow and cumbersome code to try to get around it. |
I wouldn't really consider nested arrays as widget variables a currently supported feature. The The problem you are encountering is because when you create an array by specifying a element that is beyond the zero index:
javascript has no guidance on what value to use for any non-specified elements up to that defined element and so substitutes
However, - component: oh-button
config:
text: "Now initialize element 0 of testVar2 to an array"
action: variable
actionVariable: testVar2
actionVariableKey: ="0"
actionVariableValue: =[] Once you do that and the first element of your test array is now an empty array instead of There is actually a possibility that my recent PR #2533 will address this issue by allowing you to define the nested array variables in advance, but again that's not so much officially supported as a fortunate side-effect. The other way to address this (if your nested array is a fixed size) would be to not use actionVariable: testVar2
actionVariableValue:
- =((vars.testVar2 && vars.testVar2[0]) === undefined)?([]):(vars.testVar2[0])
- - =vars.testVar2[1][0]
- 'Item 1.1' |
Hi Justin I think that is very much the conclusion I came to. I can see that it is probably down to the way Javascript handles arrays under the hood but I thought I might be able to change that first element. As you'll see from my second post, it's not just a problem with the I do have a workaround. Given my use-case means doing this inside repeaters, I have the complete array available anyway so, as you suggest, I have discarded the actionVariableKey and, instead, every change I make recreates the entire array from scratch as a single actionVariable. I can toggle items by toggling the state of the array item that matches the loop index. Everything else just gets put back in the same state. Other controls that need to know which items are selected or not again act on the relevant array item. It requires a lot more code in the widget - arrow functions with map() methods and more - but it works well. I'll post it to the marketplace shortly. I have to say, as I learn, I'm finding that the new UI is very powerful. There are a few workarounds needed like variable defaults as discussed but your PR would help with that enormously - I'll look forward to that becoming part of Openhab. I'm finding using array variables in the UI very powerful though because I can then send a lot of UI input data back to my rules in one go or, alternatively, the user can cancel the operation without having actually changed any live item states. Thanks for your interest in this. |
The problem
actionVariableKey in oh-components is not working properly. In certain circumstances, when an array item has been inserted as item 1 (and as a result item 0 is null), item 0 cannot be changed. This seems to crash the action completely because if clearVariable is also set, the variable is not cleared.
This only happens in a nested array
Expected behavior
ActionVariableKey should be able to specify any object key or array position. If array item x is set by the action, then array items 0, 1... x-1 should be set to null. If array item 0 is then changed by a later action that array item should change without affecting the rest of the array. This works successfully for a simple array.
However, consider
actionVariable: testVar
actionVariableKey: ="[x].[y]"
If x is 0, the first item in the array is set and testVar has the value [["Item 0"]]. The second item in the level 1 array is as yet undefined. Perhaps we add item 2 of the second item using the actionVariableKey "[1].[1]" and we get [["Item 0.0"],[null,"Item 1.1"]]. At this point any of the array items can be changed at will.
However, if the second item in the parent array is set first, and is itself an array, the array becomes [null,["Item 1.0"]]. The first item is set to be
null
. From this point it is impossible to change the value of the first item in the parent array.Steps to reproduce
The following sample widget exhibits the described behaviours
Your environment
The text was updated successfully, but these errors were encountered: