mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-10-15 19:54:57 +08:00
A bit of an improvement in paging
- generic collection and display but only used for search results at the moment and does nothing for other queries. in other words, build your own IPageOfItems --HG-- branch : dev
This commit is contained in:
@@ -15,29 +15,17 @@ namespace Orchard.Search.Controllers {
|
||||
_contentManager = contentManager;
|
||||
}
|
||||
|
||||
public ActionResult Index(string q, int page = 0, int pageSize = 0) {
|
||||
var take = pageSize > 0 ? pageSize : 10;
|
||||
var skip = (page > 0 ? page - 1 : 0) * take;
|
||||
public ActionResult Index(string q, int page = 1, int pageSize = 10) {
|
||||
var searchViewModel = new SearchViewModel {
|
||||
Query = q,
|
||||
Page = page > 0 ? page : 1,
|
||||
PageSize = take
|
||||
DefaultPageSize = 10, // <- yeah, I know :|
|
||||
PageOfResults = _searchService.Query(q, page, pageSize, searchHit => new SearchResultViewModel {
|
||||
Content = _contentManager.BuildDisplayModel(_contentManager.Get(searchHit.Id), "SummaryForSearch"),
|
||||
SearchHit = searchHit
|
||||
})
|
||||
};
|
||||
|
||||
var results = _searchService.Query(q, skip, take);
|
||||
|
||||
if (results == null)
|
||||
return View(searchViewModel);
|
||||
|
||||
searchViewModel.Count = results.TotalCount;
|
||||
searchViewModel.TotalPageCount = (int)Math.Ceiling((decimal)searchViewModel.Count/searchViewModel.PageSize);
|
||||
//todo: deal with page requests beyond result count
|
||||
searchViewModel.ResultsPage = results.Page
|
||||
.Select(result => new SearchResultViewModel {
|
||||
Content = _contentManager.BuildDisplayModel(_contentManager.Get(result.Id), "SummaryForSearch"),
|
||||
SearchHit = result
|
||||
})
|
||||
.ToList();
|
||||
|
||||
return View(searchViewModel);
|
||||
}
|
||||
|
@@ -1,10 +1,11 @@
|
||||
using System;
|
||||
using Orchard.Search.Models;
|
||||
using Orchard.Collections;
|
||||
using Orchard.Indexing;
|
||||
|
||||
namespace Orchard.Search.Services {
|
||||
public interface ISearchService : IDependency {
|
||||
bool HasIndexToManage { get; }
|
||||
ISearchResult Query(string query, int skip, int? take);
|
||||
IPageOfItems<T> Query<T>(string query, int skip, int? take, Func<ISearchHit, T> shapeResult);
|
||||
void RebuildIndex();
|
||||
void UpdateIndex();
|
||||
DateTime GetIndexUpdatedUtc();
|
||||
|
@@ -1,5 +1,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Orchard.Collections;
|
||||
using Orchard.Indexing;
|
||||
using Orchard.Localization;
|
||||
using Orchard.Search.Models;
|
||||
@@ -27,23 +29,27 @@ namespace Orchard.Search.Services
|
||||
get { return _indexManager.HasIndexProvider(); }
|
||||
}
|
||||
|
||||
ISearchResult ISearchService.Query(string query, int skip, int? take) {
|
||||
IPageOfItems<T> ISearchService.Query<T>(string query, int page, int? pageSize, Func<ISearchHit, T> shapeResult) {
|
||||
if (string.IsNullOrWhiteSpace(query) || !_indexManager.HasIndexProvider())
|
||||
return null;
|
||||
|
||||
var searchBuilder = _indexManager.GetSearchIndexProvider().CreateSearchBuilder(SearchIndexName)
|
||||
var searchBuilder = _indexManager.GetSearchIndexProvider().CreateSearchBuilder(SearchIndexName)
|
||||
.WithField("title", query)
|
||||
.WithField("body", query);
|
||||
|
||||
var totalCount = searchBuilder.Count();
|
||||
if (take != null)
|
||||
if (pageSize != null)
|
||||
searchBuilder = searchBuilder
|
||||
.Slice(skip, (int)take);
|
||||
.Slice((page > 0 ? page - 1 : 0) * (int)pageSize, (int)pageSize);
|
||||
|
||||
return new SearchResult {
|
||||
Page = searchBuilder.Search(),
|
||||
TotalCount = totalCount
|
||||
|
||||
var pageOfItems = new PageOfItems<T>(searchBuilder.Search().Select(shapeResult)) {
|
||||
PageNumber = page,
|
||||
PageSize = pageSize != null ? (int) pageSize : totalCount,
|
||||
TotalItemCount = totalCount
|
||||
};
|
||||
|
||||
return pageOfItems;
|
||||
}
|
||||
|
||||
void ISearchService.RebuildIndex() {
|
||||
|
@@ -1,13 +1,10 @@
|
||||
using System.Collections.Generic;
|
||||
using Orchard.Collections;
|
||||
using Orchard.Mvc.ViewModels;
|
||||
|
||||
namespace Orchard.Search.ViewModels {
|
||||
public class SearchViewModel : BaseViewModel {
|
||||
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 int DefaultPageSize { get; set; }
|
||||
public IPageOfItems<SearchResultViewModel> PageOfResults { get; set; }
|
||||
}
|
||||
}
|
@@ -4,14 +4,14 @@ Html.RegisterStyle("search.css"); %>
|
||||
<h1><%=Html.TitleForPage(T("Search").Text)%></h1><%
|
||||
Html.Zone("search");
|
||||
if (!string.IsNullOrWhiteSpace(Model.Query)) {
|
||||
if (Model.Count == 0) { %>
|
||||
if (Model.PageOfResults.Count() == 0) { %>
|
||||
<p class="search-summary"><%=T("<em>zero</em> results") %></p><%
|
||||
}
|
||||
else { %>
|
||||
<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><%
|
||||
<p class="search-summary"><%=T("<em>{0} - {1}</em> of <em>{2}</em> results", Model.PageOfResults.StartPosition, Model.PageOfResults.EndPosition, Model.PageOfResults.TotalItemCount)%></p><%
|
||||
}
|
||||
}
|
||||
if (Model.ResultsPage != null && Model.ResultsPage.Count() > 0) { %>
|
||||
<%=Html.UnorderedList(Model.ResultsPage, (r, i) => Html.DisplayForItem(r.Content).ToHtmlString(), "search-results contentItems") %>
|
||||
<%=Html.Pager(Model.TotalPageCount, Model.Page, new {q = Model.Query}) %><%
|
||||
if (Model.PageOfResults != null && Model.PageOfResults.Count() > 0) { %>
|
||||
<%=Html.UnorderedList(Model.PageOfResults, (r, i) => Html.DisplayForItem(r.Content).ToHtmlString() , "search-results contentItems") %>
|
||||
<%=Html.Pager(Model.PageOfResults, Model.PageOfResults.PageNumber, Model.DefaultPageSize, new {q = Model.Query}) %><%
|
||||
} %>
|
Reference in New Issue
Block a user