user1259167
user1259167

Reputation: 447

PagedListPager customise paging links

I'm using the following package to do paging in ASP.NET MVC

https://github.com/TroyGoode/PagedList

This is wokring great, however I'd like to be able to add a extra HTML5 data attribute to the pager links. Simply which contains the page number of the link.

For example:

<a href="admin/demo/userlist" data-page-no="1">1</a> <a href="admin/demo/userlist" data-page-no="2">2</a> etc etc

But I'm struggling to do this and can only get the page number into the URL.

Can anyone help? I would have thought this would have been a common and useful feature.

Thanks

Upvotes: 3

Views: 7238

Answers (3)

Nick Kovalsky
Nick Kovalsky

Reputation: 6462

Using "latest" 8.0.7 nuget X.PagedListMvc.Core the FunctionToTransformEachPageLink delegate is adding and additional element when passing return value. So if you return liTag the page number link will be doubled. The working code is below, just returning an empty tag builder:

FunctionToTransformEachPageLink = (TagBuilder liTag, TagBuilder aTag) =>
        {
            var ret = new TagBuilder("text");

            aTag.Attributes.Add("data-otf-target", "#page-wrapper");

            liTag.InnerHtml.AppendHtml(aTag);

            return ret;
        },

If anyone has a better solution please edit this post.

Upvotes: 0

CrazyTmack
CrazyTmack

Reputation: 66

Short deadline hack. I tried something similar, but if the inner HTML for the hyperlink is an ellipse or some other special character, it didn't quite work.

               FunctionToTransformEachPageLink = (li, a) =>
               {
                   if (a.Attributes.ContainsKey("href"))
                   {
                       Uri uri;
                       if (Uri.TryCreate("http://temp.org/" + a.Attributes["href"], UriKind.Absolute, out uri))
                       {
                           var qs = uri.Query;
                           if (!string.IsNullOrWhiteSpace(qs))
                           {
                               var nvc = HttpUtility.ParseQueryString(qs);
                               var page = nvc["page"];
                               a.MergeAttribute("data-page", page);
                           }
                       }
                   }
                   else
                   {
                       int page;
                       if (int.TryParse(a.InnerHtml, out page))
                           a.MergeAttribute("data-page", page.ToString());
                   }

                   li.InnerHtml = a.ToString();
                   return li;
               }

"FunctionToTransformEachPageLink" should include the current page as a delegate parameter.

Upvotes: 2

user1259167
user1259167

Reputation: 447

Sorted with some help from Troy, working perfect now :)

@Html.PagedListPager(Model, page => "admin/demo/userlist", new PagedListRenderOptions { FunctionToTransformEachPageLink = (liTag, aTag) => { aTag.Attributes.Add("data-page-no", aTag.InnerHtml); liTag.InnerHtml = aTag.ToString(); return liTag; } })

Upvotes: 11

Related Questions