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

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

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

    子在川上曰

      逝者如斯夫不舍晝夜
    隨筆 - 71, 文章 - 0, 評(píng)論 - 915, 引用 - 0
    數(shù)據(jù)加載中……

    讓will_paginate的分頁(yè)支持ajax

    關(guān)于will_paginate插件,請(qǐng)看這個(gè)帖子: http://mmm.javaeye.com/blog/116931。
    文/陳剛 (www.chengang.com.cn)
    但一直搜不到它支持ajax分面的方法 ,于是我參考它分頁(yè)方法的源代碼(位于:vendor/plugins/will_paginate/lib/will_paginate/view_helpers.rb),稍微改寫,變成了一個(gè)支持ajax的分頁(yè)方法。以下代碼復(fù)制到application_helper里即可。


    ??
    #-----------------------------------------
    ??#?will_paginate插件的ajax分頁(yè)
    ??#-----------------------------------------
    ??@@pagination_options?=?{?:class?=>?'pagination',
    ????????
    :prev_label???=>?'上一頁(yè)',
    ????????
    :next_label???=>?'下一頁(yè)',
    ????????
    :inner_window?=>?4,?#?links?around?the?current?page
    ????????:outer_window?=>?1,?#?links?around?beginning?and?end
    ????????:separator????=>?'?',?#?single?space?is?friendly?to?spiders?and?non-graphic?browsers
    ????????:param_name???=>?:page,
    ????????
    #add?by?chengang
    ????????:update?=>nil,?#ajax所要更新的html元素的id
    ????????:url_suffix?=>?''??#url的后綴,主要是為了補(bǔ)全REST所需要的url
    ????????#add?end
    ????????}
    ??mattr_reader?
    :pagination_options

    ??def?will_paginate_remote(entries?
    =?@entries,?options?=?{})
    ????total_pages?
    =?entries.page_count

    ????
    if?total_pages?>?1
    ??????options?
    =?options.symbolize_keys.reverse_merge(pagination_options)
    ??????page
    ,?param?=?entries.current_page,?options.delete(:param_name)
    ??????
    ??????inner_window
    ,?outer_window?=?options.delete(:inner_window).to_i,?options.delete(:outer_window).to_i
    ??????
    #add?by?chengang
    ??????update?=??options.delete(:update)
    ??????suffix?
    =??options.delete(:url_suffix)
    ??????url?
    =?request.env['PATH_INFO']?
    ??????url?
    +=?suffix?if?suffix
    ??????
    #add?end

    ??????
    min?=?page?-?inner_window
    ??????
    max?=?page?+?inner_window
    ??????
    if?max?>?total_pages?then?min?-=?max?-?total_pages
    ??????elsif?
    min?<?1??then?max?+=?1?-?min
    ??????
    end
    ??????
    ??????
    current???=?min..max
    ??????beginning?
    =?1..(1?+?outer_window)
    ??????tail??????
    =?(total_pages?-?outer_window)..total_pages
    ??????visible???
    =?[beginning,?current,?tail].map(&:to_a).flatten.sort.uniq
    ??????links
    ,?prev?=?[],?0

    ??????visible
    .each?do?|n|
    ????????
    next?if?n?<?1
    ????????
    break?if?n?>?total_pages

    ????????unless?n?
    -?prev?>?1
    ??????????
    prev?=?n
    ??????????
    #change?by?chengang
    ??????????text?=?(n==page???n?:?"[#{n}]")
    ??????????links?
    <<?page_link_remote_or_span((n?!=?page???n?:?nil),?'current',?text,?param,?update,?url)
    ????????
    else
    ??????????
    prev?=?n?-?1
    ??????????links?
    <<?''
    ??????????redo
    ????????
    end
    ??????
    end
    ??????
    ??????
    #change?by?chengang
    ??????links.unshift?page_link_remote_or_span(entries.previous_page,?'disabled',?options.delete(:prev_label),?param,?update,?url)
    ??????links
    .push????page_link_remote_or_span(entries.next_page,?????'disabled',?options.delete(:next_label),?param,?update,?url)
    ??????
    #change?end

    ??????content_tag?
    :div,?links.join(options.delete(:separator)),?options
    ????
    end
    ??
    end
    ??
    protected

    ??def?page_link_remote_or_span(page
    ,?span_class,?text,?param,?update,?url)
    ????unless?page
    ??????content_tag?
    :span,?text,?:class?=>?span_class
    ????
    else
    ??????link_to_remote?text
    ,?:update?=>?update,?:url?=>?"#{url}?#{param.to_sym}=#{page}",?:method=>:get
    ????
    end
    ??
    end


    在view中的使用如下所示:
    ??????????<%=will_paginate_remote?@topics,?:update?=>?'topicList',?:url_suffix?=>?url_suffix%>


    posted on 2007-09-02 15:42 陳剛 閱讀(4588) 評(píng)論(14)  編輯  收藏 所屬分類: Rails&Ruby

    評(píng)論

    # re: 讓will_paginate的分頁(yè)支持ajax  回復(fù)  更多評(píng)論   

    怎么沒(méi)反應(yīng)。。。
    2007-09-05 22:12 | 陳老師好

    # re: 讓will_paginate的分頁(yè)支持ajax  回復(fù)  更多評(píng)論   

    不行啊,你試過(guò)能行?
    2007-09-06 11:43 | 你自己試過(guò)?

    # re: 讓will_paginate的分頁(yè)支持ajax  回復(fù)  更多評(píng)論   

    url_suffix有沒(méi)有值,Ruby局域變量必須給一個(gè)初值,如果沒(méi)值讓他為nil
    2007-09-07 17:09 | 陳剛

    # re: 讓will_paginate的分頁(yè)支持ajax  回復(fù)  更多評(píng)論   

    我就是讓他為nil
    算了,已解決
    2007-09-22 15:18 | 陳老師好

    # re: 讓will_paginate的分頁(yè)支持ajax  回復(fù)  更多評(píng)論   

    不行,出錯(cuò)了,咋回事呢?
    undefined method `to_i' for {:page=>nil}:Hash
    RAILS_ROOT: ./script/../config/..

    2007-10-17 10:08 | skatefish

    # re: 讓will_paginate的分頁(yè)支持ajax  回復(fù)  更多評(píng)論   

    您好,調(diào)試了一下還是蠻好用的了,想問(wèn)一下能加上:loading=>Element.show這樣的參數(shù)么
    2007-10-17 13:36 | skatefish

    # re: 讓will_paginate的分頁(yè)支持ajax  回復(fù)  更多評(píng)論   

    參照了您的修改,現(xiàn)在加上了loading和complete的效果,還是不錯(cuò)的,謝謝:)
    2007-10-17 13:48 | skatefish

    # re: 讓will_paginate的分頁(yè)支持ajax  回復(fù)  更多評(píng)論   

    不錯(cuò),很好用,謝謝!
    2008-05-23 14:17 | 軒轅武

    # re: 讓will_paginate的分頁(yè)支持ajax  回復(fù)  更多評(píng)論   

    正想找一個(gè)支持AJAX的分頁(yè)呢,多謝分享!
    2008-11-14 22:36 | yanghuan

    # re: 讓will_paginate的分頁(yè)支持ajax  回復(fù)  更多評(píng)論   

    請(qǐng)問(wèn)一下:mattr_reader 這個(gè)是怎么用的??
    2008-11-14 22:48 | yanghuan

    # re: 讓will_paginate的分頁(yè)支持ajax  回復(fù)  更多評(píng)論   

    假如使用RJS的話,普通的分頁(yè)就可以實(shí)現(xiàn)無(wú)刷新頁(yè)面的效果了。
    2008-11-15 01:00 | yanghuan

    # re: 讓will_paginate的分頁(yè)支持ajax[未登錄](méi)  回復(fù)  更多評(píng)論   

    找不到page_count這個(gè)方法 這是wii_paginate內(nèi)建的方法嗎?
    2008-11-15 15:58 | YY

    # re: 讓will_paginate的分頁(yè)支持ajax  回復(fù)  更多評(píng)論   

    樓上的,你把
    total_pages = entries.page_count

    換成

    total_pages = entries.total_pages
    就沒(méi)問(wèn)題了,這是因?yàn)閃illPaginate::Collection中沒(méi)有page_count方法。
    2008-11-16 01:15 | yanghuan

    # re: 讓will_paginate的分頁(yè)支持ajax  回復(fù)  更多評(píng)論   

    過(guò)時(shí)了吧........
    2009-08-21 15:03 | fl1429
    主站蜘蛛池模板: 在线日韩日本国产亚洲| 久久99国产综合精品免费| free哆啪啪免费永久| 性生大片视频免费观看一级| 亚洲成在人线中文字幕| 亚洲精品国产综合久久一线| AV免费网址在线观看| 久久国产亚洲精品无码| 国产亚洲精品AA片在线观看不加载| 成人片黄网站色大片免费| 18禁男女爽爽爽午夜网站免费| 美女网站在线观看视频免费的| 久久久久亚洲国产AV麻豆| 亚洲国产日韩精品| 4480yy私人影院亚洲| 亚洲另类春色国产精品| 亚洲一区精品中文字幕| 国产亚洲精品岁国产微拍精品| 亚洲人成无码久久电影网站| 日韩a级毛片免费观看| 最新中文字幕电影免费观看| 91成人免费在线视频| **实干一级毛片aa免费| 久久国产精品成人片免费| 久久成人a毛片免费观看网站| 91视频免费观看| 精品国产污污免费网站| 国产在线观看免费视频软件| 一个人看的免费高清视频日本| 男人和女人高潮免费网站| 老司机午夜精品视频在线观看免费| 亚洲av成人片在线观看| 国产成人精品亚洲一区| www亚洲精品久久久乳| 老湿机一区午夜精品免费福利 | 亚洲毛片网址在线观看中文字幕| 在线免费观看一级毛片| 在线观看国产情趣免费视频| 国产在线观看免费不卡| 内射无码专区久久亚洲| 亚洲精品成人a在线观看|