예제 #1
0
        public void setModel(StringBuilder searchQ, CommonModel <T> model, Dictionary <string, string> colName, string sortFieldName, int page = 1, int pageSize = 10)
        {
            model.Filters = model.Filters == null ? new List <GridFilter>() : model.Filters;

            if (!searchQ.ToString().ToLower().Contains("where"))
            {
                searchQ.Append(" WHERE ");
            }
            searchQ.Append(CommonFunction.GenerateFilterCondition(model.Filters));

            if (!string.IsNullOrWhiteSpace(model.fieldName) && !string.IsNullOrWhiteSpace(model.sortOrder))
            {
                searchQ.Append(" ORDER BY " + model.fieldName + " " + model.sortOrder);
            }
            else
            {
                searchQ.Append(" ORDER BY " + sortFieldName);
            }

            foreach (var item in colName)
            {
                if (model.Filters != null)
                {
                    var obj = model.Filters.FirstOrDefault(d => d.ColumnName == item.Key);
                    if (obj == null)
                    {
                        obj = new GridFilter {
                            ColumnName = item.Key, DisplayText = item.Value
                        };
                        model.Filters.Add(obj);
                    }
                    else
                    {
                        obj.DisplayText = item.Value;
                    }
                }
            }

            //model.Filters = model.Filters.OrderBy(d => colName.IndexOf(d.ColumnName)).ToList();
            model.Filters = model.Filters.OrderBy(d => colName.Keys.ToList().IndexOf(d.ColumnName)).ToList();
            int totalCount = 0;
            int pIndex     = model.page.HasValue ? model.page.Value - 1 : 0;
            var list       = GetDynamicList(searchQ.ToString(), ref totalCount, pIndex, pageSize).ToList();

            var pagedList = new StaticPagedList <dynamic>(list, page, pageSize, totalCount).GetMetaData();

            model.dynamicList         = list;
            model.dynamicListMetaData = pagedList;
        }