2016-05-27 49 views
0

因此,我將AngularJS合併到我創建的SharePoint頁面中。我有一個函數可以通過REST函數從SharePoint列表中收集信息。它應該做的是用從REST調用中檢索到的數據填充表格。它做得很好,但是,其中一個字段是'個人或組'字段,而不是返回該人員的姓名,它會返回他們的ID,這很好,但它要求我在另一個列表上執行另一個REST呼叫。我已經成功地完成了這項工作,並根據用戶標識過濾了結果。我所做的是將用戶ID傳遞給一個函數,該函數將REST函數調用到包含用戶名的列表中,然後編寫一個if語句,基本上說明傳遞的ID是否等於給定的元素ID,顯示用戶名。我遇到的問題是當我檢索到正確的數據時,我無法返回數據,我相信原因是因爲我試圖從嵌套函數返回數據。我有一個函數,並在該函數中$ http.success函數。我試圖找回的數據駐留在$ http.success函數中。對不起,如果這個問題難以遵循,但希望看看下面的代碼將有助於澄清事情。將REST數據返回到另一個函數

P.S.當我從'displayProjDetails'函數提醒代碼時,我收到一個未定義的消息,但是當我通過'getUser'函數提醒它時,它會正確顯示數據。

感謝您的任何援助。

ANGULAR CODE:

// 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> 

UPDATE: 在製做我的代碼的修改建議,如下我的數據顯示...

Table

正如你所看到的,我在嘗試要做的是用適當的名稱替換'[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}); 
        } 
       }; 

      }); 
     } 

回答

0

的問題是,你與異步調用的工作,所以你不能返回數據,因爲警報不等待承諾的響應,所以一個辦法解決問題是返回從的getUser承諾:

return $http ... 

則警示:

alert($scope.getUser(data.d.results[i].Project_x0020_POCId).then(function(name){return name;})); 
0
$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; 

       $scope.user_attributes = []; 

       for(i=0; i < data.d.results.length; i++) { 
        if(data.d.results[i].Id == value) { 
         resolve(data.d.results[i].Title); 
        } 
       }; 
      }); 
     } 




$scope.getUser(data.d.results[i].Project_x0020_POCId) 
.then(function(user){ 
    alert(user) 
}); 
+0

這是接近我所想不過,我並沒有提醒我需要返回它們的值,所以我可以將它們傳遞給$ scope.details變量來代替'poc'變量。當我將'alert'改爲'return'時,當我去警告函數時,我收到[Object object]。 – Brandon

+0

您可以將它從它的位置傳遞給scope.details。在警報所在的位置添加其餘的代碼。 – Josh

+0

所以你說要把代碼從「if語句下放到$ scope.details」裏面的「then」函數中?對不起,這對我沒有意義,因爲沒有辦法識別「data.d.results [i] .whatever」,因爲它是在「then」函數之外定義的。我很抱歉,如果我很困難,我對Angular仍然陌生,並且試圖理解你想說的話。非常感謝您的全力協助。 – Brandon

相關問題