Is it possible to add anchor tags to kaminari URLs?

左心房为你撑大大i 提交于 2019-12-25 02:16:41

问题


Im using kaminari plugin for my pagination on Rails 3.1.3

I want my URL to have anchor tags at the ends as i will need that to select my jQuery tab

For Ex:

Current URL: http://mysite.com/users/index/2

What i want is: http://mysite.com/users/index/2#users

How can i append this anchor tag into my URL? Can someone help me?


回答1:


Just modify the templates at app/views/kaminari. For example, here is _page.html.haml:

= link_to_unless page.current?, page, url, {:remote => remote, :rel => page.next? ? 'next' : page.prev? ? 'prev' : nil}

You can add anchor tags like this (pay attention to the url argument):

  = link_to_unless page.current?, page, "#{url}#anchor_1", {:remote => remote, :rel => page.next? ? 'next' : page.prev? ? 'prev' : nil}



回答2:


I got the same issue (with jQuery tabs) and after looking into Kaminari's doc, I couldn't find a solution. However, here's how I solved it.

In the view, I wrapped the paginate method with a div (here slim syntax) having a class "add-anchor" and a data attribute "data-name" with the anchor name as the value (here #anchor in the example):

.add-anchor data-name="anchor"
  = paginate @items

The idea is to programmatically add the anchor to every link in the div onDocumentReady with jQuery:

$(document).ready(function() { 

  var addAnchor;
  addAnchor = $('.add-anchor');

  // For each paginate method
  addAnchor.each(function() {

    var $name;
    $name = $(this).data().name;

    // For each paginate link
    return $(this).find('a').each(function() {

      var href;
      href = $(this).attr('href');

      // Add the anchor at the end
      return $(this).attr('href', href + "#" + $name);
    });

  });

});

Hope it helps.



来源:https://stackoverflow.com/questions/8807653/is-it-possible-to-add-anchor-tags-to-kaminari-urls

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!