子在川上曰

            逝者如斯夫不舍晝夜
          隨筆 - 71, 文章 - 0, 評論 - 915, 引用 - 0
          數據加載中……

          rails中使用ajax時的分頁實現

          關于分頁可以參考這篇文章:http://ruby-lang.org.cn/forums/viewthread.php?tid=206&extra=page%3D1&page=2

          但如果使用ajax則分頁的實現需要改動一下,如下所示。

                共<b><%=@article_pages.page_count%></b>頁:&nbsp;

                
          <%=if @article_pages.current!=@article_pages.first_page
                   link_to_remote(
          "首頁",:update => "articleList",
                      :url 
          => {:action => 'list',:subcategory_id => @subcategory.id,:page => @article_pages.first_page})
                
          else
                
          "首頁"
                end
          %>

                
          <%=if @article_pages.current.previous
                link_to_remote(
          "上一頁",:update => "articleList",
                    :url 
          => {:action => 'list',:subcategory_id => @subcategory.id,:page => @article_pages.current.previous})
                
          else
                
          "上一頁"
                end 
          -%>



                
          <%=pagination_links_each(@article_pages, :window_size => 5do | page |
                link_to_remote(
          "[#{page}]", :update => "articleList",
                                     :url 
          => {:action => 'list',:subcategory_id => @subcategory.id,:page => page}) 
                end
          %>

                
          <%=if @article_pages.current.next
                link_to_remote(
          "下一頁",:update => "articleList",
                          :url 
          => {:action => 'list',:subcategory_id => @subcategory.id,:page => @article_pages.current.next})
                
          else
                
          "下一頁"
                end 
          -%>

                
          <%=if @article_pages.current!=@article_pages.last_page
                link_to_remote(
          "末頁",:update => "articleList",
                :url 
          => {:action => 'list',:subcategory_id => @subcategory.id,:page => @article_pages.last_page})
                
          else
                
          "末頁"
                end
          %>


          然后再優化一下,把這些代碼提取成一個公共函數,放在application_helper里,以便其他頁面也能共享。代碼如下:

            def ajax_pagination_links(pages,update,url)
              links 
          = []
              links 
          << "共<b>#{pages.page_count}</b>頁"
              
          return links[0if pages.page_count==1
              links 
          << ":&nbsp;"

              
          if pages.current!=pages.first_page
                url[:page] 
          = pages.first_page
                links 
          << link_to_remote("首頁", :update => update, :url => url)
              
          else
                links 
          << "首頁"
              end
           
              
          if pages.current.previous
                url[:page] 
          = pages.current.previous
                links 
          << link_to_remote("上一頁", :update => update, :url => url)
              
          else
                links 
          << "上一頁"
              end

              links 
          << pagination_links_each(pages, :window_size => 5do |page|
                          url[:page] 
          = page
                          link_to_remote(
          "[#{page}]", :update => update, :url => url) 
                      end

              
          if pages.current.next
                url[:page] 
          = pages.current.next
                links 
          << link_to_remote("下一頁",:update => update, :url => url)
              
          else
                links 
          << "下一頁"
              end

              
          if pages.current!=pages.last_page
                url[:page] 
          = pages.last_page
                links 
          << link_to_remote("末頁",:update => update, :url => url)
              
          else
                links 
          << "末頁"
              end
              
              links.join(
          " ")
            end

          以后在頁面里只需要加上這樣一句即可
               <%=ajax_pagination_links(@article_pages, "articleList", {:action => 'list',:subcategory_id => @subcategory.id})%>

          posted on 2007-07-30 17:53 陳剛 閱讀(1623) 評論(4)  編輯  收藏 所屬分類: Rails&Ruby

          評論

          # re: rails中使用ajax時的分頁實現  回復  更多評論   

          cool.

          非常好的一個實現, 謝謝你偉大的工作.
          2007-12-25 10:08 | sixfooter

          # re: rails中使用ajax時的分頁實現[未登錄]  回復  更多評論   

          干的漂亮,謝謝分享。
          2007-12-27 22:37 | daiiing

          # re: rails中使用ajax時的分頁實現  回復  更多評論   

          為什么不能用啊,我用
          <%=ajax_pagination_links(@paper_pages, "contrast_paper_list", {:action => 'paper_contrast'})%>,生成的頁面如下(末頁):

          <a onclick="new Ajax.Updater('contrast_paper_list', '/paper/paper_contrast?page=2', {asynchronous:true, evalScripts:true}); return false;" href="#">末頁</a>
          但是我看是連接到http://localhost:3001/paper/paper_contrast#這頁面的,我是把分頁的那一塊做成一個partail,我不知道action是不是就跟普通的pagination的寫法一樣啊。
          2008-02-29 11:12 | magiclin

          # re: rails中使用ajax時的分頁實現  回復  更多評論   

          :subcategory_id => @subcategory.id有什么用啊,想不通
          2008-02-29 11:24 | magiclin
          主站蜘蛛池模板: 洛扎县| 长治县| 临朐县| 怀仁县| 即墨市| 朝阳县| 达日县| 青阳县| 高唐县| 高雄市| 监利县| 平乐县| 宜良县| 林周县| 龙陵县| 东安县| 平潭县| 襄樊市| 齐河县| 布拖县| 保康县| 南乐县| 曲周县| 顺义区| 华宁县| 沾益县| 万宁市| 敦化市| 诸暨市| 万盛区| 垣曲县| 永昌县| 大埔县| 衡南县| 静海县| 太谷县| 永城市| 中山市| 西乡县| 门源| 玛纳斯县|