Dojo Dnd问题:Dojo使用insertNodes从源中的json插入数据

时间:2012-05-06 08:36:20

标签: javascript json dojo

我有一个json如下;

var jsonRoles = {"roles": [
                            {"roleId": 1, "roleName": "Admin", "roleDesc": "This is an Admin"},
                            {"roleId": 2, "roleName": "Manager", "roleDesc": "This is a Manager role"},
                            {"roleId": 3, "roleName": "User", "roleDesc": "This is a user role"}
                        ]
                    };

我想在programitacally中插入数据,就像这样;

var rolesItems = new Source("roleNode", { accept: [ "roleName"] });
            rolesItems .insertNodes(false, [
                { data: "Admin",     type: [ "roleName" ] },
                { data: "Manager",   type: [ "roleName" ] },
                { data: "User",      type: [ "roleName" ] }
            ]);
            rolesItems .forInItems(function(item, id, map){
                domClass.add(id, item.type[0]);
            });

我该如何执行任务?有什么想法吗?

1 个答案:

答案 0 :(得分:0)

我想出了如何解决它。如果有人感兴趣,下面是解决方案;

//Json
var jsonRoles = {"roles": [
                  {"roleId": 1, "roleName": "Admin", "roleDesc": "This is an Admin"},
                  {"roleId": 2, "roleName": "Manager", "roleDesc": "This is a Manager role"},
                  {"roleId": 3, "roleName": "User", "roleDesc": "This is a user role"}
                 ]
               };

//Array for Roles
var roles_len = jsonRoles.roles.length;
var arr_roles = [];
var role_type = ["roles"];
for(var i=0;i<roles_len;i++){
   var obj_roles = {};
   obj_roles["data"] = jsonRoles.roles[i].roleName;
   obj_roles["type"] = [role_type[0]];
   arr_roles.push(obj_roles);
}

//Source Container (for dojo 1.6, for dojo 1.7 its different)
    var roleListContainer = new dojo.dnd.Source("roleNode", {accept: ["permissions","roles"]});
    roleListContainer.insertNodes(false, arr_roles);