<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Creating button script in a for loop in Discussions</title>
    <link>https://community.jmp.com/t5/Discussions/Creating-button-script-in-a-for-loop/m-p/601241#M80457</link>
    <description>&lt;P&gt;Here is my old school way of handling this&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default To Here( 1 );
UserText = {"default 1", "default 2", "default 3"};

win2 = New Window( "Select files", 
	tb = Table Box( 
		Col Box( "Text", {} ), 
		Col Box( "Button", {} ), 
		), 
	tb &amp;lt;&amp;lt; set row borders(1);
);


//for loop to add a number of rows with a small button next to each one
For( i = 1, i &amp;lt;= N Items( UserText ), i++,
	tb &amp;lt;&amp;lt; add row(
		{Text Box( UserText[i], &amp;lt;&amp;lt;set width( 150 ) ), 
		Eval( parse("Check Box( \!"\!",
			 Print( \!"you pressed button " || Char( i ) || "\!"))") ) 
		}
	);
);&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 15 Feb 2023 04:52:52 GMT</pubDate>
    <dc:creator>txnelson</dc:creator>
    <dc:date>2023-02-15T04:52:52Z</dc:date>
    <item>
      <title>Creating button script in a for loop</title>
      <link>https://community.jmp.com/t5/Discussions/Creating-button-script-in-a-for-loop/m-p/601185#M80453</link>
      <description>&lt;P&gt;I'm trying to generate a table box with a number of row which will be variable. I want each row to have button which will do some function to a corresponding variable in a list.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Current issue: The scripts in the for loop aren't interpreting eval expr as I expect. The resulting script attached to each checkbox should print that button 1, 2, or 3 was pressed. Instead it always says 4. End goal is to do an operation on UserText[i] when the checkbox is clicked.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can anyone help explain what I am doing wrong? Thank you!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Bonus question: I'm using check boxes because they are smaller than buttons boxes and button boxes don't seem to be resizable. Ideally they would just be little clickable icons. Is there a friendly way to make a small clickable "x" instead of a full button?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;JMP version: 16&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default To Here( 1 );

UserText = {"default 1", "default 2", "default 3"};

win2 = New Window( "Select files", 
	tb = Table Box( 
		Col Box( "Text", {} ), 
		Col Box( "Button", {} ), 
		), 
	tb &amp;lt;&amp;lt; set row borders(1);
);


//for loop to add a number of rows with a small button next to each one
For( i = 1, i &amp;lt;= N Items( UserText ), i++,
	tb &amp;lt;&amp;lt; add row(
		{Text Box( UserText[i], &amp;lt;&amp;lt;set width( 150 ) ), 
		Check Box( "",
			Eval( Eval Expr( Print( "you pressed button " || Char( Expr( i ) ) ) ) )
		)}
	)
);

//this was my first thought, but this creates a script that contains "i" rather than the value of i
/*
For( i = 1, i &amp;lt;= 5, i++,
	tb &amp;lt;&amp;lt; add row(
		{Text Box( UserText[i], &amp;lt;&amp;lt;set width( 300 ) ), 
		Check Box( "", 
			Print( "you pressed button " || Char( i ) ) 
		)}
	)
);
*/&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 08 Jun 2023 16:35:59 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Creating-button-script-in-a-for-loop/m-p/601185#M80453</guid>
      <dc:creator>CitizenNo3</dc:creator>
      <dc:date>2023-06-08T16:35:59Z</dc:date>
    </item>
    <item>
      <title>Re: Creating button script in a for loop</title>
      <link>https://community.jmp.com/t5/Discussions/Creating-button-script-in-a-for-loop/m-p/601240#M80456</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You were very close.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you use Eval ( EvalExpr ( ) ) to wrap the entire tb &amp;lt;&amp;lt; add row () statement as below, you will get the behavior you're after:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;//for loop to add a number of rows with a small button next to each one
For( i = 1, i &amp;lt;= N Items( UserText ), i++,
	eval(evalexpr(
		tb &amp;lt;&amp;lt; add row(
			{Text Box( UserText[i], &amp;lt;&amp;lt;set width( 150 ) ), 
			Check Box( "",
				Print( "you pressed button " || Char( Expr( i ) ) )
			)}
		)
	))
);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 15 Feb 2023 04:38:10 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Creating-button-script-in-a-for-loop/m-p/601240#M80456</guid>
      <dc:creator>brady_brady</dc:creator>
      <dc:date>2023-02-15T04:38:10Z</dc:date>
    </item>
    <item>
      <title>Re: Creating button script in a for loop</title>
      <link>https://community.jmp.com/t5/Discussions/Creating-button-script-in-a-for-loop/m-p/601241#M80457</link>
      <description>&lt;P&gt;Here is my old school way of handling this&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default To Here( 1 );
UserText = {"default 1", "default 2", "default 3"};

win2 = New Window( "Select files", 
	tb = Table Box( 
		Col Box( "Text", {} ), 
		Col Box( "Button", {} ), 
		), 
	tb &amp;lt;&amp;lt; set row borders(1);
);


//for loop to add a number of rows with a small button next to each one
For( i = 1, i &amp;lt;= N Items( UserText ), i++,
	tb &amp;lt;&amp;lt; add row(
		{Text Box( UserText[i], &amp;lt;&amp;lt;set width( 150 ) ), 
		Eval( parse("Check Box( \!"\!",
			 Print( \!"you pressed button " || Char( i ) || "\!"))") ) 
		}
	);
);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 15 Feb 2023 04:52:52 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Creating-button-script-in-a-for-loop/m-p/601241#M80457</guid>
      <dc:creator>txnelson</dc:creator>
      <dc:date>2023-02-15T04:52:52Z</dc:date>
    </item>
    <item>
      <title>Re: Creating button script in a for loop</title>
      <link>https://community.jmp.com/t5/Discussions/Creating-button-script-in-a-for-loop/m-p/601275#M80460</link>
      <description>&lt;P&gt;My method is a bit more complicated using XPath and Contains(), but in some cases it might be more robust (and you can extend it to other elements beside Checkbox if you change the XPath)&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default To Here(1);

UserText = {"default 1", "default 2", "default 3"};

win2 = New Window("Select files",
	tb = Table Box(Col Box("Text", {}), Col Box("Button", {}), Col Box("Third", {})),
	tb &amp;lt;&amp;lt; set row borders(1)
);

expr_tablebox_get_checkbox_idx = Expr(
	row_idx = Contains((this &amp;lt;&amp;lt; parent) &amp;lt;&amp;lt; XPath("//CheckBoxBox"), this);
);

expr_tablebox_get_column_idx = Expr(
	col_idx = Contains(((this &amp;lt;&amp;lt; parent) &amp;lt;&amp;lt; parent) &amp;lt;&amp;lt; Get Names, (this &amp;lt;&amp;lt; parent) &amp;lt;&amp;lt; get heading)
);

expr_check_box = Expr(
	expr_tablebox_get_checkbox_idx;
	expr_tablebox_get_column_idx;
	Show(row_idx, col_idx);
);

For Each({txt}, UserText,
	tb &amp;lt;&amp;lt; Add Row(
		{Text Box(txt, &amp;lt;&amp;lt; Set Width),
			cb = Check Box({""}, &amp;lt;&amp;lt; Set Function(function({this},
					expr_check_box
				))
			),
			cb = Check Box({""}, &amp;lt;&amp;lt; Set Function(function({this},
					expr_check_box
				))
			)
		};
	);
);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;CODE class=" language-jsl"&gt;&lt;/CODE&gt;Using this method you might run into some issues where expressions evaluated correctly depending where you call them, so it shouldn't be the first method to try&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 15 Feb 2023 07:16:46 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Creating-button-script-in-a-for-loop/m-p/601275#M80460</guid>
      <dc:creator>jthi</dc:creator>
      <dc:date>2023-02-15T07:16:46Z</dc:date>
    </item>
    <item>
      <title>Re: Creating button script in a for loop</title>
      <link>https://community.jmp.com/t5/Discussions/Creating-button-script-in-a-for-loop/m-p/601493#M80486</link>
      <description>&lt;P&gt;I came back as last night I was thinking, "this can also be done with Xpath and contains". Jarmo's post was already here; the example I've put below is quite similar, but assigns all of the checkbox functions at once with an Xpath statement, once you've built the table. This is becoming one of my favorite ways to handle this type of thing.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default To Here( 1 );

UserText = {"default 1", "default 2", "default 3"};

win2 = New Window( "Select files", 
	tb = Table Box( Col Box( "Text", {} ), Col Box( "Button", {} ), &amp;lt;&amp;lt; set row borders(1) );
);


//add rows with a small button next to each one
For each ( { txt, i }, UserText,
		tb &amp;lt;&amp;lt; add row( {Text Box( txt, &amp;lt;&amp;lt;set width( 150 ) ),  Check Box( "" ) } )
);

// add functions to the checkboxes
(tb &amp;lt;&amp;lt; xpath( "//CheckBoxBox") ) &amp;lt;&amp;lt; set function(
	function ( {this},
		boxList = (this &amp;lt;&amp;lt; parent) &amp;lt;&amp;lt; Xpath("//CheckBoxBox");
		print (evalinsert ("You pressed box ^contains(boxList, this)^"))
	);
);
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 15 Feb 2023 16:48:17 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Creating-button-script-in-a-for-loop/m-p/601493#M80486</guid>
      <dc:creator>brady_brady</dc:creator>
      <dc:date>2023-02-15T16:48:17Z</dc:date>
    </item>
    <item>
      <title>Re: Creating button script in a for loop</title>
      <link>https://community.jmp.com/t5/Discussions/Creating-button-script-in-a-for-loop/m-p/601613#M80500</link>
      <description>&lt;P&gt;Thank you! I'll need to spend a bit more time understanding this one, I haven't used set function and I'm not quite understanding the behavior after a quick reading of the scripting guide and some examples. I do foresee a situation down the line where I'll need functionality like this so I'll put this in back of mind for a bit and return if that time comes.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;To confirm something basic that doesn't seem to be explicitly stated in the material: when defining a function, is 'this' a pre-defined term in JSL that will point to whatever object the function is attached to? I see some examples that use 'this' in the set function argument without is appearing anywhere else in the function definition (I'm about 60% sure I'm using the correct terms). The basic mathematical functions are intuitive such as MyFn = function ({x,y}, x + y ), but then things seem to ramp up in complexity very quickly when we get into objects.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 15 Feb 2023 20:35:31 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Creating-button-script-in-a-for-loop/m-p/601613#M80500</guid>
      <dc:creator>CitizenNo3</dc:creator>
      <dc:date>2023-02-15T20:35:31Z</dc:date>
    </item>
    <item>
      <title>Re: Creating button script in a for loop</title>
      <link>https://community.jmp.com/t5/Discussions/Creating-button-script-in-a-for-loop/m-p/601810#M80521</link>
      <description>&lt;P&gt;It is just the first parameter when used with &amp;lt;&amp;lt; Set Function (as in it doesn't have to be &lt;EM&gt;this&lt;/EM&gt;, seems like JMP17 scripting index is using &lt;EM&gt;thisBox&lt;/EM&gt; for example). And that first argument is a reference to that specific box in which that function is set to.&amp;nbsp; This help page might provide some more information with some examples:&lt;/P&gt;
&lt;P&gt;&lt;A href="https://www.jmp.com/support/help/en/17.0/#page/jmp/set-function-and-set-script.shtml" target="_self"&gt;Scripting Guide &amp;gt; Display Trees &amp;gt; Construct Custom Windows &amp;gt; Set Function and Set Script&lt;/A&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also, with some display boxes you might be able to use multiple parameters with &amp;lt;&amp;lt; Set Function . You can see these in scripting index (if you can find them, might be a bit tricky sometimes)&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="jthi_0-1676528096135.png" style="width: 400px;"&gt;&lt;img src="https://community.jmp.com/t5/image/serverpage/image-id/50208i95E3A45AD12FDD44/image-size/medium?v=v2&amp;amp;px=400" role="button" title="jthi_0-1676528096135.png" alt="jthi_0-1676528096135.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 16 Feb 2023 06:16:11 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Creating-button-script-in-a-for-loop/m-p/601810#M80521</guid>
      <dc:creator>jthi</dc:creator>
      <dc:date>2023-02-16T06:16:11Z</dc:date>
    </item>
  </channel>
</rss>

