locked
QueryableFilterRepeater: order of filter columns displayed RRS feed

  • Question

  • User-931283679 posted

    I have a standard QueryableFilterRepeater control in my ListDetails.aspx page, as comes straight out of the box with DD2:

                <asp:QueryableFilterRepeater runat="server" ID="FilterRepeater">
                    <ItemTemplate>
                      <span class="filterItem">
                        <asp:Label runat="server" Text='<%# Eval("DisplayName") %>' OnPreRender="Label_PreRender" />
                        <asp:DynamicFilter runat="server" ID="DynamicFilter" OnFilterChanged="DynamicFilter_FilterChanged" />
                      </span>
                    </ItemTemplate>
                </asp:QueryableFilterRepeater>

    Is there a way I can specify in which order the filter-columns are displayed on the page?  At the moment it seems to be random.

    Thanks,

    Ed Graham

    Friday, November 4, 2011 10:54 AM

Answers

  • User-330204900 posted

    Hi ed_ward_graham, have a look at my example here A New Way To Do Column Generation in Dynamic Data ...I create a custom meta model and in my all you need to do is override the GetFilteredColumns method in the MetaTable like this:

    public override IEnumerable<MetaColumn> GetFilteredColumns()
    {
        var filteredColumns = base.GetFilteredColumns();
        var filteredColumnsSorted = from f in filteredColumns
                                    orderby f.GetAttributeOrDefault<FilterAttribute>(), f.Name
                                    select f;
    
        return filteredColumnsSorted;
    }

    the you will need an attribute to set the order:

    [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
    public sealed class FilterAttribute : Attribute, IComparable<FilterAttribute>
    {
        internal static FilterAttribute Default = new FilterAttribute();
    
        public FilterAttribute()
        {
            Order = 0;
        }
    
        /// <summary>
        /// The ordering of a filter. Negative values are allowed.
        /// </summary>
        public int Order { get; set; }
    
        /// <summary>
        /// Gets or sets the group that this filter will be in.
        /// </summary>
        /// <value>The group.</value>
        public String Group { get; set; }
    
        #region IComparable<FilterAttribute> Members
    
            public int CompareTo(FilterAttribute other)
            {
                return Order - ((FilterAttribute)other).Order;
            }
    
            #endregion
    }

    and then the extension method to get the attribute

    public static T GetAttributeOrDefault<T>(this MetaColumn column) where T : Attribute, new()
    {
        return column.Attributes.OfType<T>().DefaultIfEmpty(new T()).FirstOrDefault();
    }

    you can see all the extension methods I use here Writing Attributes and Extension Methods for Dynam...

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, November 4, 2011 12:22 PM

All replies

  • User-330204900 posted

    Hi ed_ward_graham, have a look at my example here A New Way To Do Column Generation in Dynamic Data ...I create a custom meta model and in my all you need to do is override the GetFilteredColumns method in the MetaTable like this:

    public override IEnumerable<MetaColumn> GetFilteredColumns()
    {
        var filteredColumns = base.GetFilteredColumns();
        var filteredColumnsSorted = from f in filteredColumns
                                    orderby f.GetAttributeOrDefault<FilterAttribute>(), f.Name
                                    select f;
    
        return filteredColumnsSorted;
    }

    the you will need an attribute to set the order:

    [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
    public sealed class FilterAttribute : Attribute, IComparable<FilterAttribute>
    {
        internal static FilterAttribute Default = new FilterAttribute();
    
        public FilterAttribute()
        {
            Order = 0;
        }
    
        /// <summary>
        /// The ordering of a filter. Negative values are allowed.
        /// </summary>
        public int Order { get; set; }
    
        /// <summary>
        /// Gets or sets the group that this filter will be in.
        /// </summary>
        /// <value>The group.</value>
        public String Group { get; set; }
    
        #region IComparable<FilterAttribute> Members
    
            public int CompareTo(FilterAttribute other)
            {
                return Order - ((FilterAttribute)other).Order;
            }
    
            #endregion
    }

    and then the extension method to get the attribute

    public static T GetAttributeOrDefault<T>(this MetaColumn column) where T : Attribute, new()
    {
        return column.Attributes.OfType<T>().DefaultIfEmpty(new T()).FirstOrDefault();
    }

    you can see all the extension methods I use here Writing Attributes and Extension Methods for Dynam...

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, November 4, 2011 12:22 PM
  • User-931283679 posted

    Thanks for that, Steve -- I'll look into it when I get a chance (the answer was a little more involved than I thought it would be!).

    Cheers,

    Ed

    Friday, November 4, 2011 12:45 PM
  • User-330204900 posted

    Hi Ed, I am working on a new open source project and all my examples will be included as NuGet packages and there will also be a project template with everything in there.

    Friday, November 4, 2011 1:59 PM