Is there a way in Umbraco to skip and take on the repositories somewhere? I'm trying to page through content results of this call:
var allContent = Services.ContentService.GetContentOfContentType(contenType.Id);
I could, of course, implement pagination based on calling Skip and Take on allContent but the sql query would have already requested everything that matches the contentType.Id.
Is there a way to limit the results? Maybe by using a different API?
Are you displaying the content front end? If so then do not use content service which makes hits to the db. Can you also explain where you are making the call is it in a view / partial view or controller?
Create a class that handles all the search params, stores a list of results, stores page and total result information.
public class JobSearch
{
//search fields
public List<int> IndustryIds { get; set; }
public int? LocationId { get; set; }
public int? WorkTypeId { get; set; }
//return fields
public List<Job> Jobs { get; set; }
public int TotalPages { get; set; }
public int TotalItems { get; set; }
public int ItemsPerPage { get; set; }
public string OrderBy { get; set; }
public string OrderByDescending { get; set; }
}
Next is use Examine to run a search. And return the JobSearch object containing the paged results back to the frontend. Very simple example...
public static JobSearch SearchJobs(JobSearch jobSearch)
{
var searcher = Examine.ExamineManager.Instance.SearchProviderCollection["JobSearcher"];
var criteria = searcher.CreateSearchCriteria();
criteria = GetJobSearchCriteria(criteria, jobSearch);
int skip = (jobSearch.CurrentPage * jobSearch.ItemsPerPage) - jobSearch.ItemsPerPage;
List<Examine.SearchResult> searchResults = searcher.Search(criteria).ToList();
jobSearch.Jobs = (from x in searchResults select new Job(new Node(x.Id), true)).Skip(skip).Take(jobSearch.ItemsPerPage).ToList();
jobSearch.TotalItems = searchResults.Count();
jobSearch.TotalPages = (jobSearch.TotalItems + jobSearch.ItemsPerPage - 1) / jobSearch.ItemsPerPage;
return jobSearch;
}
Finally I use a jquery plugin to display the paging
<script src="~/Frontend/Scripts/Libs/simplePagination/jquery.simplePagination.js" type="text/javascript"></script>
<script>
$(function () {
$('.pager-control').pagination({
items: @jobSearch.TotalItems,
itemsOnPage: @jobSearch.ItemsPerPage,
displayedPages: 6,
edges:0,
onPageClick: function (pageNumber, event) {
//I think I can probably overide the location change and render some ajax content
},
});
});
</script>
ContentService and pagination
Is there a way in Umbraco to skip and take on the repositories somewhere? I'm trying to page through content results of this call:
I could, of course, implement pagination based on calling Skip and Take on allContent but the sql query would have already requested everything that matches the contentType.Id.
Is there a way to limit the results? Maybe by using a different API?
Bartek,
Are you displaying the content front end? If so then do not use content service which makes hits to the db. Can you also explain where you are making the call is it in a view / partial view or controller?
Regards
Ismail
Hi,
The way how it do this is with 3 steps
Hope this helps
is working on a reply...