使用json数据为JQGrid生成动态列

时间:2014-02-28 11:52:35

标签: jquery json jqgrid

我在表单中有逗号分隔值,如下所示:

["Error","UserId","Username","EmailAddress","TitleId","FirstName","Surname","IsActive","PasswordEncoded","UserGroupId","UserLocationId","IsTeamLeader","TeamLeaderUserId","BadLoginCount","LastLoggedInDate","PasswordLastUpdatedDate","WebServiceHashKey","CustomerId","IsSalesRep","ManagerFirstName","ManagerLastName","ManagerEmail","TeamLeaderUpdatedDate","IsPushedToReporting"] 

这些值是我的列的名称,我必须在我的JQGrid中显示,所以我使用以下代码生成我的列:

var splitCols = JSON.parse(cols);
var colM = splitCols.each(splitCols, function (a,b) {
    JSON.parse('[{"name": "' + b + '"}]');
});

variable cols由我的所有列名组成,逗号分隔如上所示。

所以,我的全部和灵魂关注的是生成像:

这样的列
{name: 'UserId'}, {name: 'UserName'}

任何人都可以帮助我实现这个目标

1 个答案:

答案 0 :(得分:1)

请尝试以下操作:

var colmns = [];
$.each(cols, function (a, b) {
    var obj = {};
    obj["name"] = b;
    colmns.push(obj);
});
console.log(JSON.stringify(cols));

此处console.log()输出如下内容:

[{"name":"Error"},{"name":"UserId"},{"name":"Username"},.....]

Checkout the jsfiddle.