jquery - Call JavaScript function from PagedListPager - Stack Overflow

Is it possible to call a JavaScript function from within @Html.PagedListPager(in here) ?I have a button

Is it possible to call a JavaScript function from within @Html.PagedListPager(in here) ?

I have a button which calls the following function and performs all its supposed to with ease:

function fetchResults() {
    $.get('@Url.Action("Search", "Notifications")',
        {
            device: "Device"
        },
        function (data) {
            $('#results').html(data);
        })
};

Now how can I do the same when I click on a page number on my PagedListPager?

Currently my pager reloads the page and that's the main thing I want to avoid.

This is my Pager:

@Html.PagedListPager((IPagedList)ViewBag.NotificationsPage, page => 
    Url.Action("Search", "Notifications", new
    {
        device = "Device",
        page = page
    }),
    PagedListRenderOptions.PageNumbersOnly)

Perhaps there's a much better way to do this. Help will be appreciated.

Is it possible to call a JavaScript function from within @Html.PagedListPager(in here) ?

I have a button which calls the following function and performs all its supposed to with ease:

function fetchResults() {
    $.get('@Url.Action("Search", "Notifications")',
        {
            device: "Device"
        },
        function (data) {
            $('#results').html(data);
        })
};

Now how can I do the same when I click on a page number on my PagedListPager?

Currently my pager reloads the page and that's the main thing I want to avoid.

This is my Pager:

@Html.PagedListPager((IPagedList)ViewBag.NotificationsPage, page => 
    Url.Action("Search", "Notifications", new
    {
        device = "Device",
        page = page
    }),
    PagedListRenderOptions.PageNumbersOnly)

Perhaps there's a much better way to do this. Help will be appreciated.

Share Improve this question asked Feb 6, 2014 at 15:06 DumisaniDumisani 3,0481 gold badge31 silver badges40 bronze badges 2
  • 1 Why not use JQuery and unobtrusively hijack the click event? I do this in some of my apps and it degrades gracefully if for some reason the event gets unhooked. – Khalid Abuhakmeh Commented Feb 6, 2014 at 15:09
  • possible duplicate of Ajax Pagination in PagedList.MVC using partial Page – avs099 Commented Dec 19, 2014 at 23:31
Add a ment  | 

1 Answer 1

Reset to default 5

All that this @Html.PagedListPager helper does is spit some HTML containing links to perform the pagination. So you could subscribe to the click event of those links and AJAXify them:

$(document).on('click', 'a', function() {
    $.ajax({
        url: this.href,
        type: 'GET',
        cache: false,
        success: function(result) {
            $('#results').html(result);
        }
    });
    return false;
});

Important things to note:

  • I have subscribed to the click event in a lively manner. This means that if we replace the links in the success callback of the AJAX request, this click event handler will continue to work
  • You might want to adjust the $(document).on('click', 'a', function() { selector which is pretty inclusive and target only the links generated by this pager. For example look if they are not inside some containing div or something in which case you could use something along the lines of $('.pager').on('click', 'a', function() {.
  • Inside the success callback you might need to adapt the $('#results') selector to target the div containing the actual results which will get refreshed with the partial HTML returned by your controller action.
  • Talking about partial HTML and controller action you will obviously need to adapt the controller action that gets invoked in the AJAX request to return a PartialView instead of a full View containing only the updated records and new pagination links.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744375052a4571144.html

相关推荐

  • jquery - Call JavaScript function from PagedListPager - Stack Overflow

    Is it possible to call a JavaScript function from within @Html.PagedListPager(in here) ?I have a button

    7天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信