<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆-167  評論-65  文章-0  trackbacks-0
    環(huán)境: ruby 1.8.7 + rails 2.1.0 + > = jquery 1.3.2

    效果預覽:


    一般 我們 在rails 中 分頁 will_paginate 是必不可少的插件,但是 一般都是基于 prototype 的,例如 ajax 式的分頁, 通過 RemoteLinkRenderer改變 css 樣式等等,都是 基于prototype 的,但是 prototype 和 Jquery 在項目中 經常遇到conflict ,這個 很 讓人 頭疼,rails 封裝好的ajax方法,好多 都因為 jquery 不能用,jquery 那么 好 前端 用戶體驗,又因為 prototype 出現(xiàn) conflict , 哎 為什么 相互殘殺 呢。。。。。

    上面實現(xiàn)的核心思想 是 前端 通過 jquery的 異步調用數(shù)據, 后臺 rails 通過 respond_to fotmat.js 的方式 給予 返回數(shù)據。。。

    Demo:

    layout 中導入:
        <%= stylesheet_link_tag 'pagination' -%>
        <%= javascript_include_tag 'jquery' %>

    pagination 是改變分頁的樣式


    Action:

      def index
        @products = Product.paginate(:per_page => 10, :page => params[:page])
        respond_to do |format|
          format.html                        #default : index.html.erb
          format.js {:layout => false} # default : index.js.erb
        end
      end


    View:

    index.html.erb

    <div id="product">
      <%= render :partial => 'products' %>
    </div>

    index.js.erb

    $("#product").html("<%= escape_javascript(render :partial => "products") %>");

    escape_javascript 是轉義的意思 和 <%=h %> 這里的 h 差不多

    partial :
    _products.html.erb

    <script>

    $(function() {
      $(".apple_pagination a").live("click", function() {
        $(".apple_pagination").html("Page is loading...");
        $.get(this.href, null, null, "script");
    /* alert(this.href); */
        return false;
      });
    });

    </script>

    <%= will_paginate @products , :class => 'apple_pagination' ,:previous_label => '<<上一頁', :next_label => '下一頁>>'   :renderer  => 'WillPaginate::LinkRenderer' %>

    <% for product in @products  %>
      <div class="product">
        <h3>
          <%= link_to h(product.name), product %>
          <%= number_to_currency(product.price) %>
        </h3>
      </div>
    <% end %>


    上面的 $(".apple_pagination a")  即是 will_paginate 的 :class ,:renderer 使用的是will_paginate 的default 的,如果 enviroment.rb 中配置了will_paginate的樣式,這里不寫 :renderer 會出錯!,具體默認的參數(shù) 可以查看 will_paginate 下的 view_helpers.rb

    全部 源碼 下載:
    http://www.uushare.com/user/fl1429/file/1941241

    配置方法:
    1,進入工程, rake setup
    2,ruby script/server
    3,okay 成功 了。。。。

    補充 : 如果 一個 頁面 有兩處需要 分頁,那么 will_paginate 默認 情況下 是同時翻頁的。。那么 如何 避免呢。。只需要 給各自的 will_paginate 指定 不同的 class 即可  例如

    will_paginate :

    <%= will_paginate collection ,
      :class => "apple_paginate my_paginate",
      :previous_label => '<<上一頁',
      :next_label => '下一頁>>' ,
      :renderer  => 'WillPaginate::LinkRenderer'  %>

    apple_paginate 是 分頁的真正的 css,my_paginate 是為了 區(qū)別 不同區(qū)的 分頁 而加的


    jquery script :
      jQuery(function() {
        jQuery(".my_paginate a").live("click", function() { 
          jQuery(".my_paginate").html("正在加載...");
          jQuery.get(this.href, {flag : "my" }, null, 'script');
          return false;
        });
      });

    注意 jquery get 方法的 四個參數(shù) 的 意義

    ref:
    http://railscasts.com/episodes/174-pagination-with-ajax
    http://soylentfoo.jnewland.com/articles/2007/09/17/resource_this-dry-rails-resource-controllers
    http://book.csdn.net/bookfiles/375/10037514155.shtml
    http://stackoverflow.com/questions/1268383/format-js-with-rails





    write by feng
    posted on 2009-08-25 15:28 fl1429 閱讀(1879) 評論(0)  編輯  收藏 所屬分類: RailsJquery
    已訪問數(shù):
    free counters
    主站蜘蛛池模板: 好爽又高潮了毛片免费下载| 亚洲日韩中文字幕一区| 久久国产精品免费看| 久久91亚洲精品中文字幕| 免费A级毛片无码专区| 亚洲手机中文字幕| 日产乱码一卡二卡三免费| 特a级免费高清黄色片 | 亚洲AV无码成人精品区蜜桃| 91精品国产免费网站| 亚洲成a人无码亚洲成av无码 | 亚洲激情视频网站| 福利片免费一区二区三区| 真实乱视频国产免费观看| 亚洲一欧洲中文字幕在线| 国产成人综合久久精品亚洲| 日本高清色本免费现在观看| 成人精品国产亚洲欧洲| 亚洲国产精品无码久久青草| 国产黄在线观看免费观看不卡| 亚洲无线码在线一区观看| 偷自拍亚洲视频在线观看99| 亚洲日韩中文在线精品第一| 亚洲成av人无码亚洲成av人| 亚洲成a人片在线观看久| 男女一边摸一边做爽的免费视频 | 在线观看视频免费国语| 羞羞漫画在线成人漫画阅读免费| 中文亚洲成a人片在线观看| 日本一区二区在线免费观看| 久久久久久久亚洲精品| 在线涩涩免费观看国产精品| 四虎永久成人免费| 可以免费观看的国产视频| youjizz亚洲| 国产无遮挡裸体免费视频| 国产情侣久久久久aⅴ免费| 亚洲综合激情五月丁香六月| 亚洲精品无码久久毛片| 7723日本高清完整版免费| 久久亚洲精品11p|