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

Community Tip - New to the community? Learn how to post a question and get help from PTC and industry experts! X

how to pass dynamic value for datatable columns

AP_9467111
6-Contributor

how to pass dynamic value for datatable columns

Iam checking each row of datatable in for loop.

So iam using code: resultDT.rows[i].XXXXX 

I stored the field definitions in array arr[] inside the same service. I want to pass them dynamically inside for loop to the code resultDT.rows[i].XXXXX . I dont want to hardcode them again. How to make this possible? 

 

1 ACCEPTED SOLUTION

Accepted Solutions
TonyZhang
13-Aquamarine
(To:AP_9467111)

Hi @AP_9467111,

 

Please find below sample code to see if that gives you some clues.

 

// Suppose you have arrays contain data for each column.

let column1Array = ["a","b","c","d"],
    column2Array = ["e","f","g","h"];

 

// Create Infotable from DataShape
let infotable = Resources["InfoTableFunctions"].CreateInfoTableFromDataShape({
    infoTableName: "InfoTable",
    dataShapeName: "SampleDataShape"
});

 

// Loop through the first array

column1Array.forEach(e => {
    // create new entry in Json format
    let newEntry = {
        Column1: e,// STRING [Primary Key]
        Column2: undefined// STRING
    };
    // add entry to Infotable
    infotable.AddRow(newEntry);
});

 

// update rest columns of each infotable row

infotable.rows.toArray().forEach((row, index) => {
    row.Column2 = column2Array[index];
});

 

// add infotable entries to datatable

me.AddOrUpdateDataTableEntries({
    sourceType: undefined /* STRING */,
    values: infotable /* INFOTABLE */,
    location: undefined /* LOCATION */,
    source: undefined /* STRING */,
    tags: undefined /* TAGS */
});

View solution in original post

2 REPLIES 2
TonyZhang
13-Aquamarine
(To:AP_9467111)

Hi @AP_9467111,

 

Please find below sample code to see if that gives you some clues.

 

// Suppose you have arrays contain data for each column.

let column1Array = ["a","b","c","d"],
    column2Array = ["e","f","g","h"];

 

// Create Infotable from DataShape
let infotable = Resources["InfoTableFunctions"].CreateInfoTableFromDataShape({
    infoTableName: "InfoTable",
    dataShapeName: "SampleDataShape"
});

 

// Loop through the first array

column1Array.forEach(e => {
    // create new entry in Json format
    let newEntry = {
        Column1: e,// STRING [Primary Key]
        Column2: undefined// STRING
    };
    // add entry to Infotable
    infotable.AddRow(newEntry);
});

 

// update rest columns of each infotable row

infotable.rows.toArray().forEach((row, index) => {
    row.Column2 = column2Array[index];
});

 

// add infotable entries to datatable

me.AddOrUpdateDataTableEntries({
    sourceType: undefined /* STRING */,
    values: infotable /* INFOTABLE */,
    location: undefined /* LOCATION */,
    source: undefined /* STRING */,
    tags: undefined /* TAGS */
});

TonyZhang
13-Aquamarine
(To:AP_9467111)

Hi @AP_9467111,

 

If you feel your question has been answered, please mark the appropriate response as the Accepted Solution for the benefit of others with the same question.

 

Regards,

Tony

Top Tags