예제 #1
0
        private DotvvmControl GetItem(IDotvvmRequestContext context, object item = null, int?index = null, bool allowMemoizationRetrive = false, bool allowMemoizationStore = false)
        {
            if (allowMemoizationRetrive && item != null && childrenCache.TryGetValue(item, out var container2) && container2.Parent == null)
            {
                Debug.Assert(item == container2.GetValueRaw(DataContextProperty));
                SetUpServerItem(context, item, (int)index, container2);
                return(container2);
            }

            var container = new DataItemContainer();

            container.SetDataContextTypeFromDataSource(GetBinding(DataSourceProperty));
            if (item == null && index == null)
            {
                SetUpClientItem(container);
            }
            else
            {
                SetUpServerItem(context, item, (int)index, container);
            }

            ItemTemplate.BuildContent(context, container);

            // write it to the cache after the content is build. If it would be before that, exception could be suppressed
            if (allowMemoizationStore && item != null)
            {
                // this GetValue call adds the value without raising exception when the value is already added...
                childrenCache.GetValue(item, _ => container);
            }

            return(container);
        }
예제 #2
0
        private void DataBind(IDotvvmRequestContext context)
        {
            Children.Clear();
            emptyDataContainer = null;
            head = null;

            var dataSourceBinding = GetDataSourceBinding();
            var dataSource        = DataSource;

            var sortCommand = SortChanged;

            sortCommand ??=
            typeof(ISortableGridViewDataSet).IsAssignableFrom((GetBinding(DataSourceProperty) as IStaticValueBinding)?.ResultType)
                  ? (Action <string?>)SortChangedCommand
                  : null;

            sortCommand ??= s =>
            throw new DotvvmControlException(this, "Cannot sort when DataSource is null.");

            CreateHeaderRow(context, sortCommand);

            var index = 0;

            if (dataSource != null)
            {
                foreach (var item in GetIEnumerableFromDataSource() !)
                {
                    // create row
                    var placeholder = new DataItemContainer {
                        DataItemIndex = index
                    };
                    placeholder.SetDataContextTypeFromDataSource(dataSourceBinding);
                    placeholder.DataContext = item;
                    placeholder.SetValue(Internal.PathFragmentProperty, GetPathFragmentExpression() + "/[" + index + "]");
                    placeholder.ID = index.ToString();
                    Children.Add(placeholder);
                    CreateRowWithCells(context, placeholder);

                    index++;
                }
                numberOfRows = index;
            }
            else
            {
                numberOfRows = 0;
            }

            // add empty item
            if (EmptyDataTemplate != null)
            {
                emptyDataContainer = new EmptyData();
                emptyDataContainer.SetValue(EmptyData.VisibleProperty, GetValueRaw(VisibleProperty));
                emptyDataContainer.SetBinding(DataSourceProperty, dataSourceBinding);
                EmptyDataTemplate.BuildContent(context, emptyDataContainer);
                Children.Add(emptyDataContainer);
            }
        }
예제 #3
0
        private DotvvmControl GetItem(IDotvvmRequestContext context, object item = null, int?index = null)
        {
            var container = new DataItemContainer();

            container.SetDataContextTypeFromDataSource(GetBinding(DataSourceProperty));
            if (item == null && index == null)
            {
                SetUpClientItem(container);
            }
            else
            {
                SetUpServerItem(context, item, (int)index, container);
            }

            ItemTemplate.BuildContent(context, container);
            return(container);
        }
