Copied to clipboard

Flag this post as spam?

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


  • Niels Lynggaard 193 posts 551 karma points
    Jan 15, 2018 @ 10:31
    Niels Lynggaard
    1

    Checkbox list in leblender grid editor

    Hi!

    I'm using Leblender to insert a filtered list via a control that has a nodepicker and a checkbox list. The nodepicker works nicely, when getting the property by item.GetValue<>

    However, I'm trying to do the same for the property based on datatype Checkbox List, with bad results.. It comes back as a json Array with ID's, and I'd like for it to return the actual value, e.g a list of strings.

    How can I accomplish this?

    Cheers, Niels

  • bob baty-barr 1180 posts 1294 karma points MVP
    Sep 10, 2018 @ 19:50
    bob baty-barr
    0

    Niels, did you ever figure anything out with this? i am currently struggling with the same. Thanks, Bob

  • Niels Lynggaard 193 posts 551 karma points
    Sep 11, 2018 @ 08:32
    Niels Lynggaard
    0

    Hey Bob.

    I actually ended up hardcoding the translation between the ID's in the Json and the actual value that I wanted. It's not the prettiest solution..

    What I did was to get the json out like this;

      var jsoncategories = item.GetValue("sponsorkategorier");
    Newtonsoft.Json.Linq.JArray arraycategories = new Newtonsoft.Json.Linq.JArray();
    if (jsoncategories != null) {
        arraycategories = (Newtonsoft.Json.Linq.JArray)JsonConvert.DeserializeObject(jsoncategories);
    }
    

    And then later I translate this into the strings that I need to use;

    foreach (string s in arraycategories)
    {
        switch (s)
        {
            case "371":
                spcategories.Add("Hovedsponsor");
                break;
            case "372":
                spcategories.Add("Stjernesponsor");
                break;
            case "373":
                spcategories.Add("Kultursponsor");
                break;
            case "374":
                spcategories.Add("Erhvervssponsor");
                break;
            default:
                break;
        }
    }
    

    Not exactly the perfect solution, since I will have to add a few lines of code if the need to expand the checkbox list should ever arise.

    If you crack the code and comes up with a better solution, please let me know.

    Cheers, Niels

  • Genius 3 posts 74 karma points
    Mar 20, 2019 @ 10:34
    Genius
    1

    Instead of using the switch statement to extract values, you can try the following to extra values of selected items

    var jsoncategories = items.GetValue("offersToPresent");
    Newtonsoft.Json.Linq.JArray arraycategories = new Newtonsoft.Json.Linq.JArray();
    if (jsoncategories != null){
        arraycategories = (Newtonsoft.Json.Linq.JArray)Newtonsoft.Json.JsonConvert.DeserializeObject(jsoncategories);
    }
    
    foreach (var item in arraycategories)
    {
        var preval = Umbraco.GetPreValueAsString(Convert.ToInt32(item));
        @Html.Raw(preval); // returns prevalue (text values of id)
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft