Beispiel #1
0
        public static DataTablesResult Create(IQueryable queryable, DataTablesParam dataTableParam,
                                              ArrayOutputType?arrayOutput = null)
        {
            var s = "Create";
            var openCreateMethod   = typeof(DataTablesResult).GetMethods().Single(x => x.Name == s && x.GetGenericArguments().Count() == 1);
            var queryableType      = queryable.GetType().GetGenericArguments()[0];
            var closedCreateMethod = openCreateMethod.MakeGenericMethod(queryableType);

            return((DataTablesResult)closedCreateMethod.Invoke(null, new object[] { queryable, dataTableParam, arrayOutput }));
        }
Beispiel #2
0
        /// <param name="transform">Should be a Func<T, TTransform></param>
        public static DataTablesResult Create(IQueryable queryable, DataTablesParam dataTableParam, object transform,
                                              ResponseOptions responseOptions = null)
        {
            var s = "Create";
            var openCreateMethod   = typeof(DataTablesResult).GetMethods().Single(x => x.Name == s && x.GetGenericArguments().Count() == 1);
            var queryableType      = queryable.GetType().GetGenericArguments()[0];
            var closedCreateMethod = openCreateMethod.MakeGenericMethod(queryableType, typeof(object));

            return((DataTablesResult)closedCreateMethod.Invoke(null, new object[] { queryable, dataTableParam, transform, responseOptions }));
        }
 private DataTablesPropertyInfo FindColumn(DataTablesParam dtParameters, DataTablesPropertyInfo[] columns, int i)
 {
     if (dtParameters.sColumnNames.Any())
     {
         return(columns.First(x => x.PropertyInfo.Name == dtParameters.sColumnNames[i]));
     }
     else
     {
         return(columns[i]);
     }
 }
Beispiel #4
0
        private object BindV10Model(IValueProvider valueProvider)
        {
            DataTablesParam obj = new DataTablesParam();

            obj.iDisplayStart  = GetValue <int>(valueProvider, "start");
            obj.iDisplayLength = GetValue <int>(valueProvider, "length");
            obj.sSearch        = GetValue <string>(valueProvider, "search[value]");
            obj.bEscapeRegex   = GetValue <bool>(valueProvider, "search[regex]");
            obj.sEcho          = GetValue <int>(valueProvider, "draw");

            int colIdx = 0;

            while (true)
            {
                string colPrefix = String.Format("columns[{0}]", colIdx);
                string colName   = GetValue <string>(valueProvider, colPrefix + "[data]");
                if (String.IsNullOrWhiteSpace(colName))
                {
                    break;
                }
                obj.sColumnNames.Add(colName);
                obj.bSortable.Add(GetValue <bool>(valueProvider, colPrefix + "[orderable]"));
                obj.bSearchable.Add(GetValue <bool>(valueProvider, colPrefix + "[searchable]"));
                obj.sSearchValues.Add(GetValue <string>(valueProvider, colPrefix + "[search][value]"));
                obj.bEscapeRegexColumns.Add(GetValue <bool>(valueProvider, colPrefix + "[searchable][regex]"));
                colIdx++;
            }
            obj.iColumns = colIdx;
            colIdx       = 0;
            while (true)
            {
                string colPrefix   = String.Format("order[{0}]", colIdx);
                int?   orderColumn = GetValue <int?>(valueProvider, colPrefix + "[column]");
                if (orderColumn.HasValue)
                {
                    obj.iSortCol.Add(orderColumn.Value);
                    obj.sSortDir.Add(GetValue <string>(valueProvider, colPrefix + "[dir]"));
                    colIdx++;
                }
                else
                {
                    break;
                }
            }
            obj.iSortingCols = colIdx;
            return(obj);
        }
Beispiel #5
0
        private DataTablesParam BindLegacyModel(IValueProvider valueProvider, int columns)
        {
            DataTablesParam obj = new DataTablesParam(columns);

            obj.iDisplayStart  = GetValue <int>(valueProvider, "iDisplayStart");
            obj.iDisplayLength = GetValue <int>(valueProvider, "iDisplayLength");
            obj.sSearch        = GetValue <string>(valueProvider, "sSearch");
            obj.bEscapeRegex   = GetValue <bool>(valueProvider, "bEscapeRegex");
            obj.iSortingCols   = GetValue <int>(valueProvider, "iSortingCols");
            obj.sEcho          = GetValue <int>(valueProvider, "sEcho");

            for (int i = 0; i < obj.iColumns; i++)
            {
                obj.bSortable.Add(GetValue <bool>(valueProvider, "bSortable_" + i));
                obj.bSearchable.Add(GetValue <bool>(valueProvider, "bSearchable_" + i));
                obj.sSearchValues.Add(GetValue <string>(valueProvider, "sSearch_" + i));
                obj.bEscapeRegexColumns.Add(GetValue <bool>(valueProvider, "bEscapeRegex_" + i));
                obj.iSortCol.Add(GetValue <int>(valueProvider, "iSortCol_" + i));
                obj.sSortDir.Add(GetValue <string>(valueProvider, "sSortDir_" + i));
            }
            return(obj);
        }
        public IQueryable <T> ApplyFiltersAndSort <T>(DataTablesParam dtParameters, IQueryable <T> data, DataTablesPropertyInfo[] columns)
        {
            if (!String.IsNullOrEmpty(dtParameters.sSearch))
            {
                var parts      = new List <string>();
                var parameters = new List <object>();
                for (var i = 0; i < dtParameters.iColumns; i++)
                {
                    if (dtParameters.bSearchable[i])
                    {
                        try
                        {
                            parts.Add(GetFilterClause(dtParameters.sSearch, columns[i], parameters));
                        }
                        catch (Exception)
                        {
                            //if the clause doesn't work, skip it!
                        }
                    }
                }
                var values = parts.Where(p => p != null);
                data = data.Where(string.Join(" or ", values), parameters.ToArray());
            }
            for (int i = 0; i < dtParameters.sSearchValues.Count; i++)
            {
                if (dtParameters.bSearchable[i])
                {
                    var searchColumn = dtParameters.sSearchValues[i];
                    if (!string.IsNullOrWhiteSpace(searchColumn))
                    {
                        DataTablesPropertyInfo column = FindColumn(dtParameters, columns, i);
                        var parameters   = new List <object>();
                        var filterClause = GetFilterClause(searchColumn, column, parameters);
                        if (string.IsNullOrWhiteSpace(filterClause) == false)
                        {
                            data = data.Where(filterClause, parameters.ToArray());
                        }
                    }
                }
            }
            string sortString = "";

            for (int i = 0; i < dtParameters.iSortingCols; i++)
            {
                int columnNumber = dtParameters.iSortCol[i];
                DataTablesPropertyInfo column = FindColumn(dtParameters, columns, columnNumber);
                string columnName             = column.PropertyInfo.Name;
                string sortDir = dtParameters.sSortDir[i];
                if (i != 0)
                {
                    sortString += ", ";
                }
                sortString += columnName + " " + sortDir;
            }
            if (string.IsNullOrWhiteSpace(sortString))
            {
                sortString = columns[0].PropertyInfo.Name;
            }
            data = data.OrderBy(sortString);


            return(data);
        }
Beispiel #7
0
        public static DataTablesResult <TSource> Create <TSource>(IQueryable <TSource> q, DataTablesParam dataTableParam,
                                                                  ResponseOptions <TSource> responseOptions = null)
        {
            var result = new DataTablesResult <TSource>(q, dataTableParam);

            var dictionaryTransform = DataTablesTypeInfo <TSource> .ToDictionary(responseOptions);

            result.Data = result.Data
                          .Transform <TSource, Dictionary <string, object> >(dictionaryTransform)
                          .Transform <Dictionary <string, object>, Dictionary <string, object> >(StringTransformers.StringifyValues);

            result.Data = ApplyOutputRules(result.Data, responseOptions);

            return(result);
        }
Beispiel #8
0
        /// <typeparam name="TSource"></typeparam>
        /// <typeparam name="TTransform"></typeparam>
        /// <param name="q">A queryable for the data. The properties of this can be marked up with [DataTablesAttribute] to control sorting/searchability/visibility</param>
        /// <param name="dataTableParam"></param>
        /// <param name="transform">//a transform for custom column rendering e.g. to do a custom date row => new { CreatedDate = row.CreatedDate.ToString("dd MM yy") } </param>
        /// <returns></returns>
        public static DataTablesResult <TSource> Create <TSource, TTransform>(IQueryable <TSource> q, DataTablesParam dataTableParam,
                                                                              Func <TSource, TTransform> transform, ResponseOptions <TSource> responseOptions = null)
        {
            var result = new DataTablesResult <TSource>(q, dataTableParam);

            result.Data = result.Data
                          .Transform <TSource, Dictionary <string, object> >(row => TransformTypeInfo.MergeTransformValuesIntoDictionary(transform, row))
                          .Transform <Dictionary <string, object>, Dictionary <string, object> >(StringTransformers.StringifyValues);

            result.Data = ApplyOutputRules(result.Data, responseOptions);

            return(result);
        }
Beispiel #9
0
        /// <typeparam name="TSource"></typeparam>
        /// <typeparam name="TTransform"></typeparam>
        /// <param name="q">A queryable for the data. The properties of this can be marked up with [DataTablesAttribute] to control sorting/searchability/visibility</param>
        /// <param name="dataTableParam"></param>
        /// <returns></returns>

        public static DataTablesResult <TSource> Create <TSource>(IQueryable <TSource> q, DataTablesParam dataTableParam,
                                                                  ArrayOutputType?arrayOutput = null)
        {
            return(Create(q, dataTableParam, new ResponseOptions <TSource>()
            {
                ArrayOutputType = arrayOutput
            }));
        }
Beispiel #10
0
 public static DataTablesResult <T> CreateResultUsingEnumerable <T>(IEnumerable <T> q, DataTablesParam dataTableParam,
                                                                    ArrayOutputType?arrayOutput = null)
 {
     return(Create(q.AsQueryable(), dataTableParam, arrayOutput));
 }