コード例 #1
0
        private static T ProcessDataResult <T>(T context, object result, NccActionsService.DataResult <T> dataResult)
        {
            if (result == null)
            {
                return(default(T));
            }

            if (result.GetType().ToString().Contains(typeof(Tuple).ToString()))
            {
                var item1 = result.GetType().GetProperty("Item1").GetValue(result);

                if (item1 is IQueryable)
                {
                    context.NccSetPropertyValue("DataObjects", ((IQueryable <object>)item1).ToList());
                }
                else
                {
                    var list = item1 as IList;
                    context.NccSetPropertyValue("DataObjects", list ?? item1);
                }
            }
            else
            {
                context.NccSetPropertyValue("DataObjects", result is IQueryable ? ((IQueryable <object>)result).ToList() : result);
            }

            dataResult?.Invoke(context, result);

            return(context);
        }
コード例 #2
0
        public static byte[] GetExcelPackage(NccGridContext context, HttpContext httpContext)
        {
            NccActionsService.ExtraParameters <NccGridContext> setExtraParameters = GridService.GetExtraNullableParameters;
            NccActionsService.DataResult <NccGridContext>      setDataResult      = GridService.SetDataResult;
            NccControlsService.BindData(context, httpContext, setExtraParameters, setDataResult);

            using (ExcelPackage package = new ExcelPackage())
            {
                Int32 row = 2;
                Int32 col = 1;

                if (string.IsNullOrEmpty(context.GridExportExcel.Title))
                {
                    context.GridExportExcel.Title = context.Id;
                }

                package.Workbook.Properties.Title    = context.GridExportExcel.Title;
                package.Workbook.Properties.Subject  = context.GridExportExcel.Subject;
                package.Workbook.Properties.Company  = context.GridExportExcel.Company;
                package.Workbook.Properties.Author   = context.GridExportExcel.Author;
                package.Workbook.Properties.Keywords = context.GridExportExcel.Keywords;

                package.Workbook.Worksheets.Add(context.GridExportExcel.Title);

                var list     = context.DataObjects as IList;
                var propInfo = list.GetType().GetTypeInfo().GenericTypeArguments[0].GetProperties();

                ExcelWorksheet sheet = package.Workbook.Worksheets[context.GridExportExcel.Title];

                foreach (var column in context.GridExportExcel.Columns)
                {
                    if (string.IsNullOrEmpty(column.ColumnTitle))
                    {
                        column.ColumnTitle = $"Coluna {col}";
                    }

                    sheet.Cells[1, col].Value = column.ColumnTitle;

                    //if (!string.IsNullOrEmpty(column.HeaderHorizontalAlignment))
                    //    sheet.Cells[1, col].Style.HorizontalAlignment = OfficeOpenXml.Style.ExcelHorizontalAlignment.

                    if (!context.GridExportExcel.AutoFitColumns.HasValue || !context.GridExportExcel.AutoFitColumns.Value)
                    {
                        sheet.Column(col).Width = column.Width.Value;
                    }

                    col++;
                }
                //foreach (var column in propInfo)
                //{
                //    if (column.PropertyType == typeof(string) || !typeof(IEnumerable).IsAssignableFrom(column.PropertyType))
                //    {
                //        sheet.Cells[1, col].Value = "Coluna " + col;
                //        sheet.Column(col++).Width = 18;
                //    }
                //}

                foreach (var gridRow in list)
                {
                    col = 1;
                    foreach (var column in context.GridExportExcel.Columns)
                    {
                        sheet.Cells[row, col++].Value = propInfo.FirstOrDefault(x => x.Name == column.ColumnPropName).GetValue(gridRow, null);
                    }
                    //foreach (var column in propInfo)
                    //{
                    //    if (column.PropertyType == typeof(string) || !typeof(IEnumerable).IsAssignableFrom(column.PropertyType))
                    //        sheet.Cells[row, col++].Value = column.GetValue(gridRow, null);
                    //}

                    row++;
                }

                // Add to table / Add summary row
                var tbl = sheet.Tables.Add(new ExcelAddressBase(fromRow: 1, fromCol: 1, toRow: row - 1, toColumn: col - 1), context.GridExportExcel.Title);
                if (context.GridExportExcel.ShowHeader.HasValue)
                {
                    tbl.ShowHeader = context.GridExportExcel.ShowHeader.Value;
                }
                //tbl.TableStyle = TableStyles.Medium13;
                if (context.GridExportExcel.ShowTotal.HasValue)
                {
                    tbl.ShowTotal = context.GridExportExcel.ShowTotal.Value;
                }
                //tbl.Columns[3].DataCellStyleName = dataCellStyleName;
                //tbl.Columns[3].TotalsRowFunction = RowFunctions.Sum;
                //sheet.Cells[5, 4].Style.Numberformat.Format = numberformat;

                if (context.GridExportExcel.AutoFitColumns.HasValue && context.GridExportExcel.AutoFitColumns.Value)
                {
                    sheet.Cells[1, 1, row - 1, col - 1].AutoFitColumns();
                }

                return(package.GetAsByteArray());
            }
        }
