Getting list items from rest api and grouping by lookup to a table

  • 28 August 2020
  • 0 replies
  • 36 views

Badge +1

I'm trying to find a workaround for the listview control and i'm using REST API to get items. I am able to get the items along with a lookup field but having trouble grouping the items and writing to a table within a nintex form. Here is a snippet of my code:

 

var pollSP;
NWF.FormFiller.Events.RegisterAfterReady(function() {
pollSP = setInterval(checkSPLoad, 500);
});

function checkSPLoad() {
if (clientContext) {
window.clearInterval(pollSP);
var hostweburl = decodeURIComponent(getQueryStringParameter("SPHostUrl"));
var appweburl = decodeURIComponent(getQueryStringParameter("SPAppWebUrl"));
var layoutsPath = "/_layouts/15/";
var scriptbase = appweburl + layoutsPath;
NWF$.getScript(scriptbase + "SP.js", function() {
NWF$.getScript(scriptbase + "SP.RequestExecutor.js", execCrossDomainRequest);
});
}

function getQueryStringParameter(param) {
var params = document.URL.split("?")[1].split("&");
var strParams = "";
for (var i = 0; i < params.length; i = i + 1) {
var singleParam = params[i].split("=");
if (singleParam[0] == param) {
return singleParam[1];
}
}
}


function execCrossDomainRequest() {
var deferred = NWF$.Deferred();
var url = appweburl + "/_api/SP.AppContextSite(@target)/web/lists/GetByTitle('ResearchPriority')/Items?$select=ID,Title,ResearchPriorityNumber,InjuryTopicArea/Title&$expand=InjuryTopicArea/Title&@target='" + hostweburl + "'";
var executor = new SP.RequestExecutor(appweburl);
executor.executeAsync({
url: url,
method: "GET",
headers: {
"accept": "application/json;odata=verbose"
},
success: function(data) {
var jsonobj = JSON.parse(data.body);
var results = jsonobj.d.results;
deferred.resolve(data);
var msg = "";
for (var i = 0; i < results.length; i++) {
msg = msg + results[i].Title + results[i].ResearchPriorityNumber + results[i].InjuryTopicArea.Title +";";
}
//alert('Injury Topic Areas = ' + msg);
//var dataResults = data.d.results;
//var injuryTopics = groupBy(dataResults, 'InjuryTopicArea/Title');
//console.log(injuryTopics);
//console.log("Injury Topics = " + msg);
},
error: function(data) {
deferred.reject(data);
alert("data");
}

});
}
//function groupBy(items,propertyName)
//{
//var injurytopicResults = [];
//$.each(items, function(index, item) {
//if ($.inArray(item[propertyName], injurytopicResults)==-1) {
//result.push(item[propertyName]);
//}
//});
//return injurytopicResults;
//alert('Group By Function');
//}
}

 

I commented out the groupby function because it wasn't grouping the items by InjuryTopicArea/Title. I need to group the items and write to a html table.

 

Any help or assistance will be greatly appreciated.


0 replies

Be the first to reply!

Reply