/// <summary>
 ///   Constructor
 /// </summary>
 /// <param name="displayLength">Number of rows to display on a single page when using pagination</param>
 /// <param name="column">Column specifications</param>
 /// <param name="sortOption">Column sort option. If this is null no sorting will be applied</param>
 /// <param name="ajaxSource">AJAX source URL</param>
 /// <param name="footerSuffix">
 ///   [Optional] This string gives information to the end user about the information
 ///   that is current on display on the page
 /// </param>
 /// <param name="tableHeight">
 ///   [Optional] Height of the table in pixels. Defaults to 200px. Note. Pass in a null if to do not
 ///   want any table height set
 /// </param>
 public DatatableSettings(int displayLength, AOColumn column, AASort sortOption, string ajaxSource,
                          string footerSuffix = "", string tableHeight = "200px")
     : this(
         displayLength, new[] { column }, (sortOption == null) ? null : new[] { sortOption }, ajaxSource, footerSuffix,
         tableHeight)
 {
     // nothing to do here
 }
Example #2
0
    /// <summary>
    /// Create AOColumn definitions from a type
    /// </summary>
    /// <param name="type">Type to create AOColumn definitions from</param>
    /// <returns>Generated columns</returns>
    public static AOColumn[] FromType(Type type)
    {
      string[] ignoredAttributeProperties = { "ValueFormatter", "TypeId" };
      List<KeyValuePair<int, AOColumn>> indexedColumns = new List<KeyValuePair<int, AOColumn>>();

      // get the type and all public properties of the given object instance
      PropertyInfo[] props = type.GetProperties();

      foreach (PropertyInfo prop in props)
      {
        AOColumnAttribute[] attribs = (AOColumnAttribute[])prop.GetCustomAttributes(typeof(AOColumnAttribute), false);

        if (attribs.Length == 0)
          continue;

        if (attribs.Length > 1)
          throw new InvalidUsageException(
            string.Format("The property '{0}' on '{1}' can not have multiple decorations of AOColumn attribute", prop.Name, type.FullName));

        // fact that we got here means that the attribute decoration was all good
        PropertyInfo[] attribProps = attribs[0].GetType().GetProperties();
        AOColumn column = new AOColumn();
        int idx = -1;
        foreach (PropertyInfo attribProp in attribProps)
        {
          // get the value of the attribute property
          object val = attribProp.GetValue(attribs[0], null);

          // the 'TypeId' property is inherited from System.Attribute class
          // and we don't care about it, so just ignore it
          if (val == null || ignoredAttributeProperties.Contains(attribProp.Name))
            continue;

          // the 'Index' property governs the position of the column in the
          // rendered table, so just store it for now and continue
          if (attribProp.Name == "Index")
          {
            idx = (int)val;
            continue;
          }

          // get the same property from AOColumn class and set it's value to
          // the value obtained from the attribute's property
          PropertyInfo propToSet = column.GetType().GetProperty(attribProp.Name);
          propToSet.SetValue(column, val, null);
        }

        // store the column with it's index for further processing
        indexedColumns.Add(new KeyValuePair<int, AOColumn>(idx, column));
      }

      // order the columns by their key and select
      List<AOColumn> columns = indexedColumns.OrderBy(f => f.Key).Select(g => g.Value).ToList();

      return columns.ToArray();
    }