コード例 #3
0
        public override async Task ProcessAsync(TagHelperContext tagContext, TagHelperOutput output)
        {
            if (Context == null)
            {
                throw new Exception("The NccGridContext is null... Please check the reference.");
            }

            _nccTagContext.CssClassGrid            = CssClassTable;
            _nccTagContext.CssClassBody            = CssClassBody;
            _nccTagContext.CssClassHeader          = CssClassHeader;
            _nccTagContext.CssClassFooter          = CssClassFooter;
            _nccTagContext.CssClassHeaderContainer = CssClassHeaderContainer;
            _nccTagContext.CssClassTableContainer  = CssClassTableContainer;
            _nccTagContext.CssClassFooterContainer = CssClassFooterContainer;

            if (!string.IsNullOrEmpty(DataKeys) && DataKeys != Context.DataKeys)
            {
                Context.DataKeys = DataKeys;
            }

            if (RenderForm.HasValue)
            {
                Context.RenderForm = RenderForm.Value;
            }
            if (AutoGenerateEditButton.HasValue)
            {
                Context.AutoGenerateEditButton = AutoGenerateEditButton.Value;
            }
            if (AllowPaging.HasValue)
            {
                Context.AllowPaging = AllowPaging.Value;
            }
            if (Context.AllowPaging)
            {
                if (PagerNavigationSize.HasValue)
                {
                    Context.PagerNavigationSize = PagerNavigationSize.Value;
                }
                if (Context.Filters.ContainsKey("pageSize"))
                {
                    Context.PageSize = Convert.ToInt32(Context.Filters["pageSize"]);
                }
                else if (PageSize.HasValue)
                {
                    Context.PageSize = PageSize.Value;
                }
                if (Context.PageSize == 0 || Context.PageSize == int.MaxValue)
                {
                    Context.PageSize = 10;
                }
            }
            else
            {
                Context.PageSize = int.MaxValue;
            }

            if (Context.Filters.ContainsKey("pageNumber"))
            {
                Context.PageNumber = Convert.ToInt32(Context.Filters["pageNumber"]);
            }

            object service = null;

            if (!string.IsNullOrEmpty(Context.EventHandlerClass))
            {
                service = NccReflectionService.NccGetClassInstance(Context.EventHandlerClass, null);
            }

            service?.NccInvokeMethod(NccEventsEnum.Load.ToString(), new object[] { new NccEventArgs {
                                                                                       NccTagContext = _nccTagContext, NccControlContext = Context, ViewContext = ViewContext
                                                                                   } });

            //Get grid id and share it with siblings parents
            if (string.IsNullOrEmpty(Context.Id))
            {
                Context.Id = Guid.NewGuid().ToString();
            }

            output.TagName = "div";
            output.Attributes.SetAttribute("id", Context.Id);
            output.Attributes.SetAttribute("style", "position:relative");

            if (Context.Visible)
            {
                tagContext.Items.Add(typeof(NccGridContext), Context);

                if (Context.RenderForm)
                {
                    var form = new TagBuilder("form")
                    {
                        TagRenderMode = TagRenderMode.StartTag
                    };

                    output.PreContent.AppendHtml(form);
                }

                NccActionsService.ExtraParameters <NccGridContext> setExtraParameters = GridService.GetExtraParameters;
                NccActionsService.DataResult <NccGridContext>      setDataResult      = GridService.SetDataResult;
                NccControlsService.BindData(Context, ViewContext.HttpContext, setExtraParameters, setDataResult);

                service?.NccInvokeMethod(NccEventsEnum.DataBound.ToString(), new object[] { new NccEventArgs {
                                                                                                NccControlContext = Context, ViewContext = ViewContext, DataObjects = Context.DataObjects
                                                                                            } });

                _nccTagContext.GridHeader = new GridRow {
                    Cells = new List <GridCell>(), CssClass = CssClassHeader
                };

                await output.GetChildContentAsync();

                var tableContainerDiv = new TagBuilder("div");
                if (!string.IsNullOrEmpty(_nccTagContext.CssClassTableContainer))
                {
                    tableContainerDiv.Attributes.Add("class", _nccTagContext.CssClassTableContainer);
                }

                tableContainerDiv.InnerHtml.AppendHtml(GridService.BuildTableHtml(_nccTagContext, Context));

                output.Content.SetHtmlContent(tableContainerDiv);

                if (Context.AllowPaging)
                {
                    output.Content.AppendHtml(GridService.BuildTablePager(_nccTagContext, Context));
                }

                output.PreContent.AppendHtml(_nccTagContext.PreContent);
                output.PostContent.AppendHtml(_nccTagContext.PostContent);

                if (Context.RenderForm)
                {
                    var antiforgeryTag = Generator.GenerateAntiforgery(ViewContext);
                    if (antiforgeryTag != null)
                    {
                        output.PostContent.AppendHtml(antiforgeryTag);
                    }
                    output.PostContent.AppendHtml(new TagBuilder("form")
                    {
                        TagRenderMode = TagRenderMode.EndTag
                    });
                }
            }

            service?.NccInvokeMethod(NccEventsEnum.PreRender.ToString(), new object[] { new NccEventArgs {
                                                                                            NccTagContext = _nccTagContext, NccControlContext = Context, ViewContext = ViewContext
                                                                                        } });

            _nccTagContext.ControlContext = Context;

            output.PostContent.AppendHtml(NccControlsService.GetEncodedContext(_protector, Context.Id, Context));
            output.PostContent.AppendHtml(NccControlsService.GetAjaxLoaderOverlay());
            output.PostContent.AppendHtml(NccControlsService.GetAjaxLoaderImage());
        }