예제 #4
0
        protected override void RenderContents(IHtmlWriter writer, IDotvvmRequestContext context)
        {
            // render the header
            head?.Render(writer, context);

            // render body
            if (!RenderOnServer)
            {
                writer.AddKnockoutForeachDataBind("dotvvm.evaluator.getDataSourceItems($gridViewDataSet)");
            }
            writer.RenderBeginTag("tbody");

            // render contents
            if (RenderOnServer)
            {
                // render on server
                var index = 0;
                foreach (var child in Children.Except(new[] { head, emptyDataContainer }))
                {
                    child.Render(writer, context);
                    index++;
                }
            }
            else
            {
                // render on client
                if (InlineEditing)
                {
                    var propertySerialization = context.Services
                                                .GetRequiredService <IPropertySerialization>();
                    var primaryKeyProperty     = ResolvePrimaryKeyProperty();
                    var primaryKeyPropertyName = propertySerialization.ResolveName(primaryKeyProperty);

                    var placeholder = new DataItemContainer {
                        DataContext = null
                    };
                    placeholder.SetDataContextTypeFromDataSource(GetBinding(DataSourceProperty));
                    placeholder.SetValue(Internal.PathFragmentProperty, GetPathFragmentExpression() + "/[$index]");
                    placeholder.SetValue(Internal.ClientIDFragmentProperty, GetValueRaw(Internal.CurrentIndexBindingProperty));
                    writer.WriteKnockoutDataBindComment("if", "ko.unwrap(ko.unwrap($gridViewDataSet).RowEditOptions().EditRowId) " +
                                                        $"!== ko.unwrap($data['{primaryKeyPropertyName}'])");
                    CreateTemplates(context, placeholder);
                    Children.Add(placeholder);
                    placeholder.Render(writer, context);
                    writer.WriteKnockoutDataBindEndComment();

                    var placeholderEdit = new DataItemContainer {
                        DataContext = null
                    };
                    placeholderEdit.SetDataContextTypeFromDataSource(GetBinding(DataSourceProperty));
                    placeholderEdit.SetValue(Internal.PathFragmentProperty, GetPathFragmentExpression() + "/[$index]");
                    placeholderEdit.SetValue(Internal.ClientIDFragmentProperty, GetValueRaw(Internal.CurrentIndexBindingProperty));
                    writer.WriteKnockoutDataBindComment("if", "ko.unwrap(ko.unwrap($gridViewDataSet).RowEditOptions().EditRowId) " +
                                                        $"=== ko.unwrap($data['{primaryKeyPropertyName}'])");
                    CreateTemplates(context, placeholderEdit, true);
                    Children.Add(placeholderEdit);
                    placeholderEdit.Render(writer, context);
                    writer.WriteKnockoutDataBindEndComment();
                }
                else
                {
                    var placeholder = new DataItemContainer {
                        DataContext = null
                    };
                    placeholder.SetDataContextTypeFromDataSource(GetBinding(DataSourceProperty));
                    placeholder.SetValue(Internal.PathFragmentProperty, GetPathFragmentExpression() + "/[$index]");
                    placeholder.SetValue(Internal.ClientIDFragmentProperty, GetValueRaw(Internal.CurrentIndexBindingProperty));
                    Children.Add(placeholder);
                    CreateRowWithCells(context, placeholder);
                    placeholder.Render(writer, context);
                }
            }

            writer.RenderEndTag();
        }
예제 #5
0
        private void DataBind(IDotvvmRequestContext context)
        {
            Children.Clear();
            emptyDataContainer = null;
            head = null;

            var dataSourceBinding = GetDataSourceBinding();
            var dataSource        = DataSource;

            var sortCommand =
                dataSource is ISortableGridViewDataSet sortableSet && sortableSet.SortingOptions is ISortingOptions sortOptions ?
                expr => {
                if (sortOptions.SortExpression == expr)
                {
                    sortOptions.SortDescending ^= true;
                }
                else
                {
                    sortOptions.SortExpression = expr;
                    sortOptions.SortDescending = false;
                }
                (sortableSet as IPageableGridViewDataSet)?.GoToFirstPage();
            }
            :
            SortChanged;

            // WORKAROUND: DataSource is null => don't throw exception
            if (sortCommand == null && dataSource == null)
            {
                sortCommand = s => {
                    throw new DotvvmControlException(this, "Cannot sort when DataSource is null.");
                };
            }

            CreateHeaderRow(context, sortCommand);

            var index = 0;

            if (dataSource != null)
            {
                var itemBinding = GetItemBinding();
                foreach (var item in GetIEnumerableFromDataSource())
                {
                    // create row
                    var placeholder = new DataItemContainer {
                        DataItemIndex = index
                    };
                    placeholder.SetDataContextTypeFromDataSource(dataSourceBinding);
                    placeholder.DataContext = item;
                    placeholder.SetValue(Internal.PathFragmentProperty, GetPathFragmentExpression() + "/[" + index + "]");
                    placeholder.ID = index.ToString();
                    Children.Add(placeholder);
                    CreateRowWithCells(context, placeholder);

                    index++;
                }
                numberOfRows = index;
            }
            else
            {
                numberOfRows = 0;
            }

            // add empty item
            if (EmptyDataTemplate != null)
            {
                emptyDataContainer = new EmptyData();
                emptyDataContainer.SetValue(EmptyData.VisibleProperty, GetValueRaw(VisibleProperty));
                emptyDataContainer.SetBinding(DataSourceProperty, dataSourceBinding);
                EmptyDataTemplate.BuildContent(context, emptyDataContainer);
                Children.Add(emptyDataContainer);
            }
        }