I am looking for a way to indicate which of a couple buttons was last pressed so as an example of events.
Load dashboard on a phone.
Press a button, button becomes highlighted. Then press another button, first button de-highlights and the second is highlighted.
Load dashboard on PC.
Second button is highlighted.
I am thinking about possible ways to solve this. Maybe load button states from a CSV file and write to it with each press and changing an icon on each button. I would prefer if the button changed color and/or shadow was removed.
For changing state in my UDButtons I use Javascript to change the text as well as state/color. And depending on the task of the button I could have 2-3 of these state changes within a single onclick - ie ‘please wait’, success, fail.
Do you want to save the state for that individual browsing only? or all users?
I’d advise to look into using either the $session or $cache variables or saving to a sql table.
I use cache for somethings, but I regularly recycle my app pool for various reasons and so for things like this I write a 0 or 1 back to sql for these values, then load the button state based off that…
This is a switch not a button but the principle is the same:
$row = invoke-sqlcmd2 @SQLCON -Query "SELECT * FROM [tablename] WHERE _id=$rowid"
New-UDSwitch -id "IDgoeshere" -ontext "" -offtext "" -OnChange {
invoke-sqlcmd2 @SQLCON -Query "UPDATE [tablename] SET [columnname]='$($EventData)' WHERE _id=$rowid"
} -On:$(if($row.columnname){$true}else{$false})
if you only want the button state to stay for the session only, then you can create a $Session:var and store the button state in there. That var will live as long as the user session does.
So when your button state changes you could something like this
$Session:Button1 = [PSCustomObject]@{
id = 'button-id'
state = 'disabled'
color = 'red'
}
Then when you call the button state again (say on refresh) you can call that $Session:Button1 first to see if there is state for that button
if ($Session:Button1 |Where {$_.id -eq 'button-id'}) {
"Button Color is $(($Session:Button1 | Where {$_.id -eq 'button-id'}).Color)"
}
But what I would prefer is a state database (you mentioned CSV) but using a CSV will get complex and difficult to manage if you decide to add more functionality. What I recommend would be a database of sorts (Local SQL Express, NoSQL, etc…) this will allow you to store all sorts of data in different tables - maybe one for buttons, another for user-settings, etc…
For our UD we use Azure Web App to host the front-end, and Azure CosmosDB as the backend database. Also as a tip (personal opinion) is to use JSON as far as you can as your datasets. so when you POST back to your database from UD just |ConvertTo-Json for your data and store it in your table/database. PowerShell will automatically convert this once the response is received, and makes integration with other systems much easier.
@cn9ne Havent tried it, but I believe you could do this by placing the button inside an element, then using set-udelement to replace the content with the ‘new’ copy of the button that has your icon applied to it.