List and library verify all items completed

@Nikita_Kurguzov
Good day ,
I have a list1. This list1 have a list and library control to list 2 with tasks.
I need to check State of tasks ,and then if all tasks are Closed , to enable button to apply form in main list1. It can be no tasks , or 1 , or more. To verify if they a closed and then enable button - send for validation

Dear @ixxxl,
The approach to this would be a little tricky, especially if there are a lot of items - there is no way to retrieve them from a List or Library if they span multiple pages (you can only retrieve items on the current page), you'll need to use pnpjs to retrieve all the items - List Items - PnP/PnPjs

Are these filtered by a lookup field? Then you'll need to include a filter condition in your pnpjs request.

@Nikita_Kurguzov
Yes this list and library is filtered by lookup id.
I think in list and library it will up to maximum 5 tasks.

Dear @ixxxl,
You can try it like this:

fd.spRendered(function(){
    function validateListOrLibrary(){
      var items = fd.control('SPDataTable1').widget.dataItems();
      for(var i = 0; i < items.length; i++){
        if(items[i].State != 'Closed'){
          $(fd.control('Button1').$parent.$el).hide();
          return false;
        }
      }
      (fd.control('Button1').$parent.$el).show();
      return true;
    }

    fd.control('SPDataTable1').$on('change', function() {
        validateListOrLibrary();
    });

    validateListOrLibrary();
});
1 Like

@Nikita_Kurguzov
Thank you !! It works!!
one thing i changed - $(fd.control('Button1').$parent.$el).hide(); to fd.control('Button1').disabled=true
Because this one doesn't work for me - $(fd.control('Button1').$parent.$el).hide();

Dear @ixxxl,
Oh yeah, for button it should be

$(fd.control('Button1').$el).hide();
$(fd.control('Button1').$el).show();
1 Like

good day @Nikita_Kurguzov
it works for some period, for now , it returns true for 3 tasks,which are not all closed , it seems that checks only first task ,not each other.

fd.spRendered(function () {
    function validateListOrLibrary() {
        setTimeout(function () {
            var items = fd.control('SPDataTable1').widget.dataItems();
            for (var i = 0; i < items.length; i++) {
			  console.log('ITEMS STARE '+ items[i].Stare)
                if (items[i].Stare !== 'Closed' ) {
                    
                    fd.control('Button5').disabled = true;
                    console.log('========Disabled button= Transmite spre validare=======================return================FALSE==================')
                    return false;
                } else {
                 
                    fd.control('Button5').disabled = false;
                    console.log('===============Enable button== Transmite spre validare=====================================TRUE==================')
                    return true;
                }
            }
       }, 1000);
    }

    fd.control('SPDataTable1').$on('change', function () {
        validateListOrLibrary();
    });

    validateListOrLibrary();
   validateListOrLibrary)
});

Dear @ixxxl,
Yes, that's because you've modified the code, it's different from what I've shown.

return true; statement should come after all items are checked, after the for loop, not inside of it, and not in the else condition, or it will return true as soon as it finds first item which is okay.

1 Like

@Nikita_Kurguzov
Thank you!! for now it works good!

1 Like