Page 1 of 3

Looping/Counting items in Related Items

Posted: 28 Jun 2017
by joss.wilde
Hello again all,

I understand it's quite possible to loop through and sum/count items in a related items control by something like this:

Code: Select all

var list = fd.relatedItems(0).data('ctx').ListData.Row;
for (i in list) { console.log(...) }
However when I do this, no matter what kind of for loop I use, it for some reason will count each item twice, which is no good if I was summing up the value in one of the columns. Just wondering if there is a better way of doing this other than a REST query, or I am doing it wrong? Also if you delete a row/item through the control it will still count the item in the loop.

Any help is appreciated,
Thank you.

Re: Looping/Counting items in Related Items

Posted: 29 Jun 2017
by Dmitry Kozlov
Hi,
You're using incorrect syntax for iterating through an array in JavaScript. Try this:
for (var i = 0; i < list.length; i++) { console.log(...) }

Re: Looping/Counting items in Related Items

Posted: 29 Jun 2017
by joss.wilde
Thanks for the reply again Dmitry. I believe I have tried 3 different types of for loops:

Code: Select all

for (i in list) { ... }
list.forEach(function(item) { ... });
for (i = 0; i < list.length; i++) { ... }
However I can certainly check the last one again as I don't think I declared the variable in the loop.

I should also mention if you do a check on the relatedItems length it will return double the number of rows/items in it e.g.

Code: Select all

var list = fd.relatedItems(0).data('ctx').ListData.Row
console.log(list.length);

Re: Looping/Counting items in Related Items

Posted: 30 Jun 2017
by Dmitry Kozlov
No, the code works correct. See below:
RelatedItemsLength.png
RelatedItemsLength.png (23.97 KiB) Viewed 3143 times

Re: Looping/Counting items in Related Items

Posted: 30 Jun 2017
by joss.wilde
Is it something I am doing differently then? I can't replicate how you got that output as entering fd.relatedItems... via console returns that 'fd is undefined'. When the loop/code is tested via javascript function it seems to to do the below:
riList.PNG
riList.PNG (5.57 KiB) Viewed 3140 times
codeoutput.PNG
codeoutput.PNG (24.8 KiB) Viewed 3140 times

Re: Looping/Counting items in Related Items

Posted: 03 Jul 2017
by Nikita Kurguzov
One possibility is that there is another Related Items element that interferes with the code. Can you check it just to be sure?
Just run this in the console:

Code: Select all

fd.relatedItems().length
But that seems rather strange. Can you tell us your version of Forms Designer? It's right at the bottom of the screen at the right corner
VersionNumber.png
VersionNumber.png (2.97 KiB) Viewed 3124 times

Re: Looping/Counting items in Related Items

Posted: 04 Jul 2017
by joss.wilde
Thank you for looking into this too Nikita,
I agree it is odd as there is not much complicated with the code at least from my side. I have done the checks you suggested and got the below results:
overview.PNG
overview.PNG (52.23 KiB) Viewed 3118 times
However, on more testing I have discovered this is only an issue on the new item form or when adding items on the edit form. When I run the above code when in the edit form of an existing item it will return the correct length and only count each item once. Anything new I add it will count that twice and will still count it twice even after it has been deleted from within the form.
The below screenshot demonstrates this, the first test is when I initially open the edit form, the second when I delete an item and third when I then add a new item.
edit test.PNG
edit test.PNG (46.18 KiB) Viewed 3118 times
Maybe this helps a bit,
Thanks again.

Re: Looping/Counting items in Related Items

Posted: 04 Jul 2017
by Nikita Kurguzov
Thank you for the report. Unfortunately, I was unable to replicate your issue, so I am still puzzled by this behavior.

Can you tell me about your Filtering options in Data Source Editor?
RelatedItems.Filtering.jpg
RelatedItems.Filtering.jpg (33.06 KiB) Viewed 3116 times
Another possibility - weird browser behavior. Have you tried testing this issue in Chrome or Edge? I would love to know if there is any difference in the results or behavior.

Re: Looping/Counting items in Related Items

Posted: 04 Jul 2017
by joss.wilde
I do wonder if it could be a browser thing as I am using Firefox, which doesnt always work well with Sharepoint I know. However for some reason recently I am having an issue with all my forms for this list as it is no longer picking up the majority of the columns/fields and is not displaying in the form:
blank.PNG
blank.PNG (26.76 KiB) Viewed 3114 times
This likely needs a separate post and may be something to do with a workflow I created that added a column to the list. In Firefox it will open the form but with missing elements. However, in IE or Edge it will not load the form properly and report this in the console:
IE errors.PNG
IE errors.PNG (17.78 KiB) Viewed 3114 times
About the filtering though it is currently set as:
filtering.PNG
filtering.PNG (45.01 KiB) Viewed 3114 times

Re: Looping/Counting items in Related Items

Posted: 04 Jul 2017
by Nikita Kurguzov
Joss, can you please show us full console log from IE? Especially, the first error in the list is important and it's hidden on your screenshot. It might help us determine the cause of your issues.