I have a string as
var str = 'Supplier Name^supplier^left^string*Spend (USD MM)^spend^right^number^5';
I write a function in javascript to retrieve the datafield names from the above string and store it in an array as :
function getColNamesfromConfig(str) {
var cols = new Array();
for (i = 0; i <= str.split('*').length - 1; i++) {
cols[i] = str.split('*')[i].split('^')[1];
//Will write logic to retrieve the Supplier Name, Spend (USD MM) fields etc
}
return cols;
}
The result i get is as :
cols[0] = supplier;
cols[1] = spend; and so on..(which are datafields)
Then i make a dynamic table and use the above info retrieved as :
"onResultHttpService": function (result, properties) {
var json_str = Sys.Serialization.JavaScriptSerializer.deserialize(result);
var colNames = getColNamesfromConfig(properties.PodAttributes.DGConfig);
var htmlMarkup = '';
htmlMarkup = "";
htmlMarkup = htmlMarkup + "<table width='100%' border='1' cellspacing='0' cellpadding='0' class='gridView gridMouseOverEffect'>";
htmlMarkup = htmlMarkup + "<tr>";
for (var c = 0, colLen = colNames.length; c < colLen; c++) {
//COLUMN LOOP (here i want to bind 'Supplier Name' and not 'supplier' which is what i get from getColNamesfromConfig(str);
htmlMarkup = htmlMarkup + "<th align='left' class='secondaryLink tableContentRow'> <h5>" + colNames[c] + "</h5>";
htmlMarkup = htmlMarkup + "</th>";
}
htmlMarkup = htmlMarkup + "</tr>";
for (var i = 0, rowlen = json_str.length; i < rowlen; i++) {
htmlMarkup = htmlMarkup + " <tr>"
for (var c = 0, colLen = colNames.length; c < colLen; c++) {
htmlMarkup = htmlMarkup + " <td align='left' class='secondaryLink tableContentRow'> " + json_str[i][colNames[c]];
htmlMarkup = htmlMarkup + "</td>"
}
htmlMarkup = htmlMarkup + "</tr>"
}
htmlMarkup = htmlMarkup + "</table>"
divPortletId = '#' + properties.id;
$(htmlMarkup).appendTo($(divPortletId));
}
If i had to retrieve the Display Name as well from the sample string how would i store it in the same array and access it?. For ex: i want something where i can just loop and get my Display Name (Supplier Name) and also datafield name (supplier). I just want to bind the Display name in my COLUMN LOOP where it is currently binding the column data field. Thus please help me TWEAK my getColNamesfromConfig(str) function to return both the datafield as well as displayname in an array or an object literal if possible..I need something like
cols[0].DisplayName = "Supplier Name";
cols[0].DatafieldName = "supplier";
cols[1].DisplayName = "Spend (USD MM)";
cols[1].DatafieldName = "spend";
That should do the trick