x First time here? Check out the FAQ

Come work for Umbraco - The Umbraco HQ are hiring Project managers, .NET developers and DevOps people!

  • Avatar245posts267karma

    filter nodes

    hetaurhet started this topic February 27, 2012 @ 04:08

    I want to filter child nodes by node name... say I have string variable to which I will compare node name...

    so how can I do that in razor?


  • Avatar694posts835karma
    Comment with ID: 109200
    Rodion Novoselov posted this reply February 27, 2012 @ 04:32

    Hi. Try something like:

    var nodes = Model.Children.Where("Name = @0", name);

     


  • Avatar245posts267karma
    Comment with ID: 109262
    hetaurhet posted this reply February 28, 2012 @ 06:58

    ok thanks I used as u said...

    now my code is like .... it shows error as Error loading MacroEngine script (file: CompetitionDropDown.cshtml) when page is run. no error on saving razor file.

    @using umbraco.BusinessLogic;
    @using umbraco.MacroEngines;
    @inherits umbraco.MacroEngines.DynamicNode
    @inherits umbraco.MacroEngines.DynamicNodeContext
    @{
    string compval="";

       <select id="ddlcompetition">
                @{
                int i=1;
                foreach (var item in @Model.Children.OrderBy("CreateDate desc"))
                { if(i==1)
                  {
                    @: <option value="@item.Name" selected="selected">@item.Name</option>
                    compval=@item.Name;
                  }
                  else
                  {
                    @: <option value="@item.Name">@item.Name</option>
                  }
                 i++;
                }
                
                }
       </select>
     

       var node = Model.Children.Where("Name = @0", compval);
      
       if(@node.Count == 1)
       {
     
         dynamic link = Library.MediaById(@node.competitionThumbImage);
         dynamic link2 = Library.MediaById(@node.competitionImage);
       }

       
    }


  • Avatar694posts835karma
    Comment with ID: 109357
    Rodion Novoselov posted this reply February 28, 2012 @ 03:49

    Hi. Is this what you want?

    @using umbraco.BusinessLogic;
    @using umbraco.MacroEngines;
    @inherits umbraco.MacroEngines.DynamicNode
    @inherits umbraco.MacroEngines.DynamicNodeContext

    @{
      var children = Model.Children.OrderBy("CreateDate desc");
      var first = children.First();
      var other = children.Skip(1);
    }

    <select id="ddlcompetition">
      <option value="@first.Name" selected="selected">
        @first.Name
      </option>
      @foreach(var item in other) {
         <option value="@item.Name">
            @item.Name
         </option>                          
      }
    </select>

    @{
      var node = children.Where("Name = @0", first.Name).FirstOrDefault();
      if(node != null) {
        var link = Library.MediaById(n.competitionThumbImage);
        var link2 = Library.MediaById(n.competitionImage);
      }
    }

  • Avatar245posts267karma
    Comment with ID: 109478
    hetaurhet posted this reply February 29, 2012 @ 12:22

    yes... I wanted this.. but now I need to compare in where condition by ignoring case in below statement... means toLower() to be used for Name property value and first.Name value.

    varnode =children.Where("Name = @0", first.Name).FirstOrDefault();


  • Jeroen Breuer posted this reply February 29, 2012 @ 12:45

    Be aware that .where has some performance problems: http://our.umbraco.org/forum/developers/razor/28479-Razor-menu-performance-(v4)

    Jeroen


  • Avatar694posts835karma
    Comment with ID: 109513
    Rodion Novoselov posted this reply February 29, 2012 @ 02:57

    Changing

    ...Where("Name == @0", first.Name)...

    to 

    ...Where("Name.ToLower() == @0", first.Name.ToLower())...

    should work, I suppose.

    (Not sure absolutely - there're limitations on using methods in dynamic LINQ, but as far as I remember for strings it's possible).


  • Avatar245posts267karma
    Comment with ID: 109647
    hetaurhet posted this reply March 1, 2012 @ 04:46

    thank you ... it works... but now I need one more help... my razor code is as follows...

    @using umbraco.BusinessLogic;
    @using umbraco.MacroEngines;
    @inherits umbraco.MacroEngines.DynamicNode
    @inherits umbraco.MacroEngines.DynamicNodeContext
    @{
        //get selected options from Request.PathInfo

        
        string comp = (Request.QueryString["comp"]);
        
        string baseurl = Request.Url.GetLeftPart(UriPartial.Path);
    }
    <script type="text/javascript">
        String.prototype.LTrim = function()
          {
          return this.replace(/(^\s*)/g, "");
          }
                              
        $(document).ready(function () {

            $('#ddlcompetition').val('@comp');
            
            $("#ddlcompetition").change(function () { onSelectChange('@baseurl'); });


        });

        function onSelectChange(href) {

            var comp = $("#ddlcompetition option:selected").text().LTrim();
            $(window.location).attr('href', href + "?" + "comp=" + comp.toLowerCase());

            
        }
    </script>
    @{
      var children = Model.Children.OrderBy("CreateDate desc");
      var first = children.First();
      var other = children.Skip(1);
    }

    <select id="ddlcompetition" class="right">
      <option value="@first.Name" selected="selected">
        @first.Name
      </option>
      @foreach(var item in other)
     {
         <option value="@item.Name">
            @item.Name
         </option>                          
      }
    </select>
    <br />
    <div class="clear"></div>

    ... in above code you can see that I am looking for Query string parameter named 'comp'  and then setting value of 'select' input to querystring parameter value. But problem is that while passing parameter I convert it to lower case but dropdown contains value with uper case and lower case letters. So, I need to set value by comparing to_lower(parameter value ) and to_lower (select input value) ... how can I do that in script above?


Please login or Sign up To post replies