-1

我有一个字符串

var str = 'Supplier Name^supplier^left^string*Spend (USD MM)^spend^right^number^5';

我在 javascript 中编写了一个函数来从上面的字符串中检索数据字段名称并将其存储在一个数组中:

        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;
    }

我得到的结果是: cols[0] = supplier; cols[1] = 花费;等等..(这是数据字段)

然后我制作一个动态表并使用上述检索到的信息:

"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));

        }

如果我还必须从示例字符串中检索显示名称,我将如何将它存储在同一个数组中并访问它?例如:我想要一些我可以循环并获取我的显示名称(供应商名称)以及数据字段名称(供应商)的东西。我只想在当前绑定列数据字段的 COLUMN LOOP 中绑定显示名称。因此,请帮助我调整我的 getColNamesfromConfig(str) 函数以返回数组中的数据字段和显示名称或对象文字(如果可能)。我需要类似的东西

cols[0].DisplayName = "Supplier Name";
cols[0].DatafieldName = "supplier";
cols[1].DisplayName = "Spend (USD MM)";
cols[1].DatafieldName = "spend";
4

1 回答 1

1
function getColNamesfromConfig(str) {
    var cols = new Array();
    for (i = 0; i <= str.split('*').length - 1; i++) { 
        cols[i] = {}
        cols[i].DatafieldName = str.split('*')[i].split('^')[1]; 
        cols[i].DisplayField = str.split('*')[i].split('^')[0];
    }
    return cols;
}

这应该够了吧

于 2012-07-27T07:06:50.883 回答