Re: Can nobody help me? Re: selecting a column in a grid
- To: mathgroup at smc.vnet.net
- Subject: [mg98318] Re: Can nobody help me? Re: selecting a column in a grid
- From: Albert Retey <awnl at gmx-topmail.de>
- Date: Sun, 5 Apr 2009 06:41:31 -0400 (EDT)
- References: <gr6c5c$ppo$1@smc.vnet.net>
Hi, > Currently I have EventHandler wrapped around the whole grid. Is it possible > to wrap it around each individual cell in a grid? I have tried a few ideas, > but they don't seem to work. The documentation suggests that EventHandler > can be wrapped around any expression, but that doesn't seem to be the case, > or at least the wrapped expression doesn't seem to display properly inside > other constructs, such as Grid, Column etc. I think using EventHandler for the content of individual cells will work much better and it works as documented, see below. The problem is that a cell in a grid is not something that exists as an individual expression (Item would actually be, but this indeed doesn't work, it looks like Grid doesn't recognize the Item as such if it is wrapped with EventHandler). > Is there some better approach I could take, rather than using EventHandler? Would this be close to what you want? Admittedly it is a little hacky, since one needs to play with the ImageMargins and Spacings options to get a result that has no blind spots for the mouse clicks and looks decent. The possibility to wrap Item in Grid with EventHandler would be nicer, or an option for Item that would allow the handling of events... Deploy[Grid[ MapIndexed[ Item[ EventHandler[ Pane[#1, ImageMargins -> {3*{1, 1}, {0, 0}}], {"MouseClicked" :> (selectedcolumn = #2[[2]])}], Background -> Dynamic[If[selectedcolumn == #2[[2]], LightGray, White]] ] &, {{aasdf, b, c}, {dasdf, e, f}, {gasdf, h, i}}, {2} ], Dividers -> All, Spacings -> 0.15 ]] note that I needed to set a small value for Spacings, otherwise when clicking in the Spacing-area, the events won't be triggered. Using Spacings->0 will make the Dividers become invisible which may or may not be a problem for you. To get back the normal look of the table with decent margins for the contents of the cells I am using a Pane with individual ImageMargins options, which you might adopt to your taste. I was choosing something that is close to the default spacings Grid will use with no options. Without using Deploy it can happen that instead of triggering the MouseClick Event you end up selecting some content of the cell without changing the column selection, so I think usually using Deploy will be a good choice for an interactive graphical user interface. Changing the background color for the selected column is just to demonstrate that it works... Here is the same thing for selecting cells: Deploy[Grid[ MapIndexed[ Item[ EventHandler[ Pane[#1, ImageMargins -> {3*{1, 1}, {0, 0}}], {"MouseClicked" :> (selectedcell = #2)}], Background -> Dynamic[Switch[selectedcell, #2, LightRed,{#2[[1]], _}, LightBlue, {_, #2[[2]]}, LightGreen,_, White]] ] &, {{aasdf, b, c}, {dasdf, e, f}, {gasdf, h, i}}, {2} ], Dividers -> All, Spacings -> 0.15 ]] hth, albert
- Follow-Ups:
- Re: Re: Can nobody help me? Re: selecting a column in a grid
- From: "Paul Ellsmore" <paul.ellsmore@nanion.co.uk>
- Re: Re: Can nobody help me? Re: selecting a column in a grid
- From: "Paul Ellsmore" <paul.ellsmore@nanion.co.uk>
- Re: Re: Can nobody help me? Re: selecting a column in a grid