cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Choose Language Hide Translation Bar
pauldeen
Level VI

Reordering a list box

I would like to be able to reorder a list in a list box, ideally with drag and drop (which seems impossible) or else with buttons. I'm looking for JSL for the up and down buttons.

 

The bigger goal is to get all files in a directory, split them into two lists based on file extensions. Display the two lists to the user, allow them to reorder the files so that pairs match up. Hit Ok to get the filename lists from the 2 list boxes and execute code on them in pairs. I just need help with the reordering of the lists in the list boxes.

1 ACCEPTED SOLUTION

Accepted Solutions
jthi
Super User

Re: Reordering a list box

Here is one very quick version for buttons (modified from Column Quick Swapper - easily change multiple Y and X-axis columns in Graph Builder )

Names Default To Here(1);

move_items = function({lb_ref, direction}, {Default Local},
	all_items = lb_ref << Get Items;
	sel_items = lb_ref << Get Selected;
	
	If(direction == "Swap",
		item_order = Transform Each({sel_item}, sel_items,
			Contains(all_items, sel_item)
		);
		new_order = all_items;
		new_order[item_order] = Reverse(sel_items);
	,
		start_idx = Contains(all_items, sel_items[1]); // set based first found item
		new_list = Filter Each({cur_item}, all_items, !Contains(sel_items, cur_item));
		
		new_idx = If(direction == "Up",
			start_idx - 1;
		, direction == "Down",
			start_idx + 1
		,
			start_idx
		);
		new_order = Insert(new_list, sel_items, new_idx);
	);
	
	lb_ref << Set Items(new_order);
	
	all_items = lb_ref << Get Items;
	For Each({cur_item}, sel_items,
		new_idx = Contains(all_items, cur_item);
		lb_ref << Set Selected(new_idx, 1, Run Script(0));
	);
);

nw = New Window("Example",
	H List Box(
		lb_ref = List Box(
			{"First Item", "Second Item", "Third Item"},
			width(200),
			max selected(2),
			nlines(6)
		),
		Lineup Box(N Col(1),
			btn_up = Button Box("", move_items(lb_ref, "Up"), << Set Icon("ListItemUp"), << Set Tip("Move selection up")),
			btn_down = Button Box("", move_items(lb_ref, "Down"), << Set Icon("ListItemDown"), << Set Tip("Move selection down")),
		)
	)
);

I think you can do drag and drop using Mouse Box, but I prefer buttons in JMP (much easier to create)

 

Edit: MouseBox ListBox Drag and Drop  has a bit more discussion, such as craige_hales saying that attempting to use Mouse Box wouldn't most likely be very robust solution and suggests using buttons.

-Jarmo

View solution in original post

1 REPLY 1
jthi
Super User

Re: Reordering a list box

Here is one very quick version for buttons (modified from Column Quick Swapper - easily change multiple Y and X-axis columns in Graph Builder )

Names Default To Here(1);

move_items = function({lb_ref, direction}, {Default Local},
	all_items = lb_ref << Get Items;
	sel_items = lb_ref << Get Selected;
	
	If(direction == "Swap",
		item_order = Transform Each({sel_item}, sel_items,
			Contains(all_items, sel_item)
		);
		new_order = all_items;
		new_order[item_order] = Reverse(sel_items);
	,
		start_idx = Contains(all_items, sel_items[1]); // set based first found item
		new_list = Filter Each({cur_item}, all_items, !Contains(sel_items, cur_item));
		
		new_idx = If(direction == "Up",
			start_idx - 1;
		, direction == "Down",
			start_idx + 1
		,
			start_idx
		);
		new_order = Insert(new_list, sel_items, new_idx);
	);
	
	lb_ref << Set Items(new_order);
	
	all_items = lb_ref << Get Items;
	For Each({cur_item}, sel_items,
		new_idx = Contains(all_items, cur_item);
		lb_ref << Set Selected(new_idx, 1, Run Script(0));
	);
);

nw = New Window("Example",
	H List Box(
		lb_ref = List Box(
			{"First Item", "Second Item", "Third Item"},
			width(200),
			max selected(2),
			nlines(6)
		),
		Lineup Box(N Col(1),
			btn_up = Button Box("", move_items(lb_ref, "Up"), << Set Icon("ListItemUp"), << Set Tip("Move selection up")),
			btn_down = Button Box("", move_items(lb_ref, "Down"), << Set Icon("ListItemDown"), << Set Tip("Move selection down")),
		)
	)
);

I think you can do drag and drop using Mouse Box, but I prefer buttons in JMP (much easier to create)

 

Edit: MouseBox ListBox Drag and Drop  has a bit more discussion, such as craige_hales saying that attempting to use Mouse Box wouldn't most likely be very robust solution and suggests using buttons.

-Jarmo