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

Community Tip - Learn all about PTC Community Badges. Engage with PTC and see how many you can earn! X

Dynamic columns from Infotable

TanmeyTWX
17-Peridot

Dynamic columns from Infotable

Hi,

I'm getting different Infotables with different count of columns. Say, table1 with 10 columns and table with 20 columns and these columns(names) are not fixed,may change frequently.

Now I want to show all the columns from all the infotable onto a single GRID.

In short,I don't have any predefined DataShape to bind.

How to achieve that?

I have tried below approach:

****************************************************

// result: INFOTABLE dataShape: RootEntityList  //Fetching Thing Names on the basis of ThingShape Name

var thingNames = ThingShapes[ThingShapeName].GetImplementingThings();


//Creating Dynamic Infotable with no Datashape

var params = {

infoTableName: "MyTable" /* STRING */

};

//result: INFOTABLE

var result = Resources["InfoTableFunctions"].CreateInfoTable(params);

//Looping to get thingName and get its Properties and adding its row in Dynamic Infotable,created above

var tableLength = thingNames.rows.length;

for (var y = 0; y < tableLength; y++)

{

var Tname = thingNames.rows.name;

    // result: INFOTABLE 

var propertyValue = Things[Tname].GetPropertyValues();

   result.AddRow(propertyValue.rows[0]);

}

****************************************************


Output: Infotable without DataShape

Mashup Bind: Grid bind to ALL DATA with "Show All Columns" option to "true".




1 ACCEPTED SOLUTION

Accepted Solutions

Hi Tarun,

Just add a ""+ in front of iLF[key ].baseType :

var iLF ;// 

if ((data.dataShape==null)||(data.dataShape==undefined)) { 

   iLF = data.ToJSON().dataShape.fieldDefinitions; 

} else { 

   iLF = data.dataShape.fields; 

for (var key in iLF) { 

   result.AddField({ name: key, baseType: (""+iLF[key].baseType) }); 

}

View solution in original post

11 REPLIES 11
AnnaAn
13-Aquamarine
(To:TanmeyTWX)

Tarun Nayyar​,

Did it work with your approach? Did you have any error during running your code(please attach it if any)? Or it worked for you and you are still looking for

any better approach from the community?

Thanks,

Br,

Anna

Hi Anna,

I think he was doing a code sample, not asking.

Carles.

Hi Ann/Carles,

My approach didn't work for me. It neither throw any error nor any data display onto grid. I'm getting Grid with blank rows as below.

You must use AddField snippet on the result previous adding data.

Carles,

Sorry, didn't get you.

After:

var result = Resources["InfoTableFunctions"].CreateInfoTable(params);


And previous inserting data, you must AddFields on the result:


result.AddField({ name: "yourDynamicFieldName1", baseType: "BASE_TYPE_FOR_THE_DINAMIC_FIELD1" });

result.AddField({ name: "yourDynamicFieldName2", baseType: "BASE_TYPE_FOR_THE_DINAMIC_FIELD2" });

...


But that's the Information I don't have.I don't know what would be the fieldName and baseType of new(extra) columns.

Its like, I'm getting table1 with two columns; Temperature and Pressure and not aware if Table2 has what type of columns.

But GetPropertyValues returns an Infotable with Fields, then you can use this information to add fields ( the ones you didn't added before ).

How to get Fields from an Infotable... Here you have (data it's your GerPropertyValues result ):

var iLF ;//

if ((data.dataShape==null)||(data.dataShape==undefined)) {

   iLF = data.ToJSON().dataShape.fieldDefinitions;

} else {

   iLF = data.dataShape.fields;

}

for (var key in iLF) {

   result.AddField({ name: key, baseType: iLF[key].baseType });

}

Getting below error.

Hi Tarun,

Just add a ""+ in front of iLF[key ].baseType :

var iLF ;// 

if ((data.dataShape==null)||(data.dataShape==undefined)) { 

   iLF = data.ToJSON().dataShape.fieldDefinitions; 

} else { 

   iLF = data.dataShape.fields; 

for (var key in iLF) { 

   result.AddField({ name: key, baseType: (""+iLF[key].baseType) }); 

}

It works

Thank you Carles.

Top Tags