コード例 #4
0
        public static void BindData <T>(T context, HttpContext httpContext, NccActionsService.ExtraParameters <T> extraParameters, NccActionsService.DataResult <T> dataResult)
        {
            var autoBind = context.NccGetPropertyValue <bool>("AutoBind");

            if (autoBind)
            {
                DataService.GetControlData(context, httpContext, extraParameters, dataResult);
            }
            else
            {
                var dataSource  = (context as NccContext)?.DataSource;
                var dataObjects = dataSource is IQueryable ? ((IQueryable <object>)dataSource).ToList() : dataSource;

                context.NccSetPropertyValue("DataSource", dataObjects);
                context.NccSetPropertyValue("DataObjects", dataObjects);

                if (context.NccPropertyExists("TotalItems"))
                {
                    var data = dataObjects as IList;
                    context.NccSetPropertyValue("TotalItems", data?.Count ?? 0);
                }
            }
        }
コード例 #5
0
        public static void GetControlData <T>(T context, HttpContext httpContext, NccActionsService.ExtraParameters <T> extraParameters, NccActionsService.DataResult <T> dataResult)
        {
            if (context == null || httpContext == null)
            {
                return;
            }

            var options     = NccReflectionService.NccGetClassInstanceWithDi(httpContext, NccConstants.OptionsAssemblyName);
            var nccSettings = options != null ? ((IOptions <NccSettings>)options).Value : new NccSettings();

            var assemblyName      = context.NccGetPropertyValue <string>("DataAccessClass");
            var autoBind          = context.NccGetPropertyValue <bool>("AutoBind");
            var constructorParams = context.NccGetPropertyValue <object[]>("DataAccessParameters");

            if (!autoBind)
            {
                return;
            }

            var dbService = nccSettings.UseDependencyInjection ? NccReflectionService.NccGetClassInstanceWithDi(httpContext, assemblyName) : NccReflectionService.NccGetClassInstance(assemblyName, constructorParams);

            if (dbService == null)
            {
                return;
            }

            var methodName = context.NccGetPropertyValue <string>("SelectMethod");


            var methodParams   = context.NccGetPropertyValue <ExpandoObject>("SelectParameters") as IDictionary <string, object>;
            var callParameters = methodParams.ToDictionary(x => x.Key, x => x.Value).NccToExpando() as IDictionary <string, object>;

            var filters = context.NccGetPropertyValue <Dictionary <string, string> >("Filters") ?? new Dictionary <string, string>();

            var renderDefault = context.NccGetPropertyValue <bool>("RenderDefault");

            if (!renderDefault)
            {
                AddFiltersToParameters(callParameters, filters);
                extraParameters?.Invoke(callParameters, context);
            }

            var result = dbService.NccInvokeMethod(methodName, (ExpandoObject)callParameters);

            if (result != null)
            {
                ProcessDataResult(context, result, dataResult);
            }
        }
