Copied to clipboard

Flag this post as spam?

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


  • John Halsey 59 posts 220 karma points
    Mar 12, 2014 @ 17:10
    John Halsey
    0

    How to count number of items in uComponents Multi URL Picker

    Hi have a Multi URL pickers set up on a doc type of "Product". The point of the picker is to assign similar or related products to this product and have them display at the bottom of the page.

    The alias of the multi url picker is "relatedProducts". The below code works perfectly for checking the item values in the multi pickers and displays the image of the related products on the page, but what I also what to do is count how many items there are before the foreach loop because I dont want the 2 divs and the h1 tag to be diplayed if there are no related products. Can anyone advise how best to do this.

    @{
    
            var product = Model.AncestorOrSelf("Product");
            if(product.HasValue("relatedProducts"))
            {   
    
    
                    <div class="width1680 popularProducts">
                    <div class="width960">
                    <h1 class="centerText">RELATED PRODUCTS</h1>
                    @foreach (var urlPicker in Model.AncestorOrSelf("Product").relatedProducts.Items)
                    {
                        if(urlPicker != null)
                        {
                            var linkTarget = (urlPicker.NewWindow) ? "target=\"_blank\"" : String.Empty;
                            var nodeId = urlPicker.NodeId;
                            var image = Library.NodeById(nodeId);
                            <div class="floatProducts">
                                <div class="sqrProductsImage">
                                    <a href="@urlPicker.Url" @Html.Raw(linkTarget)>
                                        <img src="@image.Media("mainImage","umbracoFile")" />
                                    </a>
                                </div>
                                <a href="@urlPicker.Url">           
                                    <div class="overlay"></div>
                                    <div class="ic_caption">
                                    <h2 class="centerText">@image.Name</h2> 
                                    </div>
                                </a>    
                            </div>
    
                        }
                    }
                    <div class="clearLeft"></div>
                    </div>
                    </div>
                }
            }
    
        }
    

    I have tried...

    @product.relatedProducts.Items.Count() & @product.relatedProducts.Count() with no luck.

    Any help is appreciated.

  • Fuji Kusaka 2203 posts 4220 karma points
    Mar 12, 2014 @ 18:03
    Fuji Kusaka
    0

    Try having a look at this post

    Have you also tried @product.relatedProducts.Count() > 2 ?  

  • John Halsey 59 posts 220 karma points
    Mar 12, 2014 @ 18:22
    John Halsey
    0

    Hi Fuji,

    @product.relatedProducts.Count() does nothing. The script saves but I get an error on the web page just saying there was an error loading the macro.

    The link you provided is for the Multi Node Tree Picker which is different to what I'm using, also it doesn't mention how you do any counting. :(

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Mar 12, 2014 @ 18:46
    Jeavon Leopold
    0

    Hi John,

    You should use the uComponents model deserialiser uComponents.Core.DataTypes.MultiUrlPicker.Dto.MultiUrlPickerState.Deserialize

    Check out this pastebin for a great sample

    Jeavon

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Mar 12, 2014 @ 18:47
    Jeavon Leopold
    0

    You can see there is a Count() check in there already

  • John Halsey 59 posts 220 karma points
    Mar 13, 2014 @ 10:46
    John Halsey
    0

    Hi Jeavon, thanks for your reply.

    I have tried implementing the code on the pastebin example but got this error message:

    The type or namespace name 'DataTypes' does not exist in the namespace 'uComponents.Core' (are you missing an assembly reference?)

    I have tried adding the below as a namespace but got the same error.

    using uComponents.Core.DataTypes.UrlPicker;
    

    Any ideas?

  • Andy Butland 422 posts 2334 karma points MVP 4x hq c-trib
    Mar 13, 2014 @ 14:22
    Andy Butland
    1

    The using statement in my project is:

    using uComponents.DataTypes.MultiUrlPicker.Dto

    Maybe there have been different versions, but probably this is the one you need.  With that in place you should be able to reference:

    MultiUrlPickerState.Deserialize()

    Andy

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Mar 13, 2014 @ 14:25
    Jeavon Leopold
    0

    Hi John,

    What version of uComponents do you have?

    Jeavon

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Mar 13, 2014 @ 14:26
    Jeavon Leopold
    0

    @Andy is right, it did change between versions somewhere, I think v5

  • John Halsey 59 posts 220 karma points
    Mar 14, 2014 @ 17:40
    John Halsey
    0

    I'm using uComponents v6.0.0

    OK, so I have edited my script and the on save error has gone but it still says Error loading macro on the page.

    Here is the first few lines of my code.

    @using umbraco.MacroEngines
    using uComponents.DataTypes.MultiUrlPicker.Dto
    @inherits umbraco.MacroEngines.DynamicNodeContext
    
    
    @{
        uComponents.DataTypes.MultiUrlPicker.Dto.MultiUrlPickerState links = null;  
        var product = Model.AncestorOrSelf("Product");
        if(product.HasValue("relatedProducts"))
        {   
            @* Check if the current product has any related products at all*@
            links = uComponents.DataTypes.MultiUrlPicker.Dto.MultiUrlPickerState.Deserialize(Model.relatedProducts);
            if (links != null && links.Items.Count() > 0)
            { ...
    

    I really have no clue where it doesn't work.

    Thanks again

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Mar 14, 2014 @ 18:29
    Jeavon Leopold
    0

    The only issue I see is the missing @ before the using

    @using umbraco.MacroEngines
    @using uComponents.DataTypes.MultiUrlPicker.Dto
    @inherits umbraco.MacroEngines.DynamicNodeContext
    
  • Charles Afford 1163 posts 1709 karma points
    Mar 16, 2014 @ 14:35
    Charles Afford
    0

    Have you got visual studio or another IDE that can give you intellisense?

    Its gonna be hard trying to write razor from Umbraco backoffice.

    yea the @ is missing.

    If you paste the full mark up i will put in in Visual Studio and see what resharper makes of it :).

    Charlie

     

  • John Halsey 59 posts 220 karma points
    Mar 17, 2014 @ 10:28
    John Halsey
    0

    Thanks for your comments, all.

    I have added the missing @ to no avail.

    I have Visual Web Developer 2010 installed and have been using that in some cases to write the scripts, but it doesn't show any errors, and the whole site builds without errors.

    Here is my full script.

    @using umbraco.MacroEngines
    @using uComponents.DataTypes.MultiUrlPicker.Dto
    @inherits umbraco.MacroEngines.DynamicNodeContext
    
    
    @{
        uComponents.DataTypes.MultiUrlPicker.Dto.MultiUrlPickerState links = null;  
        var product = Model.AncestorOrSelf("Product");
        if(product.HasValue("relatedProducts"))
        {   
            @* Check if the current product has any related products at all*@
            links = uComponents.DataTypes.MultiUrlPicker.Dto.MultiUrlPickerState.Deserialize(Model.relatedProducts);
            if (links != null && links.Items.Count() > 0)
            {       
                    <div class="width1680 popularProducts">
                    <div class="width960">
                    <h1 class="centerText">RELATED PRODUCTS</h1>
                    @foreach (var urlPicker in Model.AncestorOrSelf("Product").relatedProducts.Items)
                    {
    
                        if(urlPicker != null)
                        {
                            var linkTarget = (urlPicker.NewWindow) ? "target=\"_blank\"" : String.Empty;
                            var nodeId = urlPicker.NodeId;
                            var image = Library.NodeById(nodeId);
                            <div class="floatProducts">
                                <div class="sqrProductsImage">
                                    <a href="@urlPicker.Url" @Html.Raw(linkTarget)>
                                        <img src="@image.Media("mainImage","umbracoFile")" />
                                    </a>
                                </div>
                                <a href="@urlPicker.Url">           
                                    <div class="overlay"></div>
                                    <div class="ic_caption">
                                    <h2 class="centerText">@image.Name</h2> 
                                    </div>
                                </a>    
                            </div>
    
                        }
                    }
                    <div class="clearLeft"></div>
                    </div>
                    </div>
            }
        }
    
    }
    
  • John Halsey 59 posts 220 karma points
    Mar 19, 2014 @ 13:11
    John Halsey
    0

    Does anyone have any thoughts on this at all please?

  • John Halsey 59 posts 220 karma points
    Mar 20, 2014 @ 13:31
    John Halsey
    100

    I've solved this in the end.

    I couldnt figure out how to count the items at all without it erroring, so thought of something else which probably isn't very pretty and if there a lot of related products might take a little while, but for my purposes it should be fine.

    @{
        dynamic product = Model.AncestorOrSelf("Product");
        if (product.HasValue("relatedLinks"))
        {
            var count = 0;
            foreach (var urlPicker in Model.relatedLinks.Items)
            {
                count++;   
            }
            if(count > 0)
            {
                    <div class="width1680 popularProducts">
                    <div class="width960">
                    <h1 class="centerText">RELATED PRODUCTS</h1>
                    @foreach (var urlPicker in Model.relatedLinks.Items)
                    {
    
                            var linkTarget = (urlPicker.NewWindow) ? "target=\"_blank\"" : String.Empty;
                            var nodeId = urlPicker.NodeId;
                            var image = Library.NodeById(nodeId);
                            <div class="floatProducts">
                                <div class="sqrProductsImage">
                                    <a href="@urlPicker.Url" @Html.Raw(linkTarget)>
                                        <img src="@image.Media("mainImage","umbracoFile")" alt="@image.Media("mainImage","altText")"/>
                                    </a>
                                </div>
                                <a href="@urlPicker.Url">           
                                    <div class="overlay"></div>
                                    <div class="ic_caption">
                                    <h2 class="centerText">@image.Name</h2> 
                                    </div>
                                </a>    
                            </div>
    
                    }
                    <div class="clearLeft"></div>
                    </div>
                    </div>
             }
        }
    
    }
    
  • Charles Afford 1163 posts 1709 karma points
    Mar 23, 2014 @ 20:19
    Charles Afford
    0

    Is the MultiUrlPickerState object returning an Enum?

    so links will = a collection of MultiUrlPickerStates

    so you should be able to do links.count, which will give the total number of MultiUrlPickerState object in the collection. :).

    Charlie

Please Sign in or register to post replies

Write your reply to:

Draft