Group Abstract Group Abstract

Message Boards Message Boards

1
|
7.8K Views
|
4 Replies
|
6 Total Likes
View groups...
Share
Share this post:

[Solved] A Button to affect a value in a list?

Posted 5 years ago
POSTED BY: Andrew Burnett
4 Replies

May not be the most elegant method but you can have the button know its position by generating it with a fixed input, the original order.

The order list is then used to sort the list. Up means changing its current order value with the one above. moving down changing it with the order values below.

len = 10;
order = Range[len];

MoveUp[n_] := With[{i = Position[order, n][[1, 1]]},
   If[i =!= 1, order[[i - 1 ;; i]] = Reverse[order[[i - 1 ;; i]]]]
   ];
MoveDown[n_] := With[{i = Position[order, n][[1, 1]]},
   If[i =!= Length[order], 
    order[[i ;; i + 1]] = Reverse[order[[i ;; i + 1]]]]
   ];
list = {#, Button["Up", MoveUp[#]], Button["Down", MoveDown[#]]} & /@ 
   order;

Dynamic[Grid[list[[order]]]]
POSTED BY: Martijn Froeling
Posted 5 years ago

Andrew:

I do not have time to validate my idea, but I think you can do what you want by identifying the "self" through the action parameter to Button[].

The documentation gives an example of using Print[10!] as the value for action. You should be call different functions, or the same function with different parameters that identify the song.

If you want to programmatically generate the Buttons, then you call use ToExpression[] with the action. ToExpression[] takes a string (that you can generate with the song title).

Something like:

doIt[song_String] := ToExpression[Print[song]];
Button["do it", ToExpression["Print[\"title\"]"]]

Good luck, and have a great week.

POSTED BY: Mike Besso

I was looking for a simple way to manually reorder images - this is exactly what I needed! Many thanks for sharing!

POSTED BY: Henrik Schachner
Posted 5 years ago

Brilliant, thank you very much to both of you. This gives me two interesting starting points.

POSTED BY: Andrew Burnett
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard