views:

188

answers:

3

I know the dotnetkicks.com system is open source so I can view the code myself but I can't make sense of how they did their paging. It's hard to explain but if you goto dotnetkicks.com you can play with the paging on their front page.

What I am specifically interested in is how they show the first few pages, then "..." and then the last few pages.

It starts off like this

Prev 1 2 3 4 5 6 ... 355 356 Next

Then if you hit page 10 it changes to this

Prev 1 2 ... 6 7 8 9 10 11 12 13 14 ... 355 356 Next

It's by far my favorite paging system so I'd like to do the same thing on my websites

+1  A: 

The Pagination control in the project is fairly straight forward, if you've ever written a paging control. I think what may be confusing you is the use of urlrewriting.net for the /page/n url format.

Adam Lassek
Sorry aLassek, i just noticed you had a link to the actual pagination control. I guess I can't get a clearer answer than that
A: 

Sorry ALassek, I've added more details to my question to make it clearer.

A: 

I've written such a control and it took some code... there are plenty decisions to take.

public class SimplePagerLinkGenerator : IPagerLinkGenerator
{
    private PagingDisplaySettings _displaySettings;

    public SimplePagerLinkGenerator(PagingDisplaySettings displaySettings)
    {
        this._displaySettings = displaySettings;
    }

    public PageLinkData[] GetPagesLinkData(uint crtPageIndex, uint pageCount, ushort pageSize,
        string urlLinkTemplate)
    {
        int i, crtPage = (int)crtPageIndex, pageCnt = (int)pageCount,
            pgInnerCnt = (int)this._displaySettings.PagesBeforeOrAfterCurrent,
            pgEndCnt = (int)this._displaySettings.PagesShownAtEndingsCount;

        int innerBlockStart = crtPage - pgInnerCnt;
        int innerBlockEnd = crtPage + pgInnerCnt;
        int innerBlockStartNormalized = innerBlockStart < 1 ? 1 : innerBlockStart;
        int innerBlockEndNormalized = innerBlockEnd > pageCount ? pageCnt : innerBlockEnd;

        List<PageLinkData> result = new List<PageLinkData>(2 * pgEndCnt + 3 + 2 * pgInnerCnt);
        for (i = 1; i <= pgEndCnt && i <= pageCount; i++)
            result.Add(new PageLinkData(i, i == crtPage, urlLinkTemplate, pageSize));
        if (i > pageCount)
            return result.ToArray();
        if (innerBlockStart > pgEndCnt + 1)
        {
            result.Add(PageLinkData.Ellipsis);
            for (i = innerBlockStart; i <= innerBlockEndNormalized; i++)
                result.Add(new PageLinkData(i, i == crtPage, urlLinkTemplate, pageSize));
        }
        else
            for (i = pgEndCnt + 1; i <= innerBlockEndNormalized; i++)
                result.Add(new PageLinkData(i, i == crtPage, urlLinkTemplate, pageSize));

        if (innerBlockEnd < pageCount - pgInnerCnt)
        {
            result.Add(PageLinkData.Ellipsis);
            for (i = pageCnt - pgInnerCnt + 1; i <= pageCount; i++)
                result.Add(new PageLinkData(i, i == crtPage, urlLinkTemplate, pageSize));
        }
        else
            for (; i <= pageCount; i++)
                result.Add(new PageLinkData(i, i == crtPage, urlLinkTemplate, pageSize));

        return result.ToArray();
    }

    public PagingDisplaySettings DisplaySettings { get { return this._displaySettings; } }
}
Andrei Rinea