Copied to clipboard

Flag this post as spam?

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


  • Eran Meir 401 posts 543 karma points
    Jun 07, 2011 @ 09:53
    Eran Meir
    0

    Get Next node where visible

    hey, i'm trying to get Model.Next(), its going ok but how can i check if the node is also visible ?
    my lucky shot was trying Model.Next().Where("Visible") but it ain't working. anyone can help me please?

  • Steven Newstead 62 posts 103 karma points
    Jun 09, 2011 @ 09:48
    Steven Newstead
    0

    Should be about right maybe try flipping the linq so:

    Model.Where("Visible").Next()

     

  • Steven Newstead 62 posts 103 karma points
    Jun 09, 2011 @ 09:49
    Steven Newstead
    0

    Or else do:

    var items = Model.Children.Where("Visible")

  • Eran Meir 401 posts 543 karma points
    Jun 09, 2011 @ 09:54
    Eran Meir
    0

    the first line of code doesn't makes any sense to me, the 2nd one just get all the items that are visible. not the next node.

  • Steven Newstead 62 posts 103 karma points
    Jun 09, 2011 @ 10:49
    Steven Newstead
    0

    I noticed that it was nonsense as soon as I pressed submit but didn't know how to retract it, lack of caffiene I guess. You could do try something like this, not very nice though:

     

        var node = Model.Next();
        while (node != null)
        {        
            if (node.Visible)
            {
                break;
            }
            node = node.Next();
        }

  • Eran Meir 401 posts 543 karma points
    Jun 09, 2011 @ 10:52
    Eran Meir
    0

    i thought about, but what if the next node is also invisible, then i thought about making a for loop until node is null or visible, but i'm looking for a simpler solution.

  • Steven Newstead 62 posts 103 karma points
    Jun 09, 2011 @ 10:54
    Steven Newstead
    0

    In the code above if the next node is invisible it will keep looking until it either finds one that isn't or the finds that the node is null, in which case there isn't one.

    If you find a nicer solution do let me know!

  • Eran Meir 401 posts 543 karma points
    Jun 09, 2011 @ 10:57
    Eran Meir
    0

    oh somehow i saw "if" instead of a "while" :)

  • Steven Newstead 62 posts 103 karma points
    Jun 09, 2011 @ 10:58
    Steven Newstead
    0

    hee hee, early for both of us I guess! Keep me posted.

    Cheers,

    Steve

  • Anthony Dang 1404 posts 2558 karma points MVP 3x c-trib
    Jun 09, 2011 @ 12:22
    Anthony Dang
    1

     

    Steven your code should work, but I had problems with .Next() not really working for me. Then again I was trying it on DynamicNode.

    Anyway I ended up doing it like this. It is quick and dirty but it works.

     

        
            /// <summary>
            /// Gets the post immediately following the current one.
            /// </summary>
            /// <param name="current"></param>
            /// <returns></returns>
            public static DynamicNode GetNextPost(DynamicNode current)
            {
                // get siblings
                List<DynamicNode> siblings = GetPostSiblings(current);

                // get index of current
                int index = GetIndexOf(siblings, current);

                if (index == siblings.Count - 1 || index == -1)
                {
                    // index out of range
                    return null;
                }

                // return next
                return siblings[index + 1];
            }


          



            /// <summary>
            /// Gets all posts which are not hidden by umbracoNaviHide and orders them by reverse date.
            /// </summary>
            /// <param name="current"></param>
            /// <returns></returns>
            private static List<DynamicNode> GetPostSiblings(DynamicNode current)
            {
                return current.AncestorOrSelf("uBlogsyFolderBlog")
                             .Descendants("uBlogsyPost")
                             .Items
                             .Where(x => x.GetProperty("umbracoNaviHide").Value != "1" || x.Id == current.Id)
                             .OrderByDescending(x => x.GetProperty("uBlogsyPostDate").Value)
                             .ToList();
            }
           
       


       

            /// <summary>
            /// Gets the index of the current post in the list of siblings.
            /// </summary>
            /// <param name="siblings"></param>
            /// <param name="current"></param>
            /// <returns></returns>
            private static int GetIndexOf(List<DynamicNode> siblings, DynamicNode current)
            {
                for (int i = 0; i < siblings.Count; i++)
                {
                    if (siblings[i].Id == current.Id)
                    {
                        return i;
                    }
                }
                return -1; // some crazy error!
            }



  • Steven Newstead 62 posts 103 karma points
    Jun 09, 2011 @ 12:23
    Steven Newstead
    0

    Cool, thanks for sharing

  • Eran Meir 401 posts 543 karma points
    Jun 09, 2011 @ 12:34
    Eran Meir
    0

    thank you both ! i'll go with anthony solution :)

Please Sign in or register to post replies

Write your reply to:

Draft