Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Custom Attributes #7

Closed
PaddyMacBelfast opened this issue May 7, 2016 · 3 comments
Closed

Add Custom Attributes #7

PaddyMacBelfast opened this issue May 7, 2016 · 3 comments

Comments

@PaddyMacBelfast
Copy link

PaddyMacBelfast commented May 7, 2016

Hi Joe,

Excellent job with this TagHelper for Pagination. I found it very useful for paging, especially the Ajax control.

Can this be extended to add some custom attributes? e.g. data-ajax-success and data-ajax-failure
Basically, I was looking to smooth scroll to the top of the page/div when using Ajax but I need a 'data-ajax-success' attribute..

I've took a local copy down to try out..

Added to your PagerTagHelper.cs:

private const string AjaxSuccessAttributeName = "cs-ajax-success";
 private const string AjaxFailureAttributeName = "cs-ajax-failure";

 [HtmlAttributeName(AjaxSuccessAttributeName)]
 public string AjaxSuccess { get; set; } = string.Empty;

 [HtmlAttributeName(AjaxFailureAttributeName)]
 public string AjaxFailure { get; set; } = string.Empty;

then just added a simple if statement to add the attributes :

 if (AjaxTarget.Length > 0)
                {
                    a.MergeAttribute("data-ajax", "true");
                    a.MergeAttribute("data-ajax-mode", AjaxMode);
                    a.MergeAttribute("data-ajax-update", AjaxTarget);
                    if (AjaxSuccess.Length > 0)
                    {
                        a.MergeAttribute("data-ajax-success", AjaxSuccess);
                    }
                    if (AjaxFailure.Length > 0)
                    {
                        a.MergeAttribute("data-ajax-failure", AjaxFailure);
                    }
                }

Its handy to use as below:

 <cs-pager cs-paging-pagesize="@Model.PageSize"
              cs-paging-pagenumber="@Model.PageNumber"
              cs-paging-totalitems="@Model.TotalItemCount"
              cs-next-page-text="Next Page >"
              cs-previous-page-text="< Previous Page"
              cs-paging-maxpageritems="5"
              cs-pagenumber-param="page"
              cs-ajax-target="#gridcontainer"
              cs-ajax-success="scrollToListingLocation('#gridcontainer')"
              asp-controller="Home"
              asp-action="AjaxPage"
              asp-route-pagesize="@Model.PageSize"  ></cs-pager>

with the simple JS function to auto-scroll to the top of the div:

 function scrollToListingLocation(n) {
        $('html, body').animate({
            scrollTop: $(n).offset().top
        }, 1200);
    }

Is this the best way to do this?

@joeaudette
Copy link
Collaborator

That looks reasonable to me, if you're saying that worked for you I'm happy to make the code changes you suggest to support that in the taghelper

@joeaudette
Copy link
Collaborator

I have implemented the changes and published a new nuget

Thanks!

@PaddyMacBelfast
Copy link
Author

Thanks Joe... Keep up the good work!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants