Skip to Content
0

Is it possible to store text objects in an array?

Feb 15, 2017 at 09:06 AM

72

avatar image

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?

10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

3 Answers

Best Answer
Mustafa Bensan Feb 16, 2017 at 05:51 AM
1

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.

Show 1 Share
10 |10000 characters needed characters left characters exceeded

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

1
Viktoria M Feb 15, 2017 at 10:09 AM
0

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

Show 1 Share
10 |10000 characters needed characters left characters exceeded

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); }
}); });
0
Nikolaus Weiss Feb 15, 2017 at 11:29 AM
0

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);
      }
   });
});
Share
10 |10000 characters needed characters left characters exceeded