Message Boards Message Boards

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

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

Posted 3 years ago

I am trying to build the following interface

I have a list of songs. Each song is a list comprising a rating for the melody and a rating for the solo.

The interface should present the list of song titles along with an upvote and downvote button next to each title.

My initial thought was to include a Button[] as an element in each list. However, I can't find a way for the Button to 'know' which list it is a member of. In OO based languages there would be the concept of Self, ie. the object that the Button is a member of. Is there something like that in Mathematica. Or, is this completely the wrong way to build the interface?

Cheers. Andy

POSTED BY: Andrew Burnett
4 Replies
Posted 3 years ago

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

POSTED BY: Andrew Burnett

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

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 3 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
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract