Ajax †
- prototype.js を内蔵している。Helperが用意されている
- 日本語もUTF-8なら問題ない
- Akelos (svn r988)
サンプル1 divタグの内容を動的に書き換える †
- layout
<?php echo $asset_tag_helper->javascript_include_tag('prototype') ?>
- view (Rubyのタグそのまま)
<div id="time_div">
I don't have the time, but
<%= link_to_remote( "click here",
:update => "time_div",
:url =>{ :action => :say_when }) %>
and I will look it up. <br />
</div>
- controller
function say_when(){
$this->renderText("<p>テスト The time is <b>" . date('Y/m/d H:i:s') . "</b></p>");
}
サンプル2 ajax form †
- layout サンプル1と同じ
- view (Rubyのタグそのまま)
<%= form_remote_tag(:update => "my_list",
:url => { :action => :add_item },
:position => "top" ) %>
New item text:
<%= text_field_tag :newitem %>
<%= submit_tag "Add item with Ajax" %>
<%= end_form_tag %>
- controller
function add_item(){
$this->renderText("<li>" . $this->params['newitem'] . "</li>");
}
サンプル3 ライブサーチ (NG) †
- view
<%= text_field_tag :searchtext %>
<%= observe_field(:searchtext,
:frequency => 0.25,
:update => :search_hits,
:url => { :action => :live_search }) %>
<p>Search Results:</p>
<div id="search_hits"></div>
- コントローラ側で $this->params['searchtext']='値'で取得できると思いきや、$this->params['値']=''になっている。helperの不具合だろうか。
selectを動的に書き換える †
- view
<select id="options" onchange="<?=$prototype_helper->remote_function(array('update' => 'options', 'url' => array('action' => 'update_options') )); ?>">
<option value="0">Hello</option>
<option value="1">World</option>
</select>
- controller
function update_options(){
$data = array("ああ"=>"いい","うう"=>"ええ");
$result="";
foreach($data as $key=>$val){
$result .= "<option value=\"{$key}\">{$val}</option>";
}
$this->renderText($result);
}
コメント †
コメントはありません。 Comments/Memo/Akelos/sample/ajax?