Esempio n. 1
0
        /// <summary>
        /// Create records from a collection
        /// </summary>
        /// <typeparam name="TType">Type of the collection</typeparam>
        /// <param name="values">The collection to create records from</param>
        /// <returns>Created records</returns>
        /// <exception cref="WebExtras.Core.InvalidUsageException"></exception>
        public static DatatableRecords From <TType>(ICollection <TType> values)
            where TType : class
        {
            List <string[]> data = new List <string[]>();

            if (values == null)
            {
                return(new DatatableRecords());
            }

            if (!values.Any())
            {
                return(new DatatableRecords());
            }

            Type t = typeof(TType);

            foreach (TType value in values)
            {
                List <KeyValuePair <int, string> > indexedValues = new List <KeyValuePair <int, string> >();
                PropertyInfo[] props = value.GetType().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, t.FullName));
                    }

                    // fact that we got here means that the current property is an AOColumn
                    IValueFormatter formatter = (IValueFormatter)(attribs[0].ValueFormatter == null ?
                                                                  new DefaultValueFormatter() :
                                                                  Activator.CreateInstance(attribs[0].ValueFormatter));

                    string val = formatter.Format(prop.GetValue(value, null), attribs[0].FormatString, value);

                    indexedValues.Add(new KeyValuePair <int, string>(attribs[0].Index, val));
                }

                data.Add(indexedValues.OrderBy(f => f.Key).Select(g => g.Value).ToArray());
            }

            DatatableRecords records = new DatatableRecords
            {
                iTotalDisplayRecords = data.Count,
                iTotalRecords        = data.Count,
                aaData = data.ToArray()
            };

            return(records);
        }
Esempio n. 2
0
        /// <summary>
        /// Constructor. Note. The column specifications must be defined via the DatatableSettings constructor
        /// </summary>
        /// <param name="id">HTML field ID for the Datatable</param>
        /// <param name="settings">Datatable settings</param>
        /// <param name="records">Datatable records</param>
        /// <param name="postbacks">[Optional] Postback data for server side processing</param>
        /// <param name="enableStatusColumn">[Optional] flag indicating whether to enable status column. Defaults to false</param>
        public Datatable(string id, DatatableSettings settings, DatatableRecords records, IEnumerable <PostbackItem> postbacks = null, bool enableStatusColumn = false)
        {
            if (settings.aoColumns == null)
            {
                throw new NullReferenceException("Column setup not done. DatatableSettings.aoColumns property must be set in order to compose the datatable");
            }

            ID        = id.Replace("-", "_");
            Settings  = settings;
            Records   = records;
            Postbacks = postbacks != null?postbacks.ToArray() : new PostbackItem[0];

            EnableStatusColumn = enableStatusColumn;

            if (EnableStatusColumn)
            {
                settings.aoColumns = settings.aoColumns.Concat(new[] { new AOColumn {
                                                                           bVisible = false
                                                                       } }).ToArray();
                settings.SetupfnCreatedRow();
            }

            settings.SetupfnServerData(Postbacks);
        }
Esempio n. 3
0
        ///// <summary>
        ///// Constructor
        ///// </summary>
        ///// <param name="settings">Datatable settings</param>
        ///// <param name="columns">Datatable column specifications</param>
        ///// <param name="records">Datatable records</param>
        ///// <param name="postbacks">[Optional] Postback data for server side processing</param>
        ///// <param name="enableStatusColumn">[Optional] flag indicating whether to enable status column. Defaults to false</param>
        //[Obsolete("Use Ctor(settings, records, postbacks, enableStatusColumn) instead")]
        //public Datatable(DatatableSettings settings, IEnumerable<DatatableColumn> columns,
        //  DatatableRecords records, IEnumerable<PostbackItem> postbacks = null, bool enableStatusColumn = false)
        //  : this(string.Format("autogen-{0}", Guid.NewGuid()), settings, columns, records, postbacks, enableStatusColumn) { }

        ///// <summary>
        ///// Constructor to initialize with an HTML field ID
        ///// </summary>
        ///// <param name="id">HTML field ID for the Datatable</param>
        ///// <param name="settings">Datatable settings</param>
        ///// <param name="columns">Datatable column specifications</param>
        ///// <param name="records">Datatable records</param>
        ///// <param name="postbacks">[Optional] Postback data for server side processing</param>
        ///// <param name="enableStatusColumn">[Optional] flag indicating whether to enable status column. Defaults to false</param>
        //[Obsolete("Use Ctor(id, settings, records, postbacks, enableStatusColumn) instead")]
        //public Datatable(string id, DatatableSettings settings, IEnumerable<DatatableColumn> columns,
        //  DatatableRecords records, IEnumerable<PostbackItem> postbacks = null, bool enableStatusColumn = false)
        //{
        //  ID = id.Replace("-", "_");
        //  Settings = settings;

        //  // sanitize the datatable column widths
        //  IEnumerable<DatatableColumn> datatableColumns = columns as DatatableColumn[] ?? columns.ToArray();
        //  int nullWidthColumns = datatableColumns.Count(f => !f.Width.HasValue);
        //  int widthAssigned = datatableColumns.Select(f => f.Width != null ? f.Width.Value : 0).Sum();
        //  int widthLeft = 100 - widthAssigned;

        //  datatableColumns.Where(f => !f.Width.HasValue).ToList().ForEach(f => { f.Width = widthLeft / nullWidthColumns; });

        //  // setup the status column if the flag is set
        //  if (enableStatusColumn)
        //    datatableColumns = datatableColumns.Concat(new[] { new DatatableColumn("", visible: false, bSortable: false) });

        //  // if the aoColumns were not setup then do a default setup
        //  if (settings.aoColumns == null)
        //    settings.SetupAOColumns(datatableColumns);

        //  Columns = datatableColumns.ToArray();
        //  Records = records;
        //  Postbacks = postbacks != null ? postbacks.ToArray() : new PostbackItem[0];
        //  EnableStatusColumn = enableStatusColumn;
        //}

        /// <summary>
        /// Constructor. Note. The column specifications must be defined via the DatatableSettings constructor
        /// </summary>
        /// <param name="settings">Datatable settings</param>
        /// <param name="records">Datatable records</param>
        /// <param name="postbacks">[Optional] Postback data for server side processing</param>
        /// <param name="enableStatusColumn">[Optional] flag indicating whether to enable status column. Defaults to false</param>
        public Datatable(DatatableSettings settings, DatatableRecords records, IEnumerable <PostbackItem> postbacks = null, bool enableStatusColumn = false) :
            this(string.Format("autogen-{0}", Guid.NewGuid()), settings, records, postbacks, enableStatusColumn)
        {
        }