並ã³æ¿ãã®å¦çä¸ã«ã¯ã«ã¯ã«åãã¤ã¼ã¸ã±ã¼ã¿ã¼ã表示ããã
çµæ§ãæ§ãã¦ããã®ã ...ãajax_scaffoldã§ãå¦çä¸ã«ã¯ã«ã¯ã«åãã¤ã¼ã¸ã±ã¼ã¿ã¼ã«ã
æå°éã®æ©è½ã§ä¸¦ã³æ¿ããå®è£
ã¾ãã¯å¿ è¦æå°éã®æ©è½ã§ã並ã³æ¿ããå®è£ ãã¦ã¿ããã¾ã ã並ã³æ¿ãã®æ¹åãã対象åã®å¼·èª¿è¡¨ç¤ºãªã©ã®ãã£ã¼ãããã¯ã¯ä½ããªãã
- åã¿ã¤ãã«ã®ãªã³ã¯ãã¯ãªãã¯ããã¨ããã®åããã¼ã«ä¸¦ã³æ¿ããã
- åãåã¿ã¤ãã«ãç¹°ãè¿ãã¯ãªãã¯ããã¨ã並ã³æ¿ãã®æ¹åãå転ããã
<%# ---------- app/views/csvs/_listh.rhtml ---------- %> <tr> <% for title in @column_titles %> <% sort_direction = (params[:sort_field] == title) ? @next_direction : 'sorted' %> <th> <%= link_to Csv.human_attribute_name(title), :sort_field => title, :sort_direction => sort_direction %> </th> <% end %> </tr>
# ---------- app/controllers/csvs_controller.rb ---------- def list @next_direction = (params[:sort_direction] == 'asc') ? 'desc' : 'asc' order = "#{params[:sort_field] || 'id'} #{@next_direction}" @csv_pages, @csvs = paginate :csvs, :per_page => 10, :order => order end
ãã£ã¼ãããã¯ãç¡ãã¨ãç¶æ³ãã¤ããããå®è¡ããæä½ã«å¯¾ããä¸å®ãæ®ã...ã
ajaxã§ä¸¦ã³æ¿ã
å¦çä¸ã«ã¤ã¼ã¸ã±ã¼ã¿ã¼ãåãããã«ã¯ãajaxã®å©ç¨ãå¿ è¦ã¨èããlink_to_remoteã§åãå¦çãå®ç¾ãã¦ã¿ãã
- update => 'list_update'ã¯ãajaxæ´æ°ããé¨åãï¼div id="list_update"ï¼ã¿ã°ã§ãã¬ã¤ã¢ã¦ããã¡ã¤ã«å ¨ä½ãæå®ããã
<%# ---------- app/views/csvs/_listh.rhtml ---------- %> <tr> <% for title in @column_titles %> <% sort_direction = (params[:sort_field] == title) ? @next_direction : 'sorted' %> <th> <%= link_to_remote Csv.human_attribute_name(title), :update => 'list_update', :url => {:sort_field => title, :sort_direction => sort_direction} %> </th> <% end %> </tr>
- ajaxã§ã¯æç»ãã¡ã¤ã«ãæ示çã«æå®ããå¿ è¦ãããã®ã§ãrender :action => 'list'ã追å ããã
# ---------- app/controllers/csvs_controller.rb ---------- def list @next_direction = (params[:sort_direction] == 'asc') ? 'desc' : 'asc' order = "#{params[:sort_field] || 'id'} #{@next_direction}" @csv_pages, @csvs = paginate :csvs, :per_page => 10, :order => order render :action => 'list' end
- ï¼div id="list_update"ï¼ã¿ã°ã§å²ã¾ããé¨åããajaxã§æ´æ°ãããç¯å²ãã¤ã¾ãããã¼ã¸å ¨ä½ã«ãã¦ãã¾ã£ãã®ã ã
- æåããã£ãã<%= javascript_include_tag :defaults %>ãå¿ãã¦ãã¦ãæã£ãéãã«åä½ããæ©ãã§ãã¾ã£ããajaxãå©ç¨ããæã«ã¯å¿ é ã§ãã
<%# ---------- app/views/layouts/csvs.rhtml ---------- %> <div id="list_update"> <html> <head> <title>Csvs: <%= controller.action_name %></title> <%= stylesheet_link_tag 'scaffold' %> <%= stylesheet_link_tag 'list_o_matic' %> <%= javascript_include_tag :defaults %> </head> <body> <%= render :partial=>"layouts/menu2" %> <%= render :partial=>"layouts/flash" %> <%= yield %> </body> </html> </div>
以ä¸ã§ãajaxã«ãããã¼ã¸æ´æ°ã«ãªã£ãã並ã³æ¿ãã®ãªã³ã¯ãã¯ãªãã¯ãã¦ããURLã¯å¤åããªããªã£ãã
å¦çä¸ã«ã¯ã«ã¯ã«åãã¤ã¼ã¸ã±ã¼ã¿ã¼ã表示
ajax_scaffoldã®å¦çæ¹æ³ãã®ã¾ã¾ã ããèªåãªãã«ç解ãã¦ãã£ã¦ã¿ãã
- ajax_scaffoldãå©ç¨ãã¦ããã¨ãpublic/images/indicator.gifãã¤ã³ã¹ãã¼ã«ããã¦ããããããã¯ã«ã¯ã«åãã¤ã¼ã¸ã±ã¼ã¿ã¼ã«ãªããgifã¢ãã¡ã¼ã·ã§ã³ã®ç»åã ã試ãã«ãã©ã¦ã¶ã«ãã©ãã°ãã¦ã¿ãã¨ãã¤ã¼ã¸ã±ã¼ã¿ã¼ãã¯ã«ã¯ã«åã£ã¦ããç»åã表示ãããã
- å¦çä¸ã«ã¤ã¼ã¸ã±ã¼ã¿ã¼ã表示ããããã«ãlink_to_remoteã®:loading => ãªãã·ã§ã³ãæå®ãããããã§æå®ãããã¨ã¯ãå¦çã®éã ãå®è¡ãããããã ãå¦çãçµããã¨å ã®ç¶æ ã«æ»ãã
- :loading => ãªãã·ã§ã³ã§ã¯ã"Element.addClassName('#{title}','loading');"ã¨ããJavaScriptãæå®ãããid="#{title}"ã®ã¿ã°ã«ãclass="loading"ã追å ããå¦çã®ããã ã
- ã¤ã¾ããï¼th id=<%= title %>ï¼ã¿ã°ã¯ã並ã³æ¿ãã®å¦çä¸ã ãï¼th id=<%= title %> class="loading"ï¼ã¨ããç¶æ ã«ãªãã
<%# ---------- app/views/csvs/_listh.rhtml ---------- %> <tr> <% for title in @column_titles %> <% sort_direction = (params[:sort_field] == title) ? @next_direction : 'sorted' %> <th id=<%= title %>> <%= link_to_remote Csv.human_attribute_name(title), :update => 'list_update', :url => {:sort_field => title, :sort_direction => sort_direction}, :loading => "Element.addClassName('#{title}','loading');" %> </th> <% end %> </tr>
- ã¹ã¿ã¤ã«ã·ã¼ãã§ã¯ãï¼th id=<%= title %> class="loading"ï¼ã¿ã°ã«å¯¾ãã表示è¨å®ãè¡ããä¸çªä¸ã®ãããã¯ã§ãå¦çä¸ã®ãã¼ãã«ãããã¼ã®èæ¯ç»åã«ãindicator.gifãæå®ãã¦ããã
background: lavender url(../images/indicator.gif) right 50% no-repeat;
- ãã®æå®ã§ãclass="loading"ã®ç¶æ ã®æã ããindicator.gifãèæ¯ç»åã¨ãã¦è¡¨ç¤ºãã¦ãããã®ã ã
th { text-align: left; } th a { font: bold 11px verdana, sans-serif; display: block; } th a, th a:visited { color: #999; padding: 2px 20px 2px 2px; } th a:hover { background-color: #000; color: #fff; } th.loading a, th.loading a:hover { background: lavender url(../images/indicator.gif) right 50% no-repeat; }
以ä¸ã§ã並ã³æ¿ãã®å¦çä¸ã«ã¯ãã¤ã¼ã¸ã±ã¼ã¿ã¼ãã¯ã«ã¯ã«åã£ã¦ãããï¼
並ã³æ¿ãæ¹åã®ç¢å°ãèæ¯ç»åã§
- ãã®å¾ã並ã¹æ¿ãæ¹åã示ãä¸ä¸ã®ç¢å°ã表示ããããã«ãã¦ã¿ãã
>ã¿ã°ã«ãclass=<%= sort_direction %> ã追å ããã ãã <%# ---------- app/views/csvs/_listh.rhtml ---------- %> <tr> <% for title in @column_titles %> <% sort_direction = (params[:sort_field] == title) ? @next_direction : 'sorted' %> <th class=<%= sort_direction %> id=<%= title %>> <%= link_to_remote Csv.human_attribute_name(title), :update => 'list_update', :url => {:sort_field => title, :sort_direction => sort_direction}, :loading => "Element.addClassName('#{title}','loading');" %> </th> <% end %> </tr>
- ç¢å°ãèæ¯ç»åã¨ãã¦è¡¨ç¤ºããã¹ã¿ã¤ã«ã·ã¼ããè¨å®ããã¨ã並ã³æ¿ãã®æ¹åã表示ãããããã«ãªãã
th.asc a { background: lavender url(../images/arrow_up.gif) right 50% no-repeat; color: #000; } th.asc a:hover { background: #000 url(../images/arrow_up.gif) right 50% no-repeat; color: #fff; } th.desc a { background: lavender url(../images/arrow_down.gif) right 50% no-repeat; color: #000; } th.desc a:hover { background: #000 url(../images/arrow_down.gif) right 50% no-repeat; color: #fff; }
ç¾å¨ã®è¦ãç®ã¯ãããªæãã ã