Moving list item to index 0

Engaged ,
Jun 29, 2022 Jun 29, 2022

Copy link to clipboard

Copied

Hello everyone, how do I move the selected item to "index 0" in the list?

 

Capturar.PNG

var w = new Window ("dialog", "Rearrange");
var list = w.add ("listbox", undefined, ["one", "two", "three", "four", "five"]);
list.selection = 1;
var up = w.add ("button", undefined, "Index 0");

up.onClick = function () {
 var n = list.selection.index;
 if (n > 0) {
 swap (list.items [n-1], list.items [n]);
 list.selection = n-1;
 }
}

function swap (x, y) {
 var temp = x.text;
 x.text = y.text;
 y.text = temp;
}
w.show ();

 

TOPICS
Actions and scripting

Views

53

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Adobe Community Professional , Jun 29, 2022 Jun 29, 2022
var w = new Window ("dialog", "Rearrange");
var list = w.add ("listbox", undefined, ["one", "two", "three", "four", "five"]);
list.selection = 1;
var up = w.add ("button", undefined, "Index 0");

up.onClick = function () {
 var n = list.selection.index;
 if (n > 0) {
 swap (list.items [0], list.items [n]);
 list.selection = 0;
 }
}

function swap (x, y) {
 var temp = x.text;
 x.text = y.text;
 y.text = temp;
}
w.show ();

Likes

Translate

Translate
Adobe Community Professional ,
Jun 29, 2022 Jun 29, 2022

Copy link to clipboard

Copied

var w = new Window ("dialog", "Rearrange");
var list = w.add ("listbox", undefined, ["one", "two", "three", "four", "five"]);
list.selection = 1;
var up = w.add ("button", undefined, "Index 0");

up.onClick = function () {
 var n = list.selection.index;
 if (n > 0) {
 swap (list.items [0], list.items [n]);
 list.selection = 0;
 }
}

function swap (x, y) {
 var temp = x.text;
 x.text = y.text;
 y.text = temp;
}
w.show ();

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Jun 30, 2022 Jun 30, 2022

Copy link to clipboard

Copied

Perfect! Thanks for solving my problem. @jazz-y  taking advantage of this topic here, I took the opportunity to ask you, how would I move any item to the final index of the list?.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Jun 30, 2022 Jun 30, 2022

Copy link to clipboard

Copied

var w = new Window ("dialog", "Rearrange");
var list = w.add ("listbox", undefined, ["one", "two", "three", "four", "five"]);
list.selection = 1;
var up = w.add ("button", undefined, "Index 0");

up.onClick = function () {
 var n = list.selection.index;
 if (n >= 0) {
 swap (list.items [n], list.items [list.items.length-1]);
 list.selection = list.items.length-1;
 }
}

function swap (x, y) {
 var temp = x.text;
 x.text = y.text;
 y.text = temp;
}
w.show ();

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Jun 30, 2022 Jun 30, 2022

Copy link to clipboard

Copied

LATEST

@jazz-y thanks again brother

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines