2
$\begingroup$

This question is related to the following

Interactively select a row/column of a Grid

I’d like to present information in a grid where in the user can choose from the entries in the table. As shown in the figure:

Select rows in a table

Here’s some sample code:

tableHeaders = {"A", "B", "C", "D"};
data = Table[10 i + j, {i, 3}, {j, 3}];
checkBoxes = Table[Checkbox[True], {i, 3}];
table = Transpose@Insert[Transpose@data, checkBoxes, 1];


Grid[Insert[table, tableHeaders, 1]
 , Alignment -> {Left, Baseline}
 , Spacings -> {1.4, 1.3}
 , FrameStyle -> LightGray
 , Frame -> True
 , Background -> {None, {Lighter@Lighter@LightBlue, None}}
 , Dividers -> {{True, True}, {True, True}}
]

I tried to get define variables table/checkBoxes as a Dynamic but Mathematica throws up errors.

Question: How do I modify the code so that I can get a list of rows that are checked in the grid.

$\endgroup$

3 Answers 3

1
$\begingroup$

Instead of using 3 variables x,y,z the code below uses a unique variable choices which is a List

tableHeaders = {"A", "B", "C", "D"};
data = Table[10 i + j, {i, 3}, {j, 3}];

choices={};
checkBoxes = Table[myCheckbox[choices,i], {i, 3}];
table = Transpose@Insert[Transpose@data, checkBoxes, 1];


Grid[Insert[table, tableHeaders, 1]
 , Alignment -> {Left, Baseline}
 , Spacings -> {1.4, 1.3}
 , FrameStyle -> LightGray
 , Frame -> True
 , Background -> {None, {Lighter@Lighter@LightBlue, None}}
 , Dividers -> {{True, True}, {True, True}}
]

Dynamic[choices]

enter image description here

Note : One can find on Mathematica Stack Exchange three other answers where I have used myCheckbox.

$\endgroup$
0
3
$\begingroup$

I was able to find a simple solution… turns out I was using Dynamic in the wrong context… This code works

tableHeaders = {"A", "B", "C", "D"};
data = Table[10 i + j, {i, 3}, {j, 3}];
checkBoxes = Checkbox /@ {Dynamic[x], Dynamic[y], Dynamic[z]}


table = Insert [Transpose@Insert[Transpose@data, checkBoxes, 1], 
tableHeaders, 1];

grid = Dynamic@Grid[table
 , Alignment -> {Left, Baseline}
 , Spacings -> {1.4, 1.3}
 , FrameStyle -> LightGray
 , Frame -> True
 , Background -> {None, {Lighter@Lighter@LightBlue, None}}
 , Dividers -> {{True, True}, {True, True}}
 ]
$\endgroup$
1
$\begingroup$

If you want to use an array to indicate the selected rows, you can make these changes:

selected = ConstantArray[False, Length@data];
checkBoxes = Table[With[{i = i}, Checkbox@Dynamic@selected[[i]]], {i, 3}];

Then to get the currently selected rows, execute the following:

Pick[data, selected]
(*  {{11, 12, 13}, {31, 32, 33}}  *)
$\endgroup$
1
  • $\begingroup$ Thank you. I picked Andre’s answer due to the elegance of his myCheckBox function which maintains the order in which checkboxes were checked and unchecked. $\endgroup$
    – Pam
    Aug 10, 2018 at 12:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.