cancel
Showing results for 
Search instead for 
Did you mean: 

Is it possible to store text objects in an array?

Former Member
0 Kudos

I have several Text objects in my Design Studio application, named CHART_LABEL_0, CHART_LABEL_1, etc.

I'm trying to store these component objects in an array so I can later use setText on them in a for loop.

Storing them in the array seems to work without errors, but the setText function does not.

When I run the code below, nothing happens. I get the following error: "No viable alternative at input "setText"

var members = DATASOURCE.getMembers("DIMENSION", 8);
var textBoxes = [CHART_LABEL_0, CHART_LABEL_1, CHART_LABEL_2, CHART_LABEL_3, CHART_LABEL_4, CHART_LABEL_5, CHART_LABEL_6, CHART_LABEL_7];
members.forEach(function(member, index) {
	textBoxes[index].setText(member.text);
});

Am I doing something wrong here, or is it not possible to store component objects in an array like this? Is there some other way I could dynamically assign the names of a dimension's members as values in text components?

Accepted Solutions (1)

Accepted Solutions (1)

MustafaBensan
Active Contributor

Hi Nikolaus,

I have encountered this issue before and it appears to be related to array indexes not being supported in compound script statements. You don't need to add a second for loop. Instead, you can tweak your original code as follows:

var members= DATASOURCE.getMembers("DIMENSION", 8);
var textBoxes =[CHART_LABEL_0, CHART_LABEL_1, CHART_LABEL_2, CHART_LABEL_3, CHART_LABEL_4, CHART_LABEL_5, CHART_LABEL_6, CHART_LABEL_7];

members.forEach(function(member,index){

        var myTextBox = textBoxes[index];  // Reference text component array item by index in a variable
	myTextBox.setText(member.text);    // Apply setText() to text box variable

});

Regards,

Mustafa.

Former Member

Thanks! This code is definitely cleaner than using two loops.

Answers (2)

Answers (2)

Former Member
0 Kudos

Solved it myself by using the following code. You have to loop over the second array, within the loop of the first array, and check when indeces are matched.

var array1 = [items];
var array2 = [TEXT_1,TEXT_2,TEXT_3];
array1.forEach(function(member, index) {
   array2.forEach(function(member2, index2) {
      if (index == index2) {
         member2.setText(member.text);
      }
   });
});
Former Member
0 Kudos

Hello Nikolaus,

you can do this. The only problem of your coding is the access on the array.

For example:

var array = [TEXT_1, TEXT_2, TEXT_3];

array.forEach(function(element, index) {

element.setText(Convert.floatToString(index));

});

The only thing here is that you need to convert int to string if you do not want to have 1.0, 2.0 ... I only saw the possibility to convert float to String.

Hope it helps.

Best regards,

Viktoria

Former Member
0 Kudos

Hi,

This didn't quite solve my issue, as I wasn't trying to set the index itself as the text value. While looping over array2, I was trying to setText on members of array1, using the indeces and members of array2. I managed to solve it like this:

var array1 = [items];
var array2 = [TEXT_1,TEXT_2,TEXT_3];
array1.forEach(function(member, index) {
   array2.forEach(function(member2, index2) {
if (index == index2) { member2.setText(member.text); }
}); });