Beispiel #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Grid{T}"/> class.
        /// </summary>
        /// <param name="viewContext">The view context.</param>
        /// <param name="clientSideObjectWriterFactory">The client side object writer factory.</param>
        /// <param name="urlGenerator">The URL generator.</param>
        /// <param name="builderFactory">The builder factory.</param>
        public Grid(ViewContext viewContext, IClientSideObjectWriterFactory clientSideObjectWriterFactory, IUrlGenerator urlGenerator,
                    ILocalizationService localizationService, IGridHtmlBuilderFactory htmlBuilderFactory)
            : base(viewContext, clientSideObjectWriterFactory)
        {
            this.htmlBuilderFactory = htmlBuilderFactory;

            UrlGenerator = urlGenerator;

            PrefixUrlParameters = true;
            RowTemplate         = new HtmlTemplate <T>();
            DataProcessor       = new GridDataProcessor(this);
            Columns             = new List <GridColumnBase <T> >();
            DataKeys            = new List <IGridDataKey <T> >();

            Paging             = new GridPagingSettings(this);
            Sorting            = new GridSortSettings(this);
            Scrolling          = new GridScrollingSettings();
            KeyboardNavigation = new GridKeyboardNavigationSettings(this);
            ColumnContextMenu  = new GridColumnContextMenuSettings(this);
            Filtering          = new GridFilteringSettings();
            Editing            = new GridEditingSettings <T>(this)
            {
                PopUp = new Window(viewContext, clientSideObjectWriterFactory, new WindowHtmlBuilderFactory())
                {
                    Modal     = true,
                    Draggable = true
                }
            };

            Grouping   = new GridGroupingSettings(this);
            Resizing   = new GridResizingSettings();
            Reordering = new GridReorderingSettings();

            TableHtmlAttributes = new RouteValueDictionary();

            DataBinding = new GridDataBindingSettings(this);

            Footer  = true;
            IsEmpty = true;

            ClientEvents = new GridClientEvents();
            Selection    = new GridSelectionSettings();
            ScriptFileNames.AddRange(new[] { "telerik.common.js", "telerik.grid.js" });

            ToolBar           = new GridToolBarSettings <T>(this);
            Localization      = new GridLocalization(localizationService, CultureInfo.CurrentUICulture);
            NoRecordsTemplate = new HtmlTemplate();

            ValidationMetadata = new Dictionary <string, object>();

            AutoGenerateColumns = true;
        }
        public GridDataProcessorTests()
        {
            valueProvider = new Dictionary<string, ValueProviderResult>();

            context = new Mock<IGridBindingContext>();
            context.Setup(c => c.Prefix(GridUrlParameters.OrderBy)).Returns(GridUrlParameters.OrderBy);
            context.Setup(c => c.Prefix(GridUrlParameters.GroupBy)).Returns(GridUrlParameters.GroupBy);
            context.Setup(c => c.Prefix(GridUrlParameters.CurrentPage)).Returns(GridUrlParameters.CurrentPage);
            context.Setup(c => c.Prefix(GridUrlParameters.Filter)).Returns(GridUrlParameters.Filter);
            context.Setup(c => c.Prefix(GridUrlParameters.Aggregates)).Returns(GridUrlParameters.Aggregates);
            context.Setup(c => c.Prefix(GridUrlParameters.PageSize)).Returns(GridUrlParameters.PageSize);
            context.Setup(c => c.GroupDescriptors).Returns(() => new List<GroupDescriptor>());
            context.Setup(c => c.SortDescriptors).Returns(() => new List<SortDescriptor>());
            context.Setup(c => c.FilterDescriptors).Returns(() => new CompositeFilterDescriptor[] { });

            context.SetupGet(c => c.Controller).Returns(new ControllerTestDouble(valueProvider, new ViewDataDictionary()));
            context.Setup(c => c.PageSize).Returns(10);
            dataProcessor = new GridDataProcessor(context.Object);
        }
        public void OnActionExecuted(ActionExecutedContext filterContext)
        {
            if (!filterContext.HttpContext.Request.IsAjaxRequest())
            {
                return;
            }

            var actionResultAdapter = adapterFactory.Create(filterContext.Result);

            if (actionResultAdapter == null)
            {
                return;
            }

            var dataSource = actionResultAdapter.GetDataSource();

            if (dataSource == null)
            {
                return;
            }

            var total = actionResultAdapter.GetTotal();

            var dataProcessor = new GridDataProcessor(new GridActionBindingContext(EnableCustomBinding, filterContext.Controller, dataSource, total));

            var result = new Dictionary<string, object>();
            var dataTableEnumerable = dataSource as GridDataTableWrapper;
            if (dataTableEnumerable != null && dataTableEnumerable.Table != null)
            {
                result["data"] = dataProcessor.ProcessedDataSource.SerializeToDictionary(dataTableEnumerable.Table);
            }
            else
            {
                result["data"] = dataProcessor.ProcessedDataSource;
            }

            result["total"] = dataProcessor.Total;

            var modelState = actionResultAdapter.GetModelState();

            if (modelState != null && !modelState.IsValid)
            {
                result["modelState"] = SerializeErrors(modelState);
            }

            if (EnableCustomBinding)
            {
                var aggregates = actionResultAdapter.GetAggregates();
                if (aggregates != null)
                {
                    result["aggregates"] = aggregates;
                }

            }
            else
            {
                SerializeAggregateResults(result, dataProcessor.AggregatesResults);
            }

            filterContext.Result = resultFactory.Create(result);
        }