1

newbie question...I'm trying to pull in current user data. The return statement in the first code block is populating my table. The second one is not returning any data. I'm trying to look up the current user to use that data in my script. Any help is appreciated! Thanks!

function GenerateTableFromJson(objArray,objArrayTwo) {

var tableContent = '<table summary="GFSTechIntake" id="AllRequestsTable" style="width:100%"><thead><tr><th>Rank</th><th>Category</th></tr></thead><tbody>';
for (var i = 0; i < objArray.length; i++) {
    var rank = "";
    for(var j = 0;j < objArrayTwo.length; j++){
        if(objArray[i].Title == objArrayTwo[j].RequestName){
            rank = objArrayTwo[j].Rank ? objArrayTwo[j].Rank : "";
            console.log(objArrayTwo[j]);
        } 
    }   
     tableContent += '<tr class="allItemsTr">';
     if(rank != ''){
     tableContent += '<td class="allItemsPriority"><input class="title" placeholder=' + rank + ' value='+rank+'></td>';
     } else {
     tableContent += '<td class="allItemsPriority"><input class="title" placeholder=' + rank + '></td>';
     }
     tableContent += '<td class="category">'+category+'</td>';
     tableContent += '</tr>';
 } 

 return tableContent;
 }

Below is not working.

function GenerateTableFromJson(objArray,objArrayTwo) {
    function GetCurrentUser() {
        var userid = _spPageContextInfo.userId;
        var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/web/getuserbyid(" + userid + ")";   
        var requestHeaders = { "accept" : "application/json;odata=verbose" };

        return $.ajax({
            url: requestUri,
            contentType: "application/json;odata=verbose",
            headers: requestHeaders
        }); }
var currentUserRequest = GetCurrentUser();
var tableContent = '<table summary="GFSTechIntake" id="AllRequestsTable" style="width:100%"><thead><tr><th>Rank</th><th>Category</th></tr></thead><tbody>';


currentUserRequest.done(function (userData) {       
for (var i = 0; i < objArray.length; i++) {
    var rank = "";
    for(var j = 0;j < objArrayTwo.length; j++){
        if(objArray[i].Title == objArrayTwo[j].RequestName){
            rank = objArrayTwo[j].Rank ? objArrayTwo[j].Rank : "";
            console.log(objArrayTwo[j]);
        } 
    }   
     tableContent += '<tr class="allItemsTr">';
     if(rank != ''){
     tableContent += '<td class="allItemsPriority"><input class="title" placeholder=' + rank + ' value='+rank+'></td>';
     } else {
     tableContent += '<td class="allItemsPriority"><input class="title" placeholder=' + rank + '></td>';
     }
     tableContent += '<td class="category">'+category+'</td>';
     tableContent += '</tr>';
 } 

 });

 return tableContent;
 }
  • Can you elaborate. exactly for which purpose you are fetching current data ? – Rohit Waghela Sep 11 '17 at 6:20
  • because I don't see that you are using current user details anywhere to generate/filter the table. – Rohit Waghela Sep 11 '17 at 6:28
1

It might be due to your ajax request failing. You need to handle the fail and see the error.

currentUserRequest.done(function (userData) {
        for (var i = 0; i < objArray.length; i++) {
            var rank = "";
            for (var j = 0; j < objArrayTwo.length; j++) {
                if (objArray[i].Title == objArrayTwo[j].RequestName) {
                    rank = objArrayTwo[j].Rank ? objArrayTwo[j].Rank : "";
                    console.log(objArrayTwo[j]);
                }
            }
            tableContent += '<tr class="allItemsTr">';
            if (rank != '') {
                tableContent += '<td class="allItemsPriority"><input class="title" placeholder=' + rank + ' value=' + rank + '></td>';
            } else {
                tableContent += '<td class="allItemsPriority"><input class="title" placeholder=' + rank + '></td>';
            }
            tableContent += '<td class="category">' + category + '</td>';
            tableContent += '</tr>';
        }

    }).fail(function (error)
    {
        console.log(error);
    });

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.