0

I want to create a dashboard indicating when all items in a list are equal to "Yes". For testing I am innerHtmling Done/Not Done. The logic seems to work , but I am afraid the code is testing the list as a whole and not the individual items. I am using choice columns.

Am I missing a loop?

<h1 id="yes/no"></h1>

$(document).ready(function() {    
arrayIn_OutItems();    
}); 

function arrayIn_OutItems(){
  var siteUrl = "https:xxx/_api/web/lists/GetByTitle('listName')/items?$select="Title,Office,HR,Accounting;
  var array = []

  $.ajax({
    url: siteUrl,
    type: "GET",
    async: false,
    headers: {
        "accept": "application/json;odata=verbose"
    },
    success: function (data) {
        array = data.d.results;
        console.log(array);
        var isYes = 
        for(var i = 0;array[i].length < 0;i++{
            array.every(function (e) {
      return e  == "Yes"
      });
        }
        if(isYes == true){
          document.getElementById('yes/no').innerHTML = "Done";
        }else{
          document.getElementById('yes/no').innerHTML = "Not Done";
        }

    },
    error: function (err) {
        console.log(err);
    }
 });

}
1

Check the syntax of your for loop. There's a missing paren after "i++".

And you probably want something like this:

var isYes = array.every( function (e) { return e  == "Yes" } )

No for loop needed.

 

Also, you have mismatched quotes in the URL line:

var siteUrl = "https:xxx/_api/web/lists/GetByTitle('listName')/items?$select="Title,Office,HR,Accounting;

should be something like:

var siteUrl = "https:xxx/_api/web/lists/GetByTitle('listName')/items?$select=Title,Office,HR,Accounting";

Are all four columns "Title,Office,HR,Accounting", Yes/No columns? Your "data.d.results" is not selecting any particular data.

 

Another note: your REST statement returns all rows in the list. Is that what you wanted? You may need to add a filter to select a single row.

If you are testing a single row, then something like the following would work, assuming Office, HR and Account are choice columns with Yes or No:

var isYes = data.d.results[0].Office == "Yes" || 
            data.d.results[0].HR == "Yes" || 
            data.d.results[0].Accounting == "Yes"
  • I want to return items from the list and check each of their columns for Yes or No. And once they are all Yes, returning true, display some sort of icon indicating all of the columns for a particular column is a "Yes". – Ben Segarra Jul 20 at 1:35
  • 1
    Easiest way might just be a Calculated Column. =AND([Office]="Yes", [HR]="Yes",[Office]="Accounting") As you are using on-prem SharePoint, you can display the result as an icon using this approach: techtrainingnotes.blogspot.com/2018/01/… – Mike Smith - MCT - MVP Jul 20 at 2:37
  • 1
    If you want to use the JavaScript REST approach, you will need to filter the REST request with a unique property such as the internal ID so you only return a single row. Add something like this to the end of the REST URL: &$filter=Id eq 2 – Mike Smith - MCT - MVP Jul 20 at 2:39
  • Thanks for your help. – Ben Segarra Jul 21 at 1:13
  • 1
    Ben, was this the answer to your question, or did you take another approach. Just curious. – Mike Smith - MCT - MVP Jul 21 at 1:35

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.