コード例 #6
0
        public override async Task ProcessAsync(TagHelperContext tagContext, TagHelperOutput output)
        {
            if (Context == null)
            {
                throw new Exception("The NccSelectContext is null... Please check the reference.");
            }

            object service = null;

            if (!string.IsNullOrEmpty(Context.EventHandlerClass))
            {
                service = NccReflectionService.NccGetClassInstance(Context.EventHandlerClass, null);
            }

            service?.NccInvokeMethod(NccEventsEnum.Load, new object[] { new NccEventArgs {
                                                                            NccControlContext = Context, ViewContext = ViewContext
                                                                        } });

            if (string.IsNullOrEmpty(Context.Id))
            {
                Context.Id = Guid.NewGuid().ToString();
            }

            output.TagName = "select";
            output.Attributes.SetAttribute("id", Context.Id);

            if (!string.IsNullOrEmpty(DataValue))
            {
                Context.DataValue = DataValue;
            }
            if (string.IsNullOrEmpty(Context.DataValue))
            {
                throw new Exception($"Please set the DataValue property on NccSelect with id {Context.Id}.");
            }

            if (!string.IsNullOrEmpty(TextValue))
            {
                Context.TextValue = TextValue;
            }
            if (string.IsNullOrEmpty(Context.TextValue))
            {
                throw new Exception($"Please set the TextValue property on NccSelect with id {Context.Id}.");
            }

            if (!string.IsNullOrEmpty(FirstItem))
            {
                Context.FirstItem = FirstItem;
            }

            if (Context.Visible)
            {
                NccActionsService.ExtraParameters <NccSelectContext> setExtraParameters = SelectService.GetExtraParameters;
                NccActionsService.DataResult <NccSelectContext>      setDataResult      = SelectService.SetDataResult;
                NccControlsService.BindData(Context, ViewContext.HttpContext, setExtraParameters, setDataResult);

                output.Content.AppendHtml(await output.GetChildContentAsync());

                if (!string.IsNullOrEmpty(Context.FirstItem))
                {
                    var select = new TagBuilder("option")
                    {
                        Attributes = { { "value", "" } }
                    };
                    select.InnerHtml.SetContent(Context.FirstItem);

                    service?.NccInvokeMethod(NccSelectEventsEnum.OptionBound.ToString(), new object[] { new NccEventArgs {
                                                                                                            NccControlContext = Context, ViewContext = ViewContext, DataObjects = Context.DataObjects
                                                                                                        } });

                    output.Content.AppendHtml(select);
                }


                //var data = Context.DataObjects.GetType().ToString().Contains("Microsoft.EntityFrameworkCore.Internal.InternalDbSet") ||
                //    Context.DataObjects.GetType().ToString().Contains("Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable") ?
                //    ((IQueryable<object>)Context.DataObjects).ToList() : Context.DataObjects as IList;
                var data = Context.DataObjects as IList;

                if (data?.Count > 0)
                {
                    foreach (var elem in data)
                    {
                        var val    = elem.NccGetPropertyValue <string>(Context.DataValue);
                        var txt    = elem.NccGetPropertyValue <string>(Context.TextValue);
                        var select = new TagBuilder("option")
                        {
                            Attributes = { { "value", val } }
                        };
                        select.InnerHtml.Append(txt);
                        if (val == SelectedValue)
                        {
                            select.Attributes.Add("selected", "selected");
                        }

                        service?.NccInvokeMethod(NccSelectEventsEnum.OptionBound.ToString(), new object[] { new NccEventArgs {
                                                                                                                NccControlContext = Context, ViewContext = ViewContext, DataObjects = Context.DataObjects
                                                                                                            } });

                        output.Content.AppendHtml(select);
                    }
                }
                else
                {
                    output.Attributes.Add("disabled", "disabled");
                }

                service?.NccInvokeMethod(NccEventsEnum.PreRender, new object[] { new NccEventArgs {
                                                                                     NccControlContext = Context, ViewContext = ViewContext
                                                                                 } });

                output.PostContent.AppendHtml(NccControlsService.GetEncodedContext(_protector, Context.Id, Context));
                output.PostContent.AppendHtml(NccControlsService.GetAjaxLoaderOverlay());
                output.PostContent.AppendHtml(NccControlsService.GetAjaxLoaderImage());


                var divContainer = new TagBuilder("div")
                {
                    Attributes    = { { "id", Context.Id }, { "style", "position:relative" } },
                    TagRenderMode = TagRenderMode.StartTag
                };
                output.PreElement.AppendHtml(divContainer);

                var endDivContainer = new TagBuilder("div")
                {
                    TagRenderMode = TagRenderMode.EndTag
                };
                output.PostElement.AppendHtml(endDivContainer);
            }
        }