Copied to clipboard

Flag this post as spam?

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


  • nelson 6 posts 26 karma points
    Jul 25, 2012 @ 18:05
    nelson
    0

    hi, i have a question about a news list

    Please, i want to know how i can add a pagination to this code:

     @{    
            @* -- Setup a counter variable for position -- *@
            var position = 1;
        }

        @* -- List children pages from the current page (aka Model) -- *@
        @foreach (var page in @Model.Children.Where("umbracoNaviHide != true"))
        {
         
          <div class="noticia">
            <div class="n-left">       
               <img src="@page.foto" width="137" height="103" alt="xp" />
            </div>
            <div class="n-right">
                    <h2>@page.Name</h2>
                    <p><strong>
                       
                        @page.fecha.ToString("dd-MM-yyyy")
                   
                    </strong></p>
                      @{
                    var texto         = @page.texto.ToString();
                    var textoNoHtml   = umbraco.library.StripHtml(texto);
                    var textoTrunc    = umbraco.library.TruncateString(textoNoHtml, 200, "...");
                }

                <p>
                    @textoTrunc
                </p>
                      <div class="read-more2"><a href="@page.Url">leer mรกs...</a></div>          
                  </div>                
                   <img src="images/not-divider.png" alt="divider" class="divider2"/>                         
            </div>
               
                 
            @* -- After every 3rd item insert a <br/> -- *@
            if (position % 3 == 0)
            {

            }
            
            @* -- Increase the position counter by 1 -- *@
            position++;          
         
            
        }

    This code works well but i'd like to show 5 items per page with a next and previous button and a list of links to each page.

    How could i do it?

    Thanks for the answer.

  • Jazz 21 posts 41 karma points
    Jul 26, 2012 @ 12:13
    Jazz
    0

    Hi Nelson,

    Try this below code, I added your code with paging code. I hope it work for you.

    ----------------------------------

    @using umbraco.MacroEngines
    @inherits umbraco.MacroEngines.DynamicNodeContext

    @{

        var pagesToList = @Model.Children..Where("umbracoNaviHide != true");


        // configuration
        var itemsPerPage = String.IsNullOrEmpty(Parameter.ItemsPerPage) ? 5 : int.Parse(Parameter.ItemsPerPage);
        var previousLabel = String.IsNullOrEmpty(Parameter.PreviousLabel) ? "Previous" : Parameter.PreviousLabel;
        var nextLabel = String.IsNullOrEmpty(Parameter.NextLabel) ? "Next" : Parameter.NextLabel;

        // paging calculations
        var numberOfItems = pagesToList.Count();
        int currentPage = 1;
        if (!int.TryParse(HttpContext.Current.Request.QueryString["Page"], out currentPage))
        {
            currentPage = 1;
        }
        currentPage--;
        var numberOfPages = numberOfItems % itemsPerPage == 0 ? Math.Ceiling((decimal)(numberOfItems / itemsPerPage)) : Math.Ceiling((decimal)(numberOfItems / itemsPerPage)) + 1;

        
        foreach (var item in pagesToList.Skip(currentPage * itemsPerPage).Take(itemsPerPage))
        {
              <div class="noticia">
            <div class="n-left">      
               <img src="@page.foto" width="137" height="103" alt="xp" />
            </div>
            <div class="n-right">
                    <h2>@page.Name</h2>
                    <p><strong>
                      
                        @page.fecha.ToString("dd-MM-yyyy")
                  
                    </strong></p>
                      @{
                    var texto         = @page.texto.ToString();
                    var textoNoHtml   = umbraco.library.StripHtml(texto);
                    var textoTrunc    = umbraco.library.TruncateString(textoNoHtml, 200, "...");

        }
        <div class="usedcarpager">
         <p class="pagingPages">
        @{
            // Google style paging links
            if (currentPage > 0)
            {
          <a href="?page=@(currentPage)">&laquo; @previousLabel</a>
            }
            else
            {
          <span class="pagingDisabled">&laquo; @previousLabel</span>
            }

            var Pages = Enumerable.Range(1, (int)numberOfPages);
            foreach (var number in Pages)
            {
                if (number - 1 != currentPage)
                {
          <a href="?page=@number">@number</a>
                }
                else
                {
          @number
                }
          @Html.Raw("&nbsp&nbsp");
            }

            if (currentPage < Pages.Count() - 1)
            {
          <a href="?page=@(currentPage + 2)">@nextLabel &raquo;</a>
            }
            else
            {
          <span class="pagingDisabled">@nextLabel &raquo;</span>
            }
      }
      </p>
      </div>
       
    }

  • nelson 6 posts 26 karma points
    Jul 26, 2012 @ 17:16
    nelson
    0

    Thanks, i'll try to use it
    later, then i'll tell you if it works.

  • nelson 6 posts 26 karma points
    Jul 27, 2012 @ 19:11
    nelson
    0

    Jazz, thank you so much, i solved my problem with that code.

Please Sign in or register to post replies

Write your reply to:

Draft