Copied to clipboard

Flag this post as spam?

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


  • Kasper Dyrvig 246 posts 379 karma points
    Jun 08, 2012 @ 14:42
    Kasper Dyrvig
    0

    Select distinkt nodeId's from multi-node tree picker

    How do I select only unique nodeId's from a multi-node picker?

    So far, I got this:

    string hotelNodeString "";
    foreach(var season in Model.Children.Where("Visible"))
    {
     hotelNodeString hotelNodeString season.seasonHotels;
     if(!season.IsLast())
     {
      hotelNodeString hotelNodeString ",";
     }
    }
    string[accommodations hotelNodeString.Split(',');

    And then...

    @foreach(var accommodationItem in accommodations)
    {
     var accommodationNode Library.NodeById(accommodationItem.AsInt());
     var accommodationSelected accommodationNode.Id == qId "class=selected" "";
     <href="?tab=@q&a=@accommodationNode.Id" @accommodationSelected>
      @accommodationNode.hotelTitle
     </a>
    }

    It is Umbraco 4.7.2.

  • Funka! 398 posts 661 karma points
    Jun 09, 2012 @ 03:09
    Funka!
    0

    Instead of building up a comma-separated string, and then splitting that up into an array, instead perhaps consider building up a List<string> which you can check for dupes before actually adding. Here's a rough idea (untested) on what I mean by this:

    List<string> hotelNodes = new List<string>();
    foreach(var season in Model.Children.Where("Visible"))
    {
      if (!hotelNodes.Contains(season.seasonHotels))
      {
        hotelNodes.Add(season.seasonHotels);
      }
    }
    string[] accommodations = hotelNodes.ToArray<string>();
    

    This is assuming the type of season.seasonHotels is actually a string, instead of a DynamicXml or something, but if so, just add the appropriate casting where needed. Despite this, however, there are two things I'm curious about:

    First, my experience with the uComponents Multi-Node Tree Picker has been that it doesn't allow me to select duplicates, even when I try.

    Second, I'm thinking you could combine your two separate code blocks/loops into a single comprehensive loop? That is, don't bother with building up an array of nodes and then later iterate through them --- just loop once the first time and do your magic right there?

    Best of luck to you!

  • Kasper Dyrvig 246 posts 379 karma points
    Jun 11, 2012 @ 11:18
    Kasper Dyrvig
    0

    Hi again

    I have played around with the List<>. I wasn't able to make your suggestion work just like that. I have chaged the multi-node tree picker to save the data as XML to see if that helped enything. Right now I'm testing this:

    List<stringhotelIds new List<string>();
    foreach (var in Model.Children)
    {
     if(s.seasonHotels != null)
     {
      foreach (var in s.seasonHotels.InnerText.ToString())
      {
       hotelIds.Add(h.ToString());
      }
     }
    }
    <ul>
     @foreach(var hid in hotelIds)
     {
      <li>@hid</li>
     }
    </ul>

    It gives me this:

    1
    1
    2
    2
    1
    1
    2
    2
    1
    1
    6
    2

    Not exatly what I want... Any ideas?

  • Kasper Dyrvig 246 posts 379 karma points
    Jun 11, 2012 @ 11:56
    Kasper Dyrvig
    0

    If I do it like this, it works. I have combined my original code with some of Funka's suggestion.

    string hotelNodeString = "";
    foreach(var season in Model.Children.Where("Visible"))
    {
     hotelNodeString = hotelNodeString + season.seasonHotels;
     if(!season.IsLast())
     {
      hotelNodeString = hotelNodeString + ",";
     }
    }
    string[] accommodations = hotelNodeString.Split(',');

    List<string> hotels = new List<string>();
    foreach(string st in accommodations)
    {
     if (!hotels.Contains(st))
     {
      hotels.Add(st);
     }
    }
  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies