Javascript 使用多维数组创建表-错误:无法设置未定义的属性
我正在尝试使用javascript和数组创建一个表。我正在对SharePoint进行客户端调用,以返回XML响应,该响应提供了我的数据(用户/组)。通过这些调用,我得到了一个包含所有用户和组的数组。我需要遍历每个组并找到其中的用户,如果用户在该组中,则用X标记,如果不是,则留空。我很难让它工作 这就是我想做的:Javascript 使用多维数组创建表-错误:无法设置未定义的属性,javascript,jquery,xml,arrays,datatable,Javascript,Jquery,Xml,Arrays,Datatable,我正在尝试使用javascript和数组创建一个表。我正在对SharePoint进行客户端调用,以返回XML响应,该响应提供了我的数据(用户/组)。通过这些调用,我得到了一个包含所有用户和组的数组。我需要遍历每个组并找到其中的用户,如果用户在该组中,则用X标记,如果不是,则留空。我很难让它工作 这就是我想做的: //Build taxonomy var allGroups = ["Users"]; var allUsers = []; var taxonomy = []; function
//Build taxonomy
var allGroups = ["Users"];
var allUsers = [];
var taxonomy = [];
function initTax(){
console.log("initiating taxonomy...")
//get all users and add them to array
$().SPServices({
operation: "GetUserCollectionFromSite",
async: true,
completefunc: function(xData,Status){
$(xData.responseXML).find("User").each(function(){
allUsers.push($(this).attr("userLoginName"));
});
}
});
//get all groups and add them to array
$().SPServices({
operation: "GetGroupCollectionFromSite",
async: true,
completefunc: function(xData,Status){
$(xData.responseXML).find("Group").each(function(){
allGroups.push($(this).attr("Name"));
});
}
});
buildTable();
}
var i, j;
function buildTable(){
for (j=0;j<allGroups.length;i++){
for (i=0;i<allUsers.length;i++){
$().SPServices({
operation: "GetGroupCollectionFromUser",
userLoginName: allUsers[i],
async: true,
completefunc: function(xData, Status){
var userInGroup = $(xData.responseXML).find(allGroups[j]);
if(userInGroup){
taxonomy[j][i] = "X";
}else{
taxonomy[j][i] = "";
}
}
});
}
}
}
//构建分类法
var allGroups=[“用户”];
var allUsers=[];
var分类法=[];
函数initTax(){
日志(“启动分类…”)
//获取所有用户并将其添加到阵列
$().SPServices({
操作:“GetUserCollectionFromSite”,
async:true,
completefunc:函数(扩展数据、状态){
$(扩展数据.responseXML).find(“用户”).each(函数(){
allUsers.push($(this.attr(“userLoginName”);
});
}
});
//获取所有组并将它们添加到数组中
$().SPServices({
操作:“GetGroupCollectionFromSite”,
async:true,
completefunc:函数(扩展数据、状态){
$(扩展数据.responseXML).find(“组”).each(函数(){
allGroups.push($(this.attr(“Name”));
});
}
});
buildTable();
}
varⅠ,j;
函数buildTable(){
对于(j=0;j我决定将工作分成5个函数,其中3个函数模拟对服务器的请求,并使用jquery处理xml。另外2个函数负责验证数据并构建表
这是我的js代码:
/*
Simulate get requesto to the server
*/
var getGroupCollectionFromUser = '<GetGroupCollectionFromUser xmlns='
+ '"http://schemas.microsoft.com/sharepoint/soap/directory/">'
+ '<Groups>'
+'<Group ID="3" Name="Group1" Description="Description" OwnerID="1" OwnerIsUser="False" />'
+'<Group ID="15" Name="Group2" Description="Description" OwnerID="12" OwnerIsUser="True" />'
+'</Groups></GetGroupCollectionFromUser>';
var getGroupCollectionFromSite = '<GetGroupCollectionFromSite xmlns= '
+'"http://schemas.microsoft.com/sharepoint/soap/directory/">'
+'<Groups>'
+'<Group ID="3" Name="Group1" Description="Description" OwnerID="1" OwnerIsUser="False" />'
+'<Group ID="15" Name="Group2" Description="Description" OwnerID="12" OwnerIsUser="True" />'
+'<Group ID="16" Name="Group3" Description="Description" OwnerID="7" OwnerIsUser="False" />'
+'</Groups></GetGroupCollectionFromSite>';
var getUserCollectionFromSite = '<GetUserCollectionFromSite xmlns='
+'"http://schemas.microsoft.com/sharepoint/soap/directory/">'
+'<Users>'
+'<User ID="4" Sid="S-1-5-21-2127521184-1604012920-1887927527-'
+'34577" Name="User1_Display_Name" '
+'LoginName="DOMAIN\User1_Alias" Email="User1_E-mail" '
+' Notes="Notes" IsSiteAdmin="False" IsDomainGroup="False" />'
+'<User ID="5" Sid="S-1-5-21-2127521184-1604012920-1887927527-'
+'354403" Name="User2_Display_Name" '
+'LoginName="DOMAIN\User2_Alias" Email="User2_E-mail" '
+'Notes="Notes" IsSiteAdmin="False" IsDomainGroup="False" />'
+'</Users></GetUserCollectionFromSite>';
var userGroupTable = {};
var users = [];
var groups = [];
function parseUserColl (){
//simulate the get request to the server
var dom = $(getUserCollectionFromSite);
//process all user name
dom.find('User').each(function(e){
users.push($(this).attr('name'));
});
}
function parseGroupColl (){
//simulate the get request to the server
var dom = $(getGroupCollectionFromSite);
//process all group name
dom.find('Group').each(function(e){
groups.push($(this).attr('name'));
});
}
function parseGroupCollFromUser (user){
//simulate the get request to the server
var dom = $(getGroupCollectionFromUser);
var userGroups = [];
//process all group name
dom.find('Group[Name]').each(function(e){
userGroups.push($(this).attr('Name'));
});
//return all the groups of the user
return userGroups;
}
function processAll(){
for(var i=0; i<users.length; i++){
userGroupTable[users[i]] = {};
var userGroups = parseGroupCollFromUser(users[i]);
for(var j=0; j<groups.length; j++){
//if the array userGroups contains the current group, the value must be true
userGroupTable[users[i]][groups[j]] = userGroups.indexOf(groups[j]) > -1;
}
}
}
function buildTable (){
//build header
var header = "<tr><th>USERS</th>"
for(var i=0; i<groups.length; i++){
header += "<th>"+groups[i]+"</th>";
}
header += "</tr>";
$("#table-data thead").append($(header));
//build table body
for(var user in userGroupTable){
var row ="<tr><td>" + user + "</td>";
for(var i=0; i<groups.length; i++){
var groupName = groups[i];
var $td = "<td>"+userGroupTable[user][groupName]+"</td>";
row +=$td;
}
row += "</tr>";
//append the data to the table
$("#table-data tbody").append($(row));
}
}
parseUserColl();
parseGroupColl();
processAll();
/*
模拟服务器的get请求
*/
变量getGroupCollectionFromUser=''
+ ''
+''
+''
+'';
变量getGroupCollectionFromSite=''
+''
+''
+''
+''
+'';
var getUserCollectionFromSite=''
+''
+''
+''
+'';
var userGroupTable={};
var用户=[];
var组=[];
函数parseUserColl(){
//模拟对服务器的get请求
var dom=$(getUserCollectionFromSite);
//处理所有用户名
dom.find('User')。每个(函数(e){
users.push($(this.attr('name'));
});
}
函数parseGroupColl(){
//模拟对服务器的get请求
var dom=$(getGroupCollectionFromSite);
//处理所有组名
dom.find('Group')。每个(函数(e){
groups.push($(this.attr('name'));
});
}
函数parseGroupCollFromUser(用户){
//模拟对服务器的get请求
var dom=$(getGroupCollectionFromUser);
var userGroups=[];
//处理所有组名
dom.find('Group[Name]')。每个(函数(e){
userGroups.push($(this.attr('Name'));
});
//返回用户的所有组
返回用户组;
}
函数processAll(){
对于(var i=0;我可以放一些xml代码吗?非常感谢。这正是我正在尝试做的,并且与我的代码完美结合。
<GetUserCollectionFromSite xmlns=
"http://schemas.microsoft.com/sharepoint/soap/directory/">
<Users>
<User ID="4" Sid="S-1-5-21-2127521184-1604012920-1887927527-
34577" Name="User1_Display_Name"
LoginName="DOMAIN\User1_Alias" Email="User1_E-mail"
Notes="Notes" IsSiteAdmin="False" IsDomainGroup="False" />
<User ID="5" Sid="S-1-5-21-2127521184-1604012920-1887927527-
354403" Name="User2_Display_Name"
LoginName="DOMAIN\User2_Alias" Email="User2_E-mail"
Notes="Notes" IsSiteAdmin="False" IsDomainGroup="False" />
.
.
.
</Users>
</GetUserCollectionFromSite>
/*
Simulate get requesto to the server
*/
var getGroupCollectionFromUser = '<GetGroupCollectionFromUser xmlns='
+ '"http://schemas.microsoft.com/sharepoint/soap/directory/">'
+ '<Groups>'
+'<Group ID="3" Name="Group1" Description="Description" OwnerID="1" OwnerIsUser="False" />'
+'<Group ID="15" Name="Group2" Description="Description" OwnerID="12" OwnerIsUser="True" />'
+'</Groups></GetGroupCollectionFromUser>';
var getGroupCollectionFromSite = '<GetGroupCollectionFromSite xmlns= '
+'"http://schemas.microsoft.com/sharepoint/soap/directory/">'
+'<Groups>'
+'<Group ID="3" Name="Group1" Description="Description" OwnerID="1" OwnerIsUser="False" />'
+'<Group ID="15" Name="Group2" Description="Description" OwnerID="12" OwnerIsUser="True" />'
+'<Group ID="16" Name="Group3" Description="Description" OwnerID="7" OwnerIsUser="False" />'
+'</Groups></GetGroupCollectionFromSite>';
var getUserCollectionFromSite = '<GetUserCollectionFromSite xmlns='
+'"http://schemas.microsoft.com/sharepoint/soap/directory/">'
+'<Users>'
+'<User ID="4" Sid="S-1-5-21-2127521184-1604012920-1887927527-'
+'34577" Name="User1_Display_Name" '
+'LoginName="DOMAIN\User1_Alias" Email="User1_E-mail" '
+' Notes="Notes" IsSiteAdmin="False" IsDomainGroup="False" />'
+'<User ID="5" Sid="S-1-5-21-2127521184-1604012920-1887927527-'
+'354403" Name="User2_Display_Name" '
+'LoginName="DOMAIN\User2_Alias" Email="User2_E-mail" '
+'Notes="Notes" IsSiteAdmin="False" IsDomainGroup="False" />'
+'</Users></GetUserCollectionFromSite>';
var userGroupTable = {};
var users = [];
var groups = [];
function parseUserColl (){
//simulate the get request to the server
var dom = $(getUserCollectionFromSite);
//process all user name
dom.find('User').each(function(e){
users.push($(this).attr('name'));
});
}
function parseGroupColl (){
//simulate the get request to the server
var dom = $(getGroupCollectionFromSite);
//process all group name
dom.find('Group').each(function(e){
groups.push($(this).attr('name'));
});
}
function parseGroupCollFromUser (user){
//simulate the get request to the server
var dom = $(getGroupCollectionFromUser);
var userGroups = [];
//process all group name
dom.find('Group[Name]').each(function(e){
userGroups.push($(this).attr('Name'));
});
//return all the groups of the user
return userGroups;
}
function processAll(){
for(var i=0; i<users.length; i++){
userGroupTable[users[i]] = {};
var userGroups = parseGroupCollFromUser(users[i]);
for(var j=0; j<groups.length; j++){
//if the array userGroups contains the current group, the value must be true
userGroupTable[users[i]][groups[j]] = userGroups.indexOf(groups[j]) > -1;
}
}
}
function buildTable (){
//build header
var header = "<tr><th>USERS</th>"
for(var i=0; i<groups.length; i++){
header += "<th>"+groups[i]+"</th>";
}
header += "</tr>";
$("#table-data thead").append($(header));
//build table body
for(var user in userGroupTable){
var row ="<tr><td>" + user + "</td>";
for(var i=0; i<groups.length; i++){
var groupName = groups[i];
var $td = "<td>"+userGroupTable[user][groupName]+"</td>";
row +=$td;
}
row += "</tr>";
//append the data to the table
$("#table-data tbody").append($(row));
}
}
parseUserColl();
parseGroupColl();
processAll();
<button class="btn" onClick="buildTable()">Build Table</button>
<table class="table" id="table-data">
<thead></thead>
<tbody></tbody>
</table>