Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Gary 80 posts 377 karma points
    Nov 23, 2016 @ 17:12
    Gary
    0

    Child Multiple Text Boxes

    Hi,

    I have been trying to us the Multiple Textbox property to list values from child pages.

    I have tried different ways of doing this and by using the documentation:

    https://our.umbraco.org/documentation/Getting-Started/Backoffice/Property-Editors/Built-in-Property-Editors/Multiple-Textbox

    but I am yet to get a resolution. Could anyone help?

    Kind Regards,

    Gary

  • Steve Morgan 1349 posts 4459 karma points c-trib
    Nov 23, 2016 @ 18:02
    Steve Morgan
    100

    Hi Gary,

    Nice to see more Manchester based Umbraco devs :)

    Something like this will work (replace "multiTextBoxes" with the name of your multple text box.. note how they are called "Repeatable Text Fields" in Umb 7 backend!

    @{ 
        var childNodes = Model.Content.Children();
    
        foreach (var curChild in childNodes)
        {
            <h2>@curChild.Name</h2>
            if (curChild.HasValue("multiTextBoxes") && curChild.GetPropertyValue<string[]>("multiTextBoxes").Length > 0)
            {
                <ul>
                    @foreach (var item in curChild.GetPropertyValue<string[]>("multiTextBoxes"))
                    {
                        // there is a bug where a certain republish event causes new line chars to be double output. 
                        // check for an empty str until the bug is fixed
                        if (item != "")
                        {
                            <li>@item</li>
                        }
                    }
                </ul>
            }
        }
    }
    
  • Gary 80 posts 377 karma points
    Nov 23, 2016 @ 18:35
    Gary
    0

    Hi Steve,

    This is great, gives me exactly what I wanted! I knew I wasn't far off.

    Talking about Manchester, hope ill see you at the meet up next week.

    Thanks again!

    Gary

  • Steve Morgan 1349 posts 4459 karma points c-trib
    Nov 23, 2016 @ 19:57
    Steve Morgan
    0

    I'll be there - come say hi!

    Glad to have helped.

    Steve

  • Gary 80 posts 377 karma points
    Dec 01, 2016 @ 12:59
    Gary
    0

    Hi Steve,

    I am trying to get a list of distinct "keywords" (As above). I want to group all results by there keyword. As i have done with my news page (Image Below).

    enter image description here

    I have only managed at the moment to retrieve a list of Keywords but this retrieves distinct keywords on that individual page and doesn't group them from all pages, if that makes sense.

    Could you possibly give me any direction please?

    Thanks again!

  • Steve Morgan 1349 posts 4459 karma points c-trib
    Dec 01, 2016 @ 14:05
    Steve Morgan
    0

    Hi,

    Not quite sure I follow what you're doing - so check my workings! This repeatable text fields field on your document is being used as a tag? What you want is to interrogate all pages and group and count the number times a particular value exists?

    This is perfectly possible - I'd advice you ensure it's a cached macro (so each page load doesn't do this on each page load!) and I'd question the use of these multiple text fields.. are you happy that your editors are going to not miskey these? Wouldn't the tags field be better here?

    enter image description here

  • Gary 80 posts 377 karma points
    Dec 01, 2016 @ 14:44
    Gary
    0

    Yeah exactly. So i currently have a list of all "Keywords" that are stored within each child page. However, i want to group all values that are the same and then count how many Child items contain that keyword. I have done this before, but the difference is, i used a drop down box value instead of the repeatable text boxes.

    Just the methods i am trying doesn't seem to bring me back the correct values. I would appreciate any help :)

    I do believe that the user "could" mistype but I have assurances that this will be okay for this section. I may trial the tags as i think they are represented nicer than the repeatable text boxes.

  • Steve Morgan 1349 posts 4459 karma points c-trib
    Dec 01, 2016 @ 15:19
    Steve Morgan
    1

    This do you?

    Remember to cache this!

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    @using System.Collections
    @{
        var rootNode = Model.Content.AncestorOrSelf(1);
        Dictionary<string, int> tagsDict = new Dictionary<string, int>();
    
        // remember to change "repeatingValues" to that of your doctype field 
        foreach (var descendantNode in rootNode.DescendantsOrSelf().Where(x => x.HasProperty("repeatingValues")))
        {
            var tags = descendantNode.GetPropertyValue<string[]>("repeatingValues").Length > 0 ? descendantNode.GetPropertyValue<string[]>("repeatingValues") : new string[0];
    
            foreach (var curTag in tags)
            {
                // add to the dictionary
                if(tagsDict.ContainsKey(curTag))
                {
                    tagsDict[curTag] += 1;
                }
                else
                {
                    tagsDict.Add(curTag, 1);
                }
            }
    
        }
    
        foreach (var curTag in tagsDict.OrderBy(x => x.Key))
        {
            <p>@curTag.Key (@curTag.Value)</p>
        }
    }
    
  • Gary 80 posts 377 karma points
    Dec 01, 2016 @ 16:02
    Gary
    0

    Great! After tweaking a little bit to add my styling etc, works exactly as i wanted. Thank you again for this, Steve.

    Would there more than one way to do this? Or is this best practice when dealing with this kind of data?

    I love this community more and more everyday!

    Kind Regards,

    Gary

  • Steve Morgan 1349 posts 4459 karma points c-trib
    Dec 01, 2016 @ 16:12
    Steve Morgan
    1

    In short - it's not really best practise - you should try to avoid traversing the entire content tree as much as possible. That's why I empathised caching.

    You should definitely target that code to change from the site root node to just the root node of the News Articles or whatever contains those documents that you need to track. It might help to filter by doc type too so that fewer iterations are required.

    That said - if you need this sort of functionality there is always going to be a performance cost but you mitigate it with caching / node targeting and try to keep things sensible.

    I've seen sites where people traverse the entire site structure to find a "include in menu" checkbox to build their menus. This is obviously bad and more easily solved with a multi-node content picker.

    Often where sites are running slowly it's too many of these sorts of things running.. but caching and being aware will keep your site snappy.

    So in short - please update that code to only include the node it needs (unless it is a site wide sweep!?).

    HTH

    Steve

Please Sign in or register to post replies

Write your reply to:

Draft