I am creating one generic service to return the infotable values as HTML codes in the format of string output.
But unable to fetch the column values dynamically,
for ex: my column names are stored in the array as ['col1', 'col2'], etc... If I pass these values in for loop(row.array[0]) to iterate the column values, the service returns undefined for the values.
code:
const table = me.testTable; // columns: count, name
let fieldNameArr = [];
function createMainTable() {
var fields = table.dataShape.fields;
for (let fieldName in fields) {
fieldNameArr.push(fieldName);
}
var cols = table.getFieldCount();
var rows = table.rows.length;
var drawTable = '<table>';
for (var i = 0; i < rows; i++) {
let row = table.rows[i];
drawTable += '<tr>';
for (var j = 0; j < cols; j++) {
let fieldName = fieldNameArr[j];
columnValue = row.fieldName;
drawTable += '<td>' + columnValue + '</td>';
}
drawTable += '</tr>';
}
drawTable += '</table>';
return drawTable;
}
result = createMainTable();
// output/result
<table><tr><td>undefined</td><td>undefined</td></tr><tr><td>undefined</td><td>undefined</td></tr><tr><td>undefined</td><td>undefined</td></tr></table>
Solved! Go to Solution.
you need to use
columnValue = row[fieldName];
as "fieldName" is a variable which it should evaluate. Currently, it will try to get the column named "fieldName" when you access it via row.fieldName - which is "undefined".
you need to use
columnValue = row[fieldName];
as "fieldName" is a variable which it should evaluate. Currently, it will try to get the column named "fieldName" when you access it via row.fieldName - which is "undefined".
Thanks for this.