Skip to content

Commit

Permalink
Fix DataPager.HideWhenOnlyOnePage
Browse files Browse the repository at this point in the history
  • Loading branch information
exyi committed Nov 11, 2023
1 parent fd99266 commit 52ead0a
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 26 deletions.
33 changes: 16 additions & 17 deletions src/Framework/Framework/Controls/DataPager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,16 +161,15 @@ protected virtual void DataBind(Hosting.IDotvvmRequestContext context)

var dataSetBinding = GetValueBinding(DataSetProperty)!;
var dataSetType = dataSetBinding.ResultType;
ContentWrapper = CreateWrapperList();
Children.Add(ContentWrapper);

var commandType = LoadData is {} ? GridViewDataSetCommandType.StaticCommand : GridViewDataSetCommandType.Command;

pagerBindings = gridViewDataSetBindingProvider.GetDataPagerCommands(this.GetDataContextType().NotNull(), dataSetBinding, commandType);


var enabled = GetValueOrBinding<bool>(EnabledProperty)!;


ContentWrapper = CreateWrapperList();
Children.Add(ContentWrapper);

if (typeof(IPageableGridViewDataSet<IPagingFirstPageCapability>).IsAssignableFrom(dataSetType))
{
GoToFirstPageButton = CreateNavigationButton("««", FirstPageTemplate, enabled, pagerBindings.GoToFirstPage!, context);
Expand Down Expand Up @@ -232,6 +231,15 @@ protected virtual void DataBind(Hosting.IDotvvmRequestContext context)
protected virtual HtmlGenericControl CreateWrapperList()
{
var list = new HtmlGenericControl("ul");

// If Visible property was set to something, it would be overwritten by this
if (HideWhenOnlyOnePage && pagerBindings?.HasMoreThanOnePage is {} hasMoreThanOnePage)
{
if (IsPropertySet(VisibleProperty))
throw new Exception("Visible can't be set on a DataPager when HideWhenOnlyOnePage is true. You can wrap it in an element that hide that or set HideWhenOnlyOnePage to false");
list.SetProperty(HtmlGenericControl.VisibleProperty, hasMoreThanOnePage);
}

return list;
}

Expand Down Expand Up @@ -266,22 +274,13 @@ protected override void AddAttributesToRender(IHtmlWriter writer, IDotvvmRequest
throw new DotvvmControlException(this, "The DataPager control cannot be rendered in the RenderSettings.Mode='Server'.");
}

var dataSetBinding = GetDataSetBinding().GetKnockoutBindingExpression(this, unwrapped: true);
var helperBinding = new KnockoutBindingGroup();
helperBinding.Add("dataSet", dataSetBinding);
if (this.LoadData is {} loadData)
{
var helperBinding = new KnockoutBindingGroup();
helperBinding.Add("dataSet", GetDataSetBinding().GetKnockoutBindingExpression(this, unwrapped: true));
var loadDataExpression = KnockoutHelper.GenerateClientPostbackLambda("LoadData", loadData, this, new PostbackScriptOptions(elementAccessor: "$element", koContext: CodeParameterAssignment.FromIdentifier("$context")));
helperBinding.Add("loadDataSet", loadDataExpression);
}
writer.AddKnockoutDataBind("dotvvm-gridviewdataset", helperBinding.ToString());

// If Visible property was set to something, it will be overwritten by this. TODO: is it how it should behave?
if (HideWhenOnlyOnePage)
{
if (IsPropertySet(VisibleProperty))
throw new Exception("Visible can't be set on a DataPager when HideWhenOnlyOnePage is true. You can wrap it in an element that hide that or set HideWhenOnlyOnePage to false");
writer.AddKnockoutDataBind("visible", $"({dataSetBinding}).PagingOptions().PagesCount() > 1");
writer.AddKnockoutDataBind("dotvvm-gridviewdataset", helperBinding.ToString());
}

if (GetValueBinding(EnabledProperty) is IValueBinding enabledBinding)
Expand Down
1 change: 1 addition & 0 deletions src/Framework/Framework/Controls/DataPagerCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ public class DataPagerBindings
public IStaticValueBinding<IEnumerable<int>>? PageNumbers { get; init; }
public IStaticValueBinding<bool>? IsActivePage { get; init; }
public IStaticValueBinding<string>? PageNumberText { get; init; }
public IStaticValueBinding<bool>? HasMoreThanOnePage { get; init; }
}
}
27 changes: 18 additions & 9 deletions src/Framework/Framework/Controls/GridViewDataSetBindingProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ ParameterExpression CreateParameter(DataContextStack dataContextStack, string na
typeof(int), dataContextStack
);

var isFirstPage = GetValueBindingOrNull<IPageableGridViewDataSet<IPagingFirstPageCapability>, bool>(d => d.PagingOptions.IsFirstPage) ??
GetValueBindingOrNull<IPageableGridViewDataSet<IPagingPreviousPageCapability>, bool>(d => d.PagingOptions.IsFirstPage);
var isLastPage = GetValueBindingOrNull<IPageableGridViewDataSet<IPagingLastPageCapability>, bool>(d => d.PagingOptions.IsLastPage) ??
GetValueBindingOrNull<IPageableGridViewDataSet<IPagingNextPageCapability>, bool>(d => d.PagingOptions.IsLastPage);

return new DataPagerBindings()
{
GoToFirstPage = GetCommandOrNull<IPageableGridViewDataSet<IPagingFirstPageCapability>>(
Expand All @@ -89,14 +94,8 @@ ParameterExpression CreateParameter(DataContextStack dataContextStack, string na
nameof(IPagingPageIndexCapability.GoToPage),
CreateParameter(pageIndexDataContext, "_thisIndex")),

IsFirstPage =
GetValueBindingOrNull<IPageableGridViewDataSet<IPagingFirstPageCapability>, bool>(d => d.PagingOptions.IsFirstPage) ??
GetValueBindingOrNull<IPageableGridViewDataSet<IPagingPreviousPageCapability>, bool>(d => d.PagingOptions.IsFirstPage),

IsLastPage =
GetValueBindingOrNull<IPageableGridViewDataSet<IPagingLastPageCapability>, bool>(d => d.PagingOptions.IsLastPage) ??
GetValueBindingOrNull<IPageableGridViewDataSet<IPagingNextPageCapability>, bool>(d => d.PagingOptions.IsLastPage),

IsFirstPage = isFirstPage,
IsLastPage = isLastPage,
PageNumbers =
GetValueBindingOrNull<IPageableGridViewDataSet<IPagingPageIndexCapability>, IEnumerable<int>>(d => d.PagingOptions.NearPageIndexes),

Expand All @@ -112,7 +111,17 @@ ParameterExpression CreateParameter(DataContextStack dataContextStack, string na
: null,

PageNumberText =
service.Cache.CreateValueBinding<string>("_this + 1", pageIndexDataContext)
service.Cache.CreateValueBinding<string>("_this + 1", pageIndexDataContext),
HasMoreThanOnePage =
GetValueBindingOrNull<IPageableGridViewDataSet<PagingOptions>, bool>(d => d.PagingOptions.PagesCount > 1) ??
(isFirstPage != null && isLastPage != null ?
new ValueBindingExpression<bool>(service, new object[] {
dataContextStack,
new ParsedExpressionBindingProperty(Expression.Not(Expression.AndAlso(
isFirstPage.GetProperty<ParsedExpressionBindingProperty>().Expression,
isLastPage.GetProperty<ParsedExpressionBindingProperty>().Expression
)))
}) : null)
};
}

Expand Down

0 comments on commit 52ead0a

Please sign in to comment.