Copied to clipboard

Flag this post as spam?

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


  • bob baty-barr 1180 posts 1294 karma points MVP
    Jun 08, 2011 @ 18:32
    bob baty-barr
    0

    Examine and pagination help

    I have scoured umbraco.tv the farmcode site and blog and for the life of me cannot figure out how to get pagination to work with examine search results. The farmcode post i think is targeted to developers who 'get it' -- unfortunately, i am not a 'real' developer and therefore, don't really 'get it'.

    here is the code i have thus far...
    http://pastebin.com/u/bootnumlock

    and here is the post on farmcode that i am trying to marry into what i have..
    http://www.farmcode.org/post/2010/08/18/Paging-with-Examine.aspx

    if anyone could shed some light, that would be awesome... in addition to karma, i could get you a beer at #CG11 :)

  • Stefan Kip 1614 posts 4131 karma points c-trib
    Jun 08, 2011 @ 18:40
    Stefan Kip
    1

    Since you're using .NET and the Repeater control (great choice), I'd suggest this post; http://www.4guysfromrolla.com/articles/081804-1.aspx (Adding pagination to a Repeater).

    I'll accept the beer :P

  • bob baty-barr 1180 posts 1294 karma points MVP
    Jun 08, 2011 @ 18:48
    bob baty-barr
    0

    so what about the take/skip integration discussed in the farmCode post... i don't need that?

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Jun 08, 2011 @ 19:37
    Lee Kelleher
    2

    Hi Bob,

    Decided to roll my sleeves up and write some example code... here goes:

    Firstly add a PlaceHolder control to your ASCX, this will be where the HTML controls for your pagination will be displayed:

    <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="Examine.ascx.cs" Inherits="mtt.Search.Examine" %>
    
    <p>
        Your search for :&nbsp;<b><u><%=Term%></u></b>&nbsp;returned&nbsp;
        <i><b><%=TotalResults%></b>&nbsp; result(s)</i>
    </p>
    <asp:Repeater ID="rp_results" runat="server">
    <ItemTemplate>
        <h4><a href='<%# theNodeName(Container)%>'><%#((Examine.SearchResult)Container.DataItem).Fields["nodeName"].ToString()%></a></h4>
        <p>
            <%# HighLightSummary(Container) %>
        </p>    
    </ItemTemplate>
    </asp:Repeater>
    
    <asp:PlaceHolder runat="server" ID="phPagination" />

    Note, that I've renamed "theCount" to "TotalResults" - we'll get on to that in the code bit next.

    Now the code-behind:

    using System;
    using System.Web.UI.WebControls;
    
    using Examine;
    using Examine.LuceneEngine.SearchCriteria;
    using System.Linq;
    using umbraco;
    using umbraco.cms;
    using umbraco.BusinessLogic;
    using umbraco.cms.businesslogic.media;
    
    namespace mtt.Search
    {
        public partial class Examine : System.Web.UI.UserControl
        {
            public int TotalResults { get; set; }
            public string Term { get; set; }
            public int PageNumber { get; set; }
            public int PageSize { get; set; }
    
            protected void Page_Load(object sender, EventArgs e)
            {
                //Create search Criteria
                var sc = ExamineManager.Instance.CreateSearchCriteria();
    
                //define query
                var query = sc.NodeName(Term.MultipleCharacterWildcard())
                            .Or()
                            .Field("bodyText", Term.MultipleCharacterWildcard())
                            .Or()
                            .Field("FileTextContent", Term.MultipleCharacterWildcard())
                            .Compile();
    
                var results = ExamineManager.Instance.SearchProviderCollection["FrontSiteSearcher"].Search(Term, true)
                    .Concat(ExamineManager.Instance.SearchProviderCollection["PDFSearcher"].Search(Term, true));
    
                TotalResults = results.Count();
    
                rp_results.DataSource = results.Skip((PageNumber - 1) * PageSize).Take(PageSize);
                rp_results.DataBind();
    
                // pagination
                BuildPagination();
            }
    
            // helper to build the pagination
            private void BuildPagination()
            {
                decimal d = TotalResults / PageSize;
                var pages = Math.Round(d, 0, MidpointRounding.AwayFromZero);
                var url = Request.Url.AbsolutePath;
    
                if (PageNumber > 1)
                {
                    var prev = new HyperLink() { NavigateUrl = string.Concat(url, "?q=", Term, "&pageNum=", PageNumber - 1), Text = "Prev" };
                    phPagination.Controls.Add(prev);
                }
    
                for (int i = 1; i <= pages; i++)
                {
                    var page = new HyperLink() { NavigateUrl = string.Concat(url, "?q=", Term, "&pageNum=", i), Text = i.ToString() };
    
                    if (PageNumber == i)
                        page.CssClass = "current";
    
                    phPagination.Controls.Add(page);
                }
    
                if (PageNumber < pages)
                {
                    var next = new HyperLink() { NavigateUrl = string.Concat(url, "?q=", Term, "&pageNum=", PageNumber + 1), Text = "Next" };
                    phPagination.Controls.Add(next);
                }
            }
    
            //simple helper to add summary of search result
            public string HighLightSummary(RepeaterItem item)
            {
                var searchResult = (SearchResult)item.DataItem;
    
                if (searchResult.Fields.ContainsKey("bodyText"))
                    return umbraco.library.TruncateString(umbraco.library.StripHtml(searchResult.Fields["bodyText"]), 300, "...").Replace(Term, "<strong>" + Term + "</strong>");
    
                if (searchResult.Fields.ContainsKey("FileTextContent"))
                    return umbraco.library.TruncateString(umbraco.library.StripHtml(searchResult.Fields["FileTextContent"]), 300, "...").Replace(Term, "<strong>" + Term + "</strong>");
    
                return string.Empty;
            }
    
    
            //simple helper to get the name of the node
            public string theNodeName(RepeaterItem item)
            {
                var theName = (SearchResult)item.DataItem;
    
                if (theName.Fields.ContainsKey("bodyText"))
                    return umbraco.library.NiceUrl(theName.Id);
    
                if (theName.Fields.ContainsKey("umbracoFile"))
                    return theName.Fields["umbracoFile"].ToString();
    
                return string.Empty;
            }
    
            //simple helper to get the extendion of the node
            public string theExtension(RepeaterItem item)
            {
                var theExtension = (SearchResult)item.DataItem;
    
                if (theExtension.Fields.ContainsKey("bodyText"))
                    return "Conent Node";
    
                if (theExtension.Fields.ContainsKey("umbracoFile"))
                    return theExtension.Fields["umbracoExtension"].ToString();
    
                return string.Empty;
            }
        }
    }

    OK, 2 big changes here. New helper method called "BuildPagination" that creates the pagination links ... and then changing "PageNumber" and "PageSize" into public properties!  Why do that? So we can leverage some Macro goodiness! :-D

    In your SearchResults/Examine macro, you'll need to add the extra parameters:

    • Term (text) - think you've already got this in place?
    • PageNumber (number)
    • PageSize (number)

    Then in your MasterPage template, add those parameters - with the advanced macro syntax, you can pull in values from the querystring ... for the search term and pageNumber (with a fall-back value of "1").

    <umbraco:Macro Alias="Search" Term="[@q]" PageNumber="[@pageNum],1" PageSize="10" runat="server"></umbraco:Macro>

     

    There are probably a few tweaks to the code needed, as I've only given this a small test.

    Good luck!

    Cheers, Lee.

  • Stefan Kip 1614 posts 4131 karma points c-trib
    Jun 08, 2011 @ 19:55
    Stefan Kip
    0

    C'mon Lee, adding controls from code-behind instead of adding them to the template?

    Shame on you! ;-)

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Jun 08, 2011 @ 19:59
    Lee Kelleher
    0

    LOL!

    The Prev/Next, sure - could show/hide with Visible property ... but the page numbers? Got a better example? ;-)

  • bob baty-barr 1180 posts 1294 karma points MVP
    Jun 08, 2011 @ 21:12
    bob baty-barr
    1

    LEE! big #H5YR on that one... worked like a dream... thank you so much

    Not being much of a developer, i don't really know how to connect everything together... this was very helpul indeed!

    you are definitely in for some beers!

  • Stefan Kip 1614 posts 4131 karma points c-trib
    Jun 09, 2011 @ 08:47
    Stefan Kip
    2

    @Lee

    Prev/next shouldn't be too hard indeed ;-)
    I would render the page numbers with a Repeater I guess...

    Something like:

    <div class="paging">
        <asp:HyperLink runat="server" ID="lnkPrevPage" >
            <%= umbraco.library.GetDictionaryItem("control_paging_vorige") %>
        </asp:HyperLink>
        <asp:Repeater runat="server" ID="rptPages" OnItemDataBound="rptPages_ItemDataBound">
            <ItemTemplate>
                <asp:HyperLink runat="server" ID="lnkPage" />
            </ItemTemplate>
        </asp:Repeater>
        <asp:HyperLink runat="server" ID="lnkNextPage">
            <%= umbraco.library.GetDictionaryItem("control_paging_volgende") %>
        </asp:HyperLink>
    </div>
    List<int> pageNumbers = new List<int>(PageCount);
    
    for(int i = 1; i <= PageCount; i++)
        pageNumbers.Add(i);
    
    rptPages.DataSource = pageNumbers;
    rptPages.DataBind();
    
  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Jun 09, 2011 @ 09:24
    Jeroen Breuer
    2

    Why not just use the ListView webcontrol in combination with the DataPager webcontrol? It works the same as a repeater, but has paging built-in.

    Here you can find more info: http://www.codeproject.com/KB/aspnet/ListViewWithDataPager.aspx

    Jeroen

  • Stefan Kip 1614 posts 4131 karma points c-trib
    Jun 09, 2011 @ 09:27
    Stefan Kip
    1

    @Jeroen

    Yeah was thinking the same. Problem is; bob's not a real developer (his own words ;-)) and was already using a Repeater.
    Removing the repeater and using the listview/datapager instead would be too complex I'm afraid ;-)

  • bob baty-barr 1180 posts 1294 karma points MVP
    Jun 09, 2011 @ 15:50
    bob baty-barr
    1

    it would be awesome to keep this thread going to get a complete 'best practices' solution that one of us could then turn into a wiki article or even a package. As i have said and have been quoted ;) i am NOT a developer so it was much easier to follow Lee's bootstrapped example that added on to what i had, but seeing a complete solution with best practices in mind would be AWESEOME... just sayin :)

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Jun 09, 2011 @ 15:53
    Lee Kelleher
    1

    Absolutely, we could cover this at one of the "Hands On" sessions next week! #CG11

  • bob baty-barr 1180 posts 1294 karma points MVP
    Jun 09, 2011 @ 15:57
    bob baty-barr
    0

    BRILLIANT!

  • Stefan Kip 1614 posts 4131 karma points c-trib
    Jun 09, 2011 @ 15:57
    Stefan Kip
    0

    Lol, sure Lee ;-)

    We can at least discuss this subject :)

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Jun 09, 2011 @ 16:02
    Lee Kelleher
    0

    Yeah, I'll show you all how to do it in XSLT with uComponents. ;-)

  • Stefan Kip 1614 posts 4131 karma points c-trib
    Jun 09, 2011 @ 16:05
    Stefan Kip
    0

    Would be a waste of your and my time though... :P

  • bob baty-barr 1180 posts 1294 karma points MVP
    Jun 09, 2011 @ 16:07
    bob baty-barr
    0

    um, Lee, if that isn't a joke... i would be VERY interested in seeing how that could work!

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Jun 09, 2011 @ 16:08
    Lee Kelleher
    0

    Haha ... aw come on, XSLT, all that lovely markup, namespaces and tagsoup? What's not to love?

  • Stefan Kip 1614 posts 4131 karma points c-trib
    Jun 09, 2011 @ 16:08
    Stefan Kip
    0

    @bob

    Shame on you!

    @Lee

    Shame on you too! :P

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Jun 09, 2011 @ 16:08
  • bob baty-barr 1180 posts 1294 karma points MVP
    Jun 09, 2011 @ 16:11
    bob baty-barr
    0

    well, crap! i could kick myself!!! so basically you set up the indexes, some simple xslt and BAM!

    well, that was days of my life i can never get back :(

Please Sign in or register to post replies

Write your reply to:

Draft