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

我想以编程方式将数据插入到 dnd 源中,如下所示;

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

我该如何执行任务?任何的想法?

4

1 回答 1

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); 
于 2012-05-13T07:41:24.400 回答