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
) 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.