Skip to content

Commit

Permalink
+ New feature to freeze the results when paging, to avoid data shifting
Browse files Browse the repository at this point in the history
  • Loading branch information
artiomchi committed Dec 6, 2015
1 parent 8e193c8 commit b998bb5
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 7 deletions.
1 change: 1 addition & 0 deletions src/FlexLabs.Web.TablePager/ITableModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ public interface ITableModel
void UpdateSorter();
Object SortBy { get; set; }
Boolean? SortAsc { get; set; }
Int64? FirstItemID { get; set; }
}
}
45 changes: 42 additions & 3 deletions src/FlexLabs.Web.TablePager/TableModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web.Mvc;

namespace FlexLabs.Web.TablePager
Expand Down Expand Up @@ -43,15 +44,18 @@ public TableModel(TSorter defaultSorter, Boolean defaultAscending, Boolean pagin
DefaultSortAsc = defaultAscending;
PagingEnabled = pagingEnabled;
}
private TSorter DefaultSortBy;
private Boolean DefaultSortAsc;
private Boolean PagingEnabled = true;
private readonly TSorter DefaultSortBy;
private readonly Boolean DefaultSortAsc;
private readonly Boolean PagingEnabled = true;
private Func<TSource, Int64> FirstID64Selector = null;
private Func<TSource, Int32> FirstID32Selector = null;

public TSorter? ChangeSort { get; set; }
public TSorter? SortBy { get; set; }
public Boolean? SortAsc { get; set; }
public Int32? PageSize { get; set; }
public Int32? Page { get; set; }
public Int64? FirstItemID { get; set; }
object ITableModel.SortBy { get { return SortBy; } set { SortBy = (TSorter?)value; } }

public IPagedList<TModel> PageItems;
Expand Down Expand Up @@ -87,6 +91,41 @@ public void SetPageItems(IEnumerable<TSource> items, Int32? totalItemCount = nul
PageItems = new StaticPagedList<TModel>(dataSet.OfType<TModel>(), pageNumber, pageSize, totalItemCount ?? dataSet.Count());
else
PageItems = new StaticPagedList<TModel>(dataSet.Select(i => TranslateItem(i)), pageNumber, pageSize, totalItemCount ?? dataSet.Count());

if (PageItems.TotalItemCount > 0 && (FirstID32Selector != null || FirstID64Selector != null))
{
FirstItemID = FirstID64Selector != null
? items.Select(FirstID64Selector).FirstOrDefault()
: items.Select(FirstID32Selector).FirstOrDefault();
}
}

public Int64? GetFirstItemID(Func<TSource, Int64> idSelector)
{
var showNewResults = !Page.HasValue && !ChangeSort.HasValue;

if (showNewResults)
{
FirstID64Selector = idSelector;
return null;
}

return FirstItemID;
}

public Int32? GetFirstItemID(Func<TSource, Int32> idSelector)
{
var showNewResults = !Page.HasValue && !ChangeSort.HasValue;

if (showNewResults)
{
FirstID32Selector = idSelector;
return null;
}

if (FirstItemID.HasValue)
return Convert.ToInt32(FirstItemID.Value);
return null;
}

public void UpdateSorter()
Expand Down
3 changes: 3 additions & 0 deletions src/FlexLabs.Web.TablePager/TablePager.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,7 @@
@* Mvc Seems to prioritise the values from the request model, over the values changed in the code, so I have to go a bit "manual" here :) [Artiom] *@
<input type="hidden" name="SortBy" value="@model.SortBy" />
<input type="hidden" name="SortAsc" value="@model.SortAsc.ToString()" />
if (model.FirstItemID.HasValue) {
<input type="hidden" name="FirstItemID" value="@model.FirstItemID" />
}
}
29 changes: 28 additions & 1 deletion src/FlexLabs.Web.TablePager/TablePager.generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -600,13 +600,40 @@ public static System.Web.WebPages.HelperResult FormHidden(ITableModel model)
#line 76 "..\..\TablePager.cshtml"
if (model.FirstItemID.HasValue)
{
#line default
#line hidden
WriteLiteralTo(__razor_helper_writer, " <input");
WriteLiteralTo(__razor_helper_writer, " type=\"hidden\"");
WriteLiteralTo(__razor_helper_writer, " name=\"FirstItemID\"");
WriteAttributeTo(__razor_helper_writer, "value", Tuple.Create(" value=\"", 3137), Tuple.Create("\"", 3163)
#line 77 "..\..\TablePager.cshtml"
, Tuple.Create(Tuple.Create("", 3145), Tuple.Create<System.Object, System.Int32>(model.FirstItemID
#line default
#line hidden
, 3145), false)
);
WriteLiteralTo(__razor_helper_writer, " />\r\n");
#line 78 "..\..\TablePager.cshtml"
}
#line default
#line hidden
});

#line 76 "..\..\TablePager.cshtml"
#line 79 "..\..\TablePager.cshtml"
}
#line default
#line hidden
Expand Down
7 changes: 4 additions & 3 deletions src/FlexLabs.Web.TablePager/project.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
{
"version": "2.0.0-*",
"description": "",
"version": "2.0.5-*",
"title": "FlexLabs Table Pager",
"description": "Helps manage paging and page sorting in a ASP.NET MVC project. Works best when used in conjunction with FlexLabs.Util",
"authors": [ "Artiom Chilaru" ],
"tags": [ "" ],
"projectUrl": "",
"projectUrl": "https://github.com/artiomchi/TablePager",
"licenseUrl": "",

"dependencies": {
Expand Down

0 comments on commit b998bb5

Please sign in to comment.