About paging function.

Topics: General, XSLT
Jul 18, 2011 at 3:31 AM

Hi ALl.

I have a lot of records that return from a C# function.

For example

This is my XSLT Function that call a C# function

    <xsl:variable name="ResulltOfSearch">
           <f:function name="Hr2b.SearchClient.SearchClient.GetResultSearchClient">
                   <f:param name="keyword" value="{$getKeyword}" />
                   <f:param name="industry" value="{$getIndustry}" />
                   <f:param name="service" value="{$getRelatedService}" />
           </f:function>
    </xsl:variable>
    <xsl:variable name="ListOfSearch" select="c1:CallFunction($ResulltOfSearch)" />

This is C# fucntion

public static IEnumerable<XElement> GetResultSearchClient(string keyword,string industry,string service)
{
       using (DataConnection connection = new DataConnection()) 
       {
......
foreach (var item in result)
            {
                 yield return new XElement("Client",
                                 new XAttribute("company", item.Company),
                                 new XAttribute("relatedservices", item.RelatedService),
                                 new XAttribute("companyURL", item.CompanyURL),
                                 new XAttribute("industry.industry", item.Industry),
                                 new XAttribute("image.Id", item.Image));
            }



  } }

Please show me how to paging and i want the paging function display as bellow:

First Prev 1 2 3 4 5 Next Last



Thank you.






Coordinator
Jul 18, 2011 at 8:27 AM

Hello,

Just for your information - the Get(Data)Xml function has a number of parameters which allow to implement paging easily - http://docs.composite.net/C1/XSLT/SortingAndPaging.aspx

In your case you should implement this in your GetResultSearchClient function something like this:

1. To switch pages consider to use additional query parameter e.g. named  Page

2. Consider to add additional two parameters to GetResultSearchClient function: itemsPerPage and currentPageNumber (get query Page parameter)

3. Add additional XElement("PagingInfo") to the return result of the GetResultSearchClient function.

 public static IEnumerable<XElement> GetResultSearchClient(string keyword, string industry, string service, int itemsPerPage, int currentPageNumber)
        {
            using (DataConnection connection = new DataConnection())
            {

                 ...

                /*calculate total page count*/
                int totalPageCount = (int)Math.Ceiling((double)result.Count() / itemsPerPage);
                if (currentPageNumber == 0) currentPageNumber = 1;

                // skip result
                result = result.Skip(itemsPerPage * (currentPageNumber - 1)).Take(itemsPerPage).ToList();

                foreach (var item in result)
                {
                    yield return new XElement("Client",
                                     new XAttribute("company", item.Company),
                                     new XAttribute("relatedservices", item.RelatedService),
                                     new XAttribute("companyURL", item.CompanyURL),
                                     new XAttribute("industry.industry", item.Industry),
                                     new XAttribute("image.Id", item.Image));
                }


                if (totalPageCount > 1)
                {
                    yield return new XElement("PagingInfo",
                                     new XAttribute("CurrentPageNumber", currentPageNumber),
                                     new XAttribute("TotalPageCount", totalPageCount));
                }

            }
        }

 

 

Then XSLT will be something like this:

 <xsl:variable name="ResulltOfSearch">
           <f:function name="Hr2b.SearchClient.SearchClient.GetResultSearchClient">
                   <f:param name="keyword" value="{$getKeyword}" />
                   <f:param name="industry" value="{$getIndustry}" />
                   <f:param name="service" value="{$getRelatedService}" />
                   <f:param name="itemsPerPage" value="20" />
                   <f:param name="currentPageNumber" value="c1:GetQueryStringValue('Page')" />
           </f:function>
    </xsl:variable>
    <xsl:variable name="ListOfSearch" select="c1:CallFunction($ResulltOfSearch)" />
    <xsl:variable name="Clients" select="$ListOfSearch/Client" />      
    <xsl:variable name="PagingInfo" select="$ListOfSearch/PagingInfo" />      
   <!--proceed clients  / paging info -->
  <!-- ..............  -->
  <xsl:template match="PagingInfo" mode="Navigation"> 
  <div class="PagingInfo"> 
    <xsl:choose> 
      <xsl:when test="@CurrentPageNumber &gt; 1"> 
        <xsl:text>&lt; </xsl:text> 
        <a href="?Page={@CurrentPageNumber - 1}">Prev</a> 
      </xsl:when> 
      <xsl:otherwise> 
        <xsl:text>&lt; Prev</xsl:text> 
      </xsl:otherwise> 
    </xsl:choose> 
    <xsl:choose> 
      <xsl:when test="@CurrentPageNumber &lt; @TotalPageCount"> 
        <a href="?Page={@CurrentPageNumber + 1}">Next</a> &gt;</xsl:when> 
      <xsl:otherwise>Next &gt;</xsl:otherwise> 
    </xsl:choose> 
  </div> 
</xsl:template> 

Different template examples for PagingInfo you can find here - http://docs.composite.net/C1/XSLT/SortingAndPaging.aspx

Jul 18, 2011 at 9:19 AM

Hi Anna .

Thank you very much