UDButton not updated after Set-UDElement

Hello,

I try to update the content and in the future more attributes of an UDButton after click.
Is there any obvious I am doing wrong?
As far as I understood Sync-UDElement is not working with static content.

I would expect that it is working the same way as an text box: e.g. https://gist.github.com/adamdriscoll/f3eacce165e98a601586677855559732

$ButtonID = (New-Guid).Guid
New-UDButton -ID $ButtonID -Text "Click me ..." -OnClick (New-UDEndpoint -Endpoint {
        $ButtonID = $ArgumentList[0]

        Show-UDToast -Message "successfully clicked button - $ButtonID" -Duration 2000

        Set-UDElement -Id $ButtonID -Content { "You clicked the button!" }
        
        <#
        Set-UDElement -Id $ButtonID -Attributes @{
            text = "You clicked the button!"
        }
        #>

        #no need because its a static content
        #Sync-UDElement -Id $ButtonID
} -ArgumentList $ButtonID)

Hi @augustin.ziegler!

The $ButtonID variable won’t be avaliable to the endpoint “onclick”.
I believe what’s passed in $ArgumentList is the GUID of the onclick endpoint which should be “{GUID}onClick”
Try storing the button’s GUID in a $cache or $session variable and try again. Either that or set a static id on the button.

Should work :slight_smile:

@BoSen29

The variable $ButtonID is available and shown in the Show-UDToast message.

Do you have a working code snippet?

@augustin.ziegler
Apologies, i missed the -argumentlist part, and just asumed that it would read the default passed variables of the endpoint. :-/

Anywhomst: i can’t get it working, and in hindsight i remember seeing a github issue regarding the rework of buttons in 2.5 changed the behaviour with get/set-udelement commands.

Given @adam’s comments this should be fixed in a PR shortly afterwards, however the issue is still open.

I’ll give the source code a look later tonight and see if i can figure this one out :slight_smile:

1 Like

That would help me out a lot as in my opinion this is a basic functionality.

@augustin.ziegler
I see your point, haven’t actually used set-udelement on a button before myself.

What you can do meanwhile, is write the logic yourself in javascript with the “Invoke-UDJavaScript” command available in the nightly builds.

See:

It’s a quick (given that you’re proficient in javascript) and dirty workaround until we get this bug sorted :slight_smile:

Hit me up if you want to go down this road, and need some guidance with the javascript

@BoSen29

As I am not a web developer I really need some help with Invoke-UDJavaScript, can you provide me a code snipped to e.g. change the content (only need text) and the background color of a button via JS?

Sorry for asking but I cannot find any example online except the one you gave me: https://github.com/ironmansoftware/universal-dashboard/blob/master/src/UniversalDashboard/Help/Invoke-UDJavaScript.md

I am looking forward to the fix https://github.com/ironmansoftware/universal-dashboard/issues/978

Hi again my man @augustin.ziegler
No worries bruv.

Try with:

Invoke-UDJavaScript -Javascript “var btn = document.getElementById(’$ButtonID’); btn.textContent = ‘$NewBtnContent’; btn.style.backgroundColor = ‘$NewBtnBkGrColor’”

Works in the browser console atleast :sweat_smile:

@BoSen29

Thanks … I will try that one maybe tomorrow and now will start my weekend. Have a great weekend too …

@augustin.ziegler
Anytime bro!

Have a good one! :slight_smile:

1 Like

after installing the nightly build your code snipped is working fine, thanks for this workaround

$ButtonID = (New-Guid).Guid
    New-UDButton -ID $ButtonID -Text "Click me ..." -OnClick (New-UDEndpoint -Endpoint {
        $ButtonID = $ArgumentList[0]

        Show-UDToast -Message "successfully clicked button - $ButtonID" -Duration 2000

        Invoke-UDJavaScript -Javascript "
            var btn = document.getElementById('$ButtonID');
            btn.textContent = 'You clicked the button!';
            btn.style.backgroundColor = 'green'"
    } -ArgumentList $ButtonID)

Awesome my dude!

Anytime :wink:

Hi @augustin.ziegler
The PR is in the nightly builds tonight:

Should solve the issues!

1 Like

Great news :slight_smile: thanks

@BoSen29

I tried the nightly built and its not working for me.
Maybe I did something wrong, is it working for you?

   Set-UDElement -Id $ButtonID -Content { "You clicked the button!" } -Attributes @{
            backgroundColor = 'green'
        }

   Set-UDElement -Id $ButtonID  -Attributes @{
          text = 'You clicked the button! Attribute'            
          backgroundColor = 'green'
        }

Hi @augustin.ziegler,

Try:
Set-UDElement -Id $ButtonID -Attributes @{
backgroundColor = ‘green’
text = "You clicked the button!
}

Since the text of a button is part of it’s attributes, and not it’s content.

It might not be included in the nightly build, i honestly have no idea how @adam makes them avaliable.
You may need to build the module from github?

Thanks for the text hint, I will do it with the attribute.

As I installed the newest built the fix should be included:

Nightly build 20191202.2 - b4bff54b2b1bcb52b3963a9dd3b870c1f165d65e Fixes 978 (#1342)

Changes:

  • b4bff54b2b1bcb52b3963a9dd3b870c1f165d65e Fixes 978 (#1342)
  • 6b828cb638f2b552c97fc48a55389511adf9ae08 Fixes #658 (#1338)
  • 67fce2856cd300c6b875c5d26afa3f8209d9c15c Forgotten console.log (#1333)
  • 9bb10af84a707b053e12ddb8eb1df9934bc69ea0 Create ping-grid-part2.ps1 (#1328)
  • 1eae7c4f9e33b16191ae1ca1284aafa8e0210ac5 Fixes #1319 (#1325)
  • 0c0907aad0c2b01c1986f922a197a99d0149a080 Merge pull request #1326 from ironmansoftware/youtubedemoscripts
  • cd3597104b3bcdbabbde863f29f96e28dff79c5a Youtube Demo Example Scripts
  • 0b521a5e17abd1fce269fb1dddeb271c2c81cb3f Copy pasta of default theme with edits (#1321)
  • fac1cec99efc0b4d20ea812a8993790a07989745 Fixes #1313 (#1316) [ #1295 ]
  • 5987af020b4a85baa7b0b531b13022b0a78b9dd2 Add -totop param to select-udelement (#1315)

Awesome!

In that case, it should work with the text attribute :slight_smile:

Sorry for this miss understanding, I tried with the lastest build and the text attribute and its not working for me. Can you give it a try?
Maybe I am doing something wrong :open_mouth:

Hi again,
Ill check when im done in this meeting!

1 Like