【发布时间】:2016-05-27 00:12:33
【问题描述】:
所以我将 AngularJS 合并到我正在创建的 SharePoint 页面中。我有一个函数可以通过 REST 函数从 SharePoint 中的列表中收集信息。它应该做的是用它从 REST 调用中检索到的数据填充一个表。它做得很好,但是,其中一个字段是“人员或组”字段,而不是返回人的姓名,而是返回他们的 ID,这很好,但它要求我在另一个列表上执行另一个 REST 调用。我已经成功地这样做了,并根据用户 ID 过滤掉了结果。我所做的是将用户 ID 传递给一个函数,该函数将 REST 函数调用到包含用户名的列表,然后编写一个 if 语句,基本上说如果传递的 ID 等于给定的元素 ID,则显示用户名。我遇到的问题是,当我检索到正确的数据时,我无法返回数据,我相信原因是因为我试图从嵌套函数返回数据。我有一个函数,在该函数中有一个 $http.success 函数。我试图检索的数据位于 $http.success 函数中。抱歉,如果这个问题很难理解,但希望看看下面的代码将有助于解决问题。
附:当我从“displayProjDetails”函数提醒代码时,我收到一条未定义的消息,但是当我从“getUser”函数提醒它时,它会正确显示数据。
提前感谢您的帮助。
角码:
// Function to display Project Details Data
$scope.displayProjDetails = function() {
$http({type: "GET", url:"http://mysiteurl/_api/web/lists/getbytitle('Project%20Details')/items?$top=5000", headers: { "ACCEPT": "application/json;odata=verbose"}})
.success(function(data) {
$scope.results = data.d.results;
$scope.details = [];
var full_funded;
for(i=0; i < data.d.results.length; i++) {
if(data.d.results[i].gibi == $scope.selectedProject.id) {
alert($scope.getUser(data.d.results[i].Project_x0020_POCId)); // CALL TO FUNCTION I AM WORKING ON (LOCATED AT THE BOTTOM OF THE PAGE)
if(data.d.results[i].Fully_x0020_Funded == true) {
full_funded = "Yes";
} else {
full_funded = "No"
}
$scope.details.push({id: data.d.results[i].gibi, poc: data.d.results[i].Project_x0020_POCId, code_poc: data.d.results[i].Code_x0020_312_x0020_POCId,
perc_complete: data.d.results[i].OData__x0025__x0020_Complete, funded: full_funded, pop_from: data.d.results[i].PoP_x0020_From,
pop_to: data.d.results[i].PoP_x0020_To});
}
};
});
}
// Function to retrieve the name of the Point of Contact (Currently working on this...)
$scope.getUser = function(value) {
$http({type: "GET", url: "http://mysiteurl/_api/web/siteusers?$top=5000", headers: { "ACCEPT": "application/json;odata=verbose"}})
.success(function(data) {
$scope.results = data.d.results;
$scope.user_attributes = [];
for(i=0; i < data.d.results.length; i++) {
if(data.d.results[i].Id == value) {
return data.d.results[i].Title;
}
};
});
}
HTML 代码
<div class="col-lg-9" id="project_details_table">
<h3>Project Details</h3>
<table class="table table-striped">
<thead>
<tr>
<th>Project ID</th>
<th>PoC</th>
<th>Code 2532 PoC</th>
<th>% Complete</th>
<th>Fully Funded</th>
<th>PoP From</th>
<th>PoP To</th>
</tr>
</thead>
<tbody>
<tr data-ng-repeat="detail in details">
<td data-ng-bind="detail.id"></td>
<td data-ng-bind="detail.poc"></td> <!--TRYING TO REPLACE THIS WITH THE USER'S NAME INSTEAD OF THE USER'S ID-->
<td data-ng-bind="detail.code_poc"></td>
<td data-ng-bind="detail.perc_complete"></td>
<td data-ng-bind="detail.funded"></td>
<td data-ng-bind="detail.pop_from | date:'yyyy/MM/dd'"></td>
<td data-ng-bind="detail.pop_to | date:'yyyy/MM/dd'"></td>
</tr>
</tbody>
</table>
</div>
更新: 对我的代码进行建议的更改后,我的数据显示如下...
如您所见,我正在尝试用正确的名称替换“[object Object]”。在这里,它只是向表中添加新行,而不是将名称放在正确的位置。这是我更新的代码...
$scope.getUser = function(value) {
return $q(function(resolve, reject){
$http({type: "GET", url: "http:mysiteurl/_api/web/siteusers?$top=5000", headers: { "ACCEPT": "application/json;odata=verbose"}})
.success(function(data) {
$scope.results = data.d.results;
for(i=0; i < data.d.results.length; i++) {
if(data.d.results[i].Id == value) {
resolve(data.d.results[i].Title);
}
};
});
});
}
// Function to display Project Details Data
$scope.displayProjDetails = function() {
$http({type: "GET", url:"http:mysiteurl/_api/web/lists/getbytitle('Project%20Details')/items?$top=5000", headers: { "ACCEPT": "application/json;odata=verbose"}})
.success(function(data) {
$scope.results = data.d.results;
$scope.details = [];
$scope.name = [];
var full_funded;
for(i=0; i < data.d.results.length; i++) {
if(data.d.results[i].gibi == $scope.selectedProject.id) {
$scope.name = $scope.getUser(data.d.results[i].Project_x0020_POCId).then(function(user){$scope.details.push({poc: user});});
if(data.d.results[i].Fully_x0020_Funded == true) {
full_funded = "Yes";
} else {
full_funded = "No"
}
$scope.details.push({id: data.d.results[i].gibi, poc: $scope.name, code_poc: data.d.results[i].Code_x0020_312_x0020_POCId,
perc_complete: data.d.results[i].OData__x0025__x0020_Complete, funded: full_funded, pop_from: data.d.results[i].PoP_x0020_From,
pop_to: data.d.results[i].PoP_x0020_To});
}
};
});
}
【问题讨论】:
标签: angularjs function rest sharepoint return