TableKitのEditに対応するコントローラアクション習作

簡単・便利のTableKitはかなりお役立ちのテーブルソートScript。最近ちょいちょい使うので、これのEditableに対応したZend FrameworkのActionをメモっておきます。

基本として、

&row=n: The row index of the edited cell
&cell=n: The row index of the edited cell
&id=id: The id attribute of the row, it may be useful to set this to the record ID you are editing
&field=field: The id attribute of the header cell of the column of the edited cell, it may be useful to set this to the field name you are editing
&value=xxxxxx: The rest of the POST body is the serialised form. The default name of the field is ‘value’.

というリクエストを取得して、データの修正を行います。

[php]
public function updateFromTablekitAction()
{
$content = null;

// IDを取得。
try {
$id = $this->_getParam(‘id’);

// テーブルのidを取得したり、どの表から取得、というのを取得するための苦肉の処理です。。
list($from,$id) = explode(‘:’, $id);

if (!ctype_digit($id)) {
throw new Exception();
}
$field = $this->_getParam(‘field’);
$value = $this->_getParam(‘value’);

$hoge = new HOGE_TABLE();
$hoge_row = $hoge->find($id)->current();
$hoge_row->$field = $value;
$id = $hoge_row->save();

$site_row = $hoge->find($id)->current();
$this->view->content = $hoge_row->$field;
}
catch (Exception $e) {
$this->view->content = “#ERROR#”;
}
}
[/php]

テーブルに再表示するための文字列として$contentをview_helperに渡していますが、まあ普通にechoしたほうが楽な気がします。
どのテーブルの値を変更するのか、$fromなどから取得したりします。
Validateも、もう少しきちんと行わなければいけないですね。
場当たり的ですが、今のところエラー表示をひとまとめにtry~catchでくくってます。
idの列などEditableにしない列は

[html]

[/html]

と、テーブルヘッダclassを”noedit” しておくことで編集不可になります。
#noeditのクラス名は衝突を避けるため変更可能です。
#TableKit.options.noEditClass = ‘unavailabe’
#とか。