mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-10-15 19:54:57 +08:00
Adding paging to search
--HG-- branch : dev
This commit is contained in:
@@ -14,14 +14,28 @@ namespace Orchard.Search.Controllers {
|
|||||||
_contentManager = contentManager;
|
_contentManager = contentManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ActionResult Index(string q) {
|
public ActionResult Index(string q, int page = 0, int pageSize = 0) {
|
||||||
var searchViewModel = new SearchViewModel {Query = q};
|
var take = pageSize > 0 ? pageSize : 10;
|
||||||
|
var skip = (page > 0 ? page - 1 : 0) * take;
|
||||||
|
var searchViewModel = new SearchViewModel {
|
||||||
|
Query = q,
|
||||||
|
Page = page > 0 ? page : 1,
|
||||||
|
PageSize = take
|
||||||
|
};
|
||||||
|
|
||||||
var results = _searchService.Query(q);
|
var results = _searchService.Query(q);
|
||||||
searchViewModel.Results = results.Select(result => new SearchResultViewModel {
|
|
||||||
Content = _contentManager.BuildDisplayModel(_contentManager.Get(result.Id), "SummaryForSearch"),
|
searchViewModel.Count = results.Count();
|
||||||
SearchHit = result
|
searchViewModel.TotalPageCount = searchViewModel.Count/searchViewModel.PageSize;
|
||||||
}).ToList();
|
//todo: deal with page requests beyond result count
|
||||||
|
searchViewModel.ResultsPage = results
|
||||||
|
.Select(result => new SearchResultViewModel {
|
||||||
|
Content = _contentManager.BuildDisplayModel(_contentManager.Get(result.Id), "SummaryForSearch"),
|
||||||
|
SearchHit = result
|
||||||
|
})
|
||||||
|
.Skip(skip)
|
||||||
|
.Take(take)
|
||||||
|
.ToList();
|
||||||
|
|
||||||
return View(searchViewModel);
|
return View(searchViewModel);
|
||||||
}
|
}
|
||||||
|
@@ -3,7 +3,11 @@ using Orchard.Mvc.ViewModels;
|
|||||||
|
|
||||||
namespace Orchard.Search.ViewModels {
|
namespace Orchard.Search.ViewModels {
|
||||||
public class SearchViewModel : BaseViewModel {
|
public class SearchViewModel : BaseViewModel {
|
||||||
public IEnumerable<SearchResultViewModel> Results { get; set; }
|
public IEnumerable<SearchResultViewModel> ResultsPage { get; set; }
|
||||||
|
public int Count { get; set; }
|
||||||
|
public int Page { get; set; }
|
||||||
|
public int PageSize { get; set; }
|
||||||
|
public int TotalPageCount { get; set; }
|
||||||
public string Query { get; set; }
|
public string Query { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
@@ -4,8 +4,9 @@ Html.RegisterStyle("search.css"); %>
|
|||||||
<h1><%=Html.TitleForPage(T("Search").Text)%></h1><%
|
<h1><%=Html.TitleForPage(T("Search").Text)%></h1><%
|
||||||
Html.Zone("search");
|
Html.Zone("search");
|
||||||
if (!string.IsNullOrWhiteSpace(Model.Query)) { %>
|
if (!string.IsNullOrWhiteSpace(Model.Query)) { %>
|
||||||
<p class="search-summary"><%=T("<em>{0}</em> results", Model.Results.Count()) %></p><%
|
<p class="search-summary"><%=T("<em>{0} - {1}</em> of <em>{2}</em> results", (Model.Page - 1) * Model.PageSize + 1, Model.Page * Model.PageSize > Model.Count ? Model.Count : Model.Page * Model.PageSize, Model.Count)%></p><%
|
||||||
}
|
}
|
||||||
if (Model.Results.Count() > 0) { %>
|
if (Model.ResultsPage.Count() > 0) { %>
|
||||||
<%=Html.UnorderedList(Model.Results, (r, i) => Html.DisplayForItem(r.Content).ToHtmlString(), "search-results contentItems") %><%
|
<%=Html.UnorderedList(Model.ResultsPage, (r, i) => Html.DisplayForItem(r.Content).ToHtmlString(), "search-results contentItems") %>
|
||||||
|
<%=Html.Pager(Model.TotalPageCount, Model.Page, new {q = Model.Query}, "<", ">", false) %><%
|
||||||
} %>
|
} %>
|
@@ -40,6 +40,66 @@ namespace Orchard.Mvc.Html {
|
|||||||
|
|
||||||
return MvcHtmlString.Create(builder.ToString(TagRenderMode.Normal));
|
return MvcHtmlString.Create(builder.ToString(TagRenderMode.Normal));
|
||||||
}
|
}
|
||||||
|
#region Pager
|
||||||
|
|
||||||
|
public static string Pager(this HtmlHelper html, int pageCount, int currentPage, object values, string previousText, string nextText, bool alwaysShowPreviousAndNext) {
|
||||||
|
if (pageCount < 2)
|
||||||
|
return "";
|
||||||
|
|
||||||
|
var sb = new StringBuilder(75);
|
||||||
|
var viewContext = html.ViewContext;
|
||||||
|
var rvd = new RouteValueDictionary();
|
||||||
|
|
||||||
|
foreach (var item in viewContext.RouteData.Values) {
|
||||||
|
rvd.Add(item.Key, item.Value);
|
||||||
|
}
|
||||||
|
|
||||||
|
var urlHelper = new UrlHelper(viewContext.RequestContext);
|
||||||
|
|
||||||
|
if (values != null) {
|
||||||
|
var rvd2 = new RouteValueDictionary(values);
|
||||||
|
|
||||||
|
foreach (var item in rvd2) {
|
||||||
|
rvd[item.Key] = item.Value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
sb.Append("<p class=\"pager\">");
|
||||||
|
|
||||||
|
if (currentPage > 1 || alwaysShowPreviousAndNext) {
|
||||||
|
if (currentPage == 2)
|
||||||
|
rvd.Remove("page");
|
||||||
|
else
|
||||||
|
rvd["page"] = currentPage - 1;
|
||||||
|
|
||||||
|
sb.AppendFormat(" <a href=\"{1}\" class=\"previous\">{0}</a>", previousText,
|
||||||
|
urlHelper.RouteUrl(rvd));
|
||||||
|
}
|
||||||
|
|
||||||
|
//todo: when there are many pages (> 15?) maybe do something like 1 2 3...6 7 8...13 14 15
|
||||||
|
for (var p = 1; p <= pageCount; p++) {
|
||||||
|
if (p == currentPage) {
|
||||||
|
sb.AppendFormat(" <span>{0}</span>", p);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
rvd["page"] = p;
|
||||||
|
sb.AppendFormat(" <a href=\"{1}\">{0}</a>", p,
|
||||||
|
urlHelper.RouteUrl(rvd));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (currentPage < pageCount || alwaysShowPreviousAndNext) {
|
||||||
|
rvd["page"] = currentPage + 1;
|
||||||
|
sb.AppendFormat("<a href=\"{1}\" class=\"next\">{0}</a>", nextText,
|
||||||
|
urlHelper.RouteUrl(rvd));
|
||||||
|
}
|
||||||
|
|
||||||
|
sb.Append("</p>");
|
||||||
|
|
||||||
|
return sb.ToString();
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
#region UnorderedList
|
#region UnorderedList
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user