Copied to clipboard

Flag this post as spam?

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


  • Stephanie Nicolaou 33 posts 91 karma points
    Mar 09, 2015 @ 20:47
    Stephanie Nicolaou
    0

    Pass variable on click of link to function within template

    Hi all,

    I'm quite new to umbraco so I need to know how the fundamentals with functions work. I would like to know if I can pass a variable to a function when an unordered list link is clicked, assign it to an existing variable and then return that updated variable back to the body of the page.

    So far I can assign a variable for each list item and I have tried to pass that value to a function with the hyperlink reference tag, a href:

    [code]
    foreach (dynamic secondPage in page.Children){
    var itemtest = secondPage.Name;                               
    <li><a href="@afunction(itemtest)">@secondPage.Name</a></li>[/code]

    In the function I am simply trying to assign the itemtest variable to an existing variable called 'anothertest' however when trying to display this in the body there is no result.

    [code]
    @functions {   
    public string anothertest {get; set;}
    public string afunction(string b) {   
    @anothertest = @b;                   
    return anothertest;
        }
    }
    [/code]

    If someone could guide me on the basics of this that would be much appreciated. As I only want to reassign a variable on click of a link, I don't think a partial view is required.

    Steph

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Mar 09, 2015 @ 21:11
    Jan Skovgaard
    0

    Hi Steph

    What is the purpose of your function? What should it do for you? Can you ellaborate a bit more about what it is you're trying to achieve? The above scenario seem a bit abstract :)

    /Jan

  • Stephanie Nicolaou 33 posts 91 karma points
    Mar 09, 2015 @ 22:00
    Stephanie Nicolaou
    0

    Hi Jan,

    I'm filtering content based on chosen sidebar menu links. When a menu link is clicked I need to pass the link name value to a function in order to assign it to the variable used to compare the content's property, allowing me to select which content to show on the page if there is a match.

    I had this party working with javascript when the page initially loaded but had trouble passing js values back to razor in order to update the content set so now I need to know how this is done using an umbraco function.

    Steph

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Mar 09, 2015 @ 22:28
    Jan Skovgaard
    0

    Hi Steph

    Would it be possible for you to render all the content using Razor and then show/hide when sorting using JavaScript? Seems like a cumbersome approach you're doing currently? :)

    /Jan

  • Stephanie Nicolaou 33 posts 91 karma points
    Mar 09, 2015 @ 23:29
    Stephanie Nicolaou
    0

    Hi Jan,

    Is it possible to include an umbraco doc type property in JS in order to hide the matched content?

    Steph

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Mar 10, 2015 @ 05:33
    Jan Skovgaard
    0

    Hi Stephanie

    No, not unless you're using ajax somehow. But I'm thinking that if you render all of the content (depending on how much of course) you should be able to provide the content with different classes based on the content type and then in JavaScript you will be able to sort it using a plugin like isotope for instance? http://isotope.metafizzy.co/

    /Jan

  • Stephanie Nicolaou 33 posts 91 karma points
    Mar 10, 2015 @ 13:33
    Stephanie Nicolaou
    0

    Hi Jan,

    It would be useful to know if I can use a razor function on click of a list item or at least get the list/div ID in umbraco ?

    I can actually sort the content on page load just by using an if statement when loading the content in the foreach. the problem is updating the value used in the if statement as it will not update when the list link is clicked. So all my function would need to do is reassign to variable.

    I have assigned the variable before each list is created however it needs to be assigned on click or I need to get the list id:

    [code]

    foreach (dynamic secondPage in page.Children)
            {
            var itemtest2 = @secondPage.Name;
            itemtest = itemtest2; 
             <li style="color:black">
             <a href="#" id="@itemtest" > @secondPage.Name </a>
             </li>  
            }     

    [/code]

    Thanks for the plugin reference though, I will use that if I don't get anywhere!

Please Sign in or register to post replies

Write your reply to:

Draft