Looping/Counting items in Related Items

Discussions about Forms Designer for SharePoint 2013 / 2016 and Office 365.
joss.wilde
Posts: 18
Joined: Wed May 17, 2017

28 Jun 2017

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.

User avatar
Dmitry Kozlov
Site Admin
Posts: 1524
Joined: Thu Jun 07, 2012

29 Jun 2017

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(...) }

joss.wilde
Posts: 18
Joined: Wed May 17, 2017

29 Jun 2017

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);

User avatar
Dmitry Kozlov
Site Admin
Posts: 1524
Joined: Thu Jun 07, 2012

30 Jun 2017

No, the code works correct. See below:
RelatedItemsLength.png
RelatedItemsLength.png (23.97 KiB) Viewed 3137 times

joss.wilde
Posts: 18
Joined: Wed May 17, 2017

30 Jun 2017

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 3134 times
codeoutput.PNG
codeoutput.PNG (24.8 KiB) Viewed 3134 times

User avatar
Nikita Kurguzov
Posts: 889
Joined: Mon Jul 03, 2017

03 Jul 2017

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 3118 times
Cheers

joss.wilde
Posts: 18
Joined: Wed May 17, 2017

04 Jul 2017

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 3112 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 3112 times
Maybe this helps a bit,
Thanks again.

User avatar
Nikita Kurguzov
Posts: 889
Joined: Mon Jul 03, 2017

04 Jul 2017

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 3110 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.
Cheers

joss.wilde
Posts: 18
Joined: Wed May 17, 2017

04 Jul 2017

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 3108 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 3108 times
About the filtering though it is currently set as:
filtering.PNG
filtering.PNG (45.01 KiB) Viewed 3108 times

User avatar
Nikita Kurguzov
Posts: 889
Joined: Mon Jul 03, 2017

04 Jul 2017

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.
Cheers

Locked
  • Information
  • Who is online

    Users browsing this forum: No registered users and 5 guests