Showing posts with label Hide Column. Show all posts
Showing posts with label Hide Column. Show all posts

Friday, 4 December 2009

Hiding Foreign Key or Children Columns Globally in Dynamic Data

A continuation of Hiding Foreign Key column Globally in Dynamic Data. Kharot here was asking in this tread Conditional display of Foreign key navigation for a way to do this same thing with the Children FieldTemplate (it took me a while to catch on Embarrassed) and so here it is, it’s basically the same as previouly written but just a few changes to the attribute and the Extension method used in the previous example.

/// <summary>
/// Checks if either the Foreign Key or 
/// Children navigation fields the are hidden.
/// </summary>
/// <param name="column">The current MetaColumn.</param>
/// <returns>
/// true if either the Foreign Key or Children 
/// navigation field are set to hidden at table level
/// </returns>
public static Boolean FkIsHidden(this MetaColumn column)
{
    var fkColumn = column as MetaForeignKeyColumn;
    if (fkColumn != null)
        return fkColumn.ParentTable.
            GetAttributeOrDefault<HideFKColumnAttribute>().
            ForeignKeyFieldIsHidden;
    
    var childrenColumn = column as MetaChildrenColumn;
    if (childrenColumn != null)
        return childrenColumn.ChildTable.
            GetAttributeOrDefault<HideFKColumnAttribute>().
            ChildrenFieldIsHidden;

    return false;
}

Listing 1 – the Extension method

All I’ve done here is test for the column being a hidden in either the Parent or Children tables.

/// <summary>
/// Hides the ForeignKey or Children Navigation Column
/// </summary>
[AttributeUsage(AttributeTargets.Class)]
public class HideFKColumnAttribute : Attribute
{
    /// <summary>
    /// Gets or sets a value indicating whether [foreign key field hidden].
    /// </summary>
    /// <value>
    ///     <c>true</c> if [foreign key field hidden]; otherwise, <c>false</c>.
    /// </value>
    public Boolean ForeignKeyFieldIsHidden { get; set; }

    /// <summary>
    /// Gets or sets a value indicating whether [children field hidden].
    /// </summary>
    /// <value><c>true</c> if [children field hidden]; otherwise, <c>false</c>.</value>
    public Boolean ChildrenFieldIsHidden { get; set; }

    /// <summary>
    /// Initializes a new instance of the <see cref="HideFKColumnAttribute"/> class.
    /// </summary>
    public HideFKColumnAttribute()
    {
        ForeignKeyFieldIsHidden = false;
        ChildrenFieldIsHidden = false;
    }
}

Listing 2 – HideFKColumnAttribute

Here I’ve added a new property and done a little refactoring to make things make sense and since it’s a Foreign Key relationship I’ve left the attribute with the same name.

[HideFKColumn(
    ForeignKeyFieldIsHidden = true,
    ChildrenFieldIsHidden = true)]
public partial class Employee
{
    // code omitted for brevity
}

Listing 3 – the metadata

I’ve made no changes to the IAutoFieldGenerator so things should just work, you can now hide the Foreign Key or Children columns globally.

Download

Remember to have fun coding Happy Wizzard

Saturday, 11 April 2009

Hiding Foreign Key column Globally in Dynamic Data

This article is based on a question in the Dynamic Data forum Hide Foreign Key Column. And so I thought I’d document what I did for posterity or at least so I can find it again if ever the question arises again :D

So I decided  here is what I would need:

  1. An Attribute to mark FK relation ships as hidden.
  2. Some Extension methods to extract and test the attribute
  3. An IAutoFieldGenerator to filter the Columns on a page

The Attribute

[AttributeUsage(AttributeTargets.Class)]
public class HideFKColumnAttribute : Attribute
{
    public Boolean Hidden { get; set; }
    public HideFKColumnAttribute()
    {
        Hidden = false;
    }

    public HideFKColumnAttribute(Boolean hide)
    {
        Hidden = hide;
    }
}

Listing 1 – HideFKColumnAttribute

This attribute will be set to true if the FK relationship is to be hidden and will default to false to make testing for easy.

The Extension Methods

/// <summary>
/// Test if this FK column should be hidden.
/// </summary>
/// <param name="column">
/// The column to test.
/// </param>
/// <returns>
/// Returns true if it the column should be hidden or false if not.
/// </returns>
public static Boolean FkIsHidden(this MetaColumn column)
{
    var fkColumn = column as MetaForeignKeyColumn;
    if (fkColumn != null)
        return fkColumn.ParentTable.GetAttributeOrDefault<HideFKColumnAttribute>().Hidden;
    else
        return false;
}

/// <summary>
/// Get the attribute or a default instance of the attribute
/// if the Table attribute do not contain the attribute
/// </summary>
/// <typeparam name="T">Attribute type</typeparam>
/// <param name="table">Table to search for the attribute on.</param>
/// <returns>The found attribute or a default instance of the attribute of type T</returns>
public static T GetAttributeOrDefault<T>(this MetaTable table) where T : Attribute, new()
{
    return table.Attributes.OfType<T>().DefaultIfEmpty(new T()).FirstOrDefault();
}

Listing 2 – Extension methods

FkIsHidden is used to test if the ParentTable has the HideFKColumnAttribute applied. And the extension method GetAttributeOrDefault returns an HideFKColumnAttribute which is false if not set but true if set explicitly.

The IAutoFieldGenerator class

using System.Collections;
using System.Collections.Generic;
using System.Web.DynamicData;
using System.Web.UI;

public class HideColumnFieldsManager : IAutoFieldGenerator
{
    protected MetaTable _table;
    protected PageTemplate _currentPage;

    public HideColumnFieldsManager(MetaTable table, PageTemplate currentPage)
    {
        _table = table;
        _currentPage = currentPage;
    }

    public ICollection GenerateFields(Control control)
    {
        var oFields = new List<DynamicField>();

        foreach (var column in _table.Columns)
        {
            // carry on the loop at the next column  
            // if scaffold table is set to false or DenyRead
            if (!column.Scaffold ||
                column.IsLongString ||
                column.FkIsHidden())
                continue;

            var f = new DynamicField();

            f.DataField = column.Name;
            oFields.Add(f);
        }
        return oFields;
    }
}

Listing 3 – HideColumnFieldManager

This is the same IAutoFieldGenerator class as used in the article Dynamic Data - Hiding Columns in selected PageTemplates only I’ve added an column.FkIsHidden() to the list of tests for column to display.

I think that about wraps this up, you can download the file from the article mentioned in the paragraph above and then add the extension methods and attribute. Then modify the HideColumnFieldManager class to test for the Hidden FK relationship.

Download