Looping/Counting items in Related Items
-
- Posts: 18
- Joined: Wed May 17, 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:
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.
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(...) }
Any help is appreciated,
Thank you.
- Dmitry Kozlov
- Site Admin
- Posts: 1524
- Joined: Thu Jun 07, 2012
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(...) }
You're using incorrect syntax for iterating through an array in JavaScript. Try this:
for (var i = 0; i < list.length; i++) { console.log(...) }
-
- Posts: 18
- Joined: Wed May 17, 2017
Thanks for the reply again Dmitry. I believe I have tried 3 different types of for loops:
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
for (i in list) { ... }
list.forEach(function(item) { ... });
for (i = 0; i < list.length; i++) { ... }
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);
- Dmitry Kozlov
- Site Admin
- Posts: 1524
- Joined: Thu Jun 07, 2012
No, the code works correct. See below:
-
- Posts: 18
- Joined: Wed May 17, 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:
- Nikita Kurguzov
- Posts: 889
- Joined: Mon Jul 03, 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:
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
Just run this in the console:
Code: Select all
fd.relatedItems().length
Cheers
-
- Posts: 18
- Joined: Wed May 17, 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: 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. Maybe this helps a bit,
Thanks again.
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: 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. Maybe this helps a bit,
Thanks again.
- Nikita Kurguzov
- Posts: 889
- Joined: Mon Jul 03, 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? 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.
Can you tell me about your Filtering options in Data Source Editor? 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
-
- Posts: 18
- Joined: Wed May 17, 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:
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:
About the filtering though it is currently set as:
- Nikita Kurguzov
- Posts: 889
- Joined: Mon Jul 03, 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
-
- Information
-
Who is online
Users browsing this forum: No registered users and 5 guests