cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Visit the PTCooler (the community lounge) to get to know your fellow community members and check out some of Dale's Friday Humor posts! X

Assigning values to a dynamically created infotable

khayes1
13-Aquamarine

Assigning values to a dynamically created infotable

Hi, hoping someone can help with with an issue I'm struggling with? I'm tying to create a dynamic monthly sales table. something like the one shown below.

 

Product Feb  March April May June July
AX 500 100 80 120 80 100 50
PQ 500 50 60 65 65 80 80
AX250 100 87 60 70 60 75

 

I am using a dynamic infotable as I allow the user to select the number of months they wish to display, so there could be 1 month or 20 months. I am able to dynamically create the infotable to return from my service by creating an empty infotable and then adding the required fields (months) using the code shown below:

var params = { infoTableName: "tmpTable" /* STRING */};
var myTable = Resources["InfoTableFunctions"].CreateInfoTable(params); // myTable: INFOTABLE

var newField = new Object();
newField.name = "Product"
newField.baseType = 'STRING';
myTable.AddField(newField);

 

newField.name = "Feb"
newField.baseType = 'INTEGER"
myTable.AddField(newField);

 

newField.name = "March"
newField.baseType = 'INTEGER';
myTable.AddField(newField);

and so on.

My problem arises when I want to add a row for each of the products. Normally with a pre-defined datashape  I could use something likethe below to assign values to each column

var newEntry = new Object();
newEntry.Product = "AX500" // STRING
newEntry.Feb = 100 ; //INTEGER
newEntry.March = 60 ; //INTEGER
myTable.AddRow(newEntry);

 

How can I do this with a dynamic field which I have no prior knowledge of?

Can I index the fields as an array in some way?

 

Thanks

K

 

3 REPLIES 3

I think you can still use the 

newEntry.Feb = 100 ;

definition for undefined datashapes, as long as the shape contains all the fields whose value just been assigned, and the rest of the fields will be left blank (so don't leave primary Key to the un-defined properties in your shape)

khayes1
13-Aquamarine
(To:zyuan1)

Hi, thanks for the quick reply, but still not sure how that would work.

As I mentioned, I want to allow the user to select which months they want to see. So if for example they choose a start date of 01-03-2018 (1st March 2018) and an end date of 01-08-2018 (1st august 2018) I can use a script to extract the months required & iterate through the months adding a column for each one using the code

newField.name = "Mar";
newField.baseType = 'INTEGER';
myTable.AddField(newField);

 

newField.name = "Apr";
newField.baseType = 'INTEGER';
myTable.AddField(newField);

 

but when it comes to assigning values in a row I don't know in advance what the column names are & I presumably I can't use newEntry."Mar" = 100 ;

 

Is there some way I can add a value using an index into the 'fieldDefinitions' of the dynamic infotable?

 

If the un-assigned datashape contains the Datetime field and you know the property name, you can assign the value by newEntry."Mar" = 100 ;

(You may not know what the whole datashape look like, but you should at least know some important properties)

 

If you don't know the incoming infotable, you can use the snippet addfield to change it.

var newField = new Object();
newField.name = yourFieldName;
newField.baseType = 'BASETYPENAME';
yourInfoTable.AddField(newField);

Top Tags