Esempio n. 1
0
        public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            var valueProvider = bindingContext.ValueProvider;

            DataTablesParam obj = new DataTablesParam(GetValue <int>(valueProvider, "iColumns"));

            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");
            obj.sColumns       = GetValue <string>(valueProvider, "sColumns").Split(',').ToList();
            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.sSearchColumns.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);
        }
Esempio n. 2
0
        public static DataTablesResult Create(IQueryable queryable, DataTablesParam dataTableParam)
        {
            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 }));
        }
Esempio n. 3
0
 public static DataTablesResult <T> CreateResultUsingEnumerable <T>(IEnumerable <T> q, DataTablesParam dataTableParam)
 {
     return(Create(q.AsQueryable(), dataTableParam));
 }
Esempio n. 4
0
        public static DataTablesResult <TSource> Create <TSource>(IQueryable <TSource> q, DataTablesParam dataTableParam)
        {
            var result = new DataTablesResult <TSource>(q, dataTableParam);

            result.Data = result.Data
                          .Transform <TSource, Dictionary <string, object> >(DataTablesTypeInfo <TSource> .ToDictionary)
                          .Transform <Dictionary <string, object>, Dictionary <string, object> >(StringTransformers.StringifyValues);
            return(result);
        }
Esempio n. 5
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)
        {
            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);
            return(result);
        }
Esempio n. 6
0
        public IQueryable <T> ApplyFiltersAndSort <T>(DataTablesParam dtParameters, IQueryable <T> data, DataTablesPropertyInfo[] columns)
        {
            string[] cloumnsStr = dtParameters.sColumns.ToArray();
            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.FirstOrDefault(p => p.PropertyInfo.Name == cloumnsStr[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.sSearchColumns.Count; i++)
            {
                if (dtParameters.bSearchable[i])
                {
                    var searchColumn = dtParameters.sSearchColumns[i];
                    if (!string.IsNullOrWhiteSpace(searchColumn))
                    {
                        var parameters   = new List <object>();
                        var filterClause = GetFilterClause(dtParameters.sSearchColumns[i], columns.FirstOrDefault(p => p.PropertyInfo.Name == cloumnsStr[i]), 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];
                string columnName   = columns.FirstOrDefault(p => p.PropertyInfo.Name == cloumnsStr[columnNumber]).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);
        }