Space-separated list of classifications / taxonomies

Posted by Community Admin on 04-Aug-2018 15:33

Space-separated list of classifications / taxonomies

All Replies

Posted by Community Admin on 06-Jan-2014 00:00

I have created a custom module which includes a field for a custom classification which is selected just like tags are. I'm using client-side list filtering via Isotope (http://isotope.metafizzy.co/). In order to accomplish this, I need to render the classification names assigned to each module item as a space-separated text. I've been editing the list template for this custom module, but I can't seem to get it to render a simple space-separated list.

To clarify, let's say I've created a custom module of Widget. I've added a custom classification of WidgetCategory, which is a field for each Widget item. Let's say I have a widget named "Sprocket" that's has categories "Round" and "Steel." I need the list to render something like this:

<div class="isotopeItem Round Steel">
     Sprocket
</div>

I tried using the FlatTaxonField control, but it creates a UL for the classifications. I tried specifying my own layout template, but it gives me an "object reference not set to an instance of an object" error when rendering the control.
 
I'd like to be able to do this via the list template rather than developing a custom widget if possible.

Posted by Community Admin on 06-Jan-2014 00:00
Posted by Community Admin on 06-Jan-2014 00:00

Hey John,

I think that you either use the solution like Steve suggested, or create a custom widget. It's more flexible.

I've done quite the same thing using MixItUp on this page: http://www.lightfortheworld.nl/en/what-we-do/projects

Is that what you like to achieve?

Kind regards,
Daniel

Posted by Community Admin on 06-Jan-2014 00:00

Steve's post was a big help, but I couldn't use it as a user control since I needed to append the class listing as a plain string. After playing around with it for a bit, I decided to just do an extension method for the databinding container. Here's what I did:

public static class TaxonomyPropertyExtensions
    public static string GetTaxonomyNameList(this IDataItemContainer container, string propertyName, string separator)
    
        var dataItem = container.DataItem;
 
        if (dataItem != null)
        
            if (!String.IsNullOrEmpty(propertyName))
            
                var property =
                    OrganizerBase.GetProperty(dataItem.GetType(), propertyName) as
                        TaxonomyPropertyDescriptor;
                if (property != null)
                
                    var @value = property.GetValue(dataItem) as IList<Guid>;
                    if (@value != null)
                    
                        var manager = TaxonomyManager.GetManager();
                        return string.Join(separator, @value.Select(guid => manager.GetTaxon(guid).Name).ToArray());
                    
                
            
        
 
        return "";
    

To use this in my custom module list, you just need to do something like this:

<div class='isotopeItem <%# Container.GetTaxonomyNameList("propertyname", " ") %>'>
    [Standard binding stuff]
</div>

This thread is closed