Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Bartek 5 posts 85 karma points
    Jun 06, 2016 @ 05:03
    Bartek
    0

    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:

    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?

  • Ismail Mayat 4511 posts 10091 karma points MVP 2x admin c-trib
    Jun 06, 2016 @ 08:52
    Ismail Mayat
    0

    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

  • David Armitage 508 posts 2077 karma points
    Jun 06, 2016 @ 11:09
    David Armitage
    101

    Hi,

    The way how it do this is with 3 steps

    • 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>
    

    Hope this helps

Please Sign in or register to post replies

Write your reply to:

Draft