Lift CSS SelectorとAjax
相変わらずボチボチLiftを触ってる。Lift 2.2 M1から入った便利な機能として、CSS Selectorがある。詳しくはWikiの該当のエントリを見てもらうとして、それとAjaxを組み合わせる方法。
■環境
Lift 2.2
■やりたい事
<form>タグを使った例はSimply Liftの4.8に載っているんだけど、普通のボタン(ajaxButton)を使いたかった。
DBのテーブルよりデータを取得して、HTMLの表にして出力。各行についているボタンをクリックすると、その行のデータに対する何らかの処理が行われるようにしたい。
結論から言うと簡単。
■コード
以下、ビューのHTMLコード。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | < table > < thead > < tr > < th >name</ th > < th >action</ th > </ tr > </ thead > < tbody > < tr class = "lift:test_snippet.listAll foo" > < td class = "name" ></ td > < td >< a href = "" class = "action" >action</ td > </ tr > </ tbody > </ table > |
snippetは以下の通り。
1 2 3 4 5 6 7 8 9 10 11 12 | def listAll : CssBindFunc = { ".foo *" # > ModelFoo.findAll.map(f = > ".name *" # > f.name & ".action *" # > SHtml.ajaxButton(Text( "Action" ), () = > doAction(f)) ) } def doAction(foo : ModelFoo) : JsCmd = { // fooのsomethingメソッドを呼び出して、その結果を画面に表示する S.notice(foo.something) Noop } |
Ajaxの処理が簡単に書けるのもLiftのいい所かな。