0

我在 Rails 3.1.3 上使用 kaminari 插件进行分页

我希望我的 URL 在末尾有锚标记,因为我需要它来选择我的 jQuery 选项卡

例如:

当前网址: http: //mysite.com/users/index/2

我想要的是: http: //mysite.com/users/index/2#users

如何将此锚标记附加到我的 URL 中?有人能帮我吗?

4

2 回答 2

1

只需修改app/views/kaminari. 例如,这里是_page.html.haml

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

您可以像这样添加锚标签(注意url参数):

  = link_to_unless page.current?, page, "#{url}#anchor_1", {:remote => remote, :rel => page.next? ? 'next' : page.prev? ? 'prev' : nil}
于 2012-01-10T17:47:48.803 回答
0

我遇到了同样的问题(使用 jQuery 选项卡),在查看了 Kaminari 的文档后,我找不到解决方案。但是,这就是我解决它的方法。

在视图中,我使用 div (此处为 slim 语法)包装了paginate方法,该 div 具有一个类“add-anchor”和一个数据属性“data-name”,其中锚名称作为值(示例中为 #anchor):

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

这个想法是使用 jQuery 以编程方式将锚点添加到 div onDocumentReady 中的每个链接:

$(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);
    });

  });

});

希望能帮助到你。

于 2012-04-05T02:28:20.317 回答