上松です。
管理画面へのページ追加について、3回目になります。
一覧画面の表示とデータの新規登録についてです。
Gridは以下のような構成になっています。
前回、コンテナの設定をしたので今回はグリッドの設定をします。
Gridクラス
app/code/local/Others/Another/Block/Adminhtml/Example/Grid.php
class Others_Another_Block_Adminhtml_Example_Grid extends Mage_Adminhtml_Block_Widget_Grid { public function __construct() { parent::__construct(); $this->setId('example_grid'); } protected function _prepareCollection() { ・・・コレクションクラスの設定 $collection = Mage::getModel('others_another_m/example')->getCollection(); $this->setCollection($collection); parent::_prepareCollection(); } protected function _prepareColumns() { ・・・表示するカラムの設定 $this->addColumn( ・・・ カラムの追加 'id', ・・・ 画面上で使う名前 array( 'header' => 'ID', ・・・ カラムのヘッダ表示名 'align' => 'right', ・・・ 配置指定 'width' => '50px', ・・・ 幅 'index' => 'id', ・・・ カラム名 )); $this->addColumn('name', array( 'header' => 'name', 'index' => 'name', )); $this->addColumn('description', array( 'header' => 'description', 'index' => 'description', )); $this->addColumn('other', array( 'header' => 'other', 'index' => 'other', )); parent::_prepareColumns(); } }
_prepareCollection()で使用するコレクションクラスを設定します。
取得するカラムやあらかじめフィルタリングを設定したい場合などはこのメソッド内で設定しておきます。
親クラスのメソッド内では画面で選択されたフィルタなどを設定しています。
_prepareColumns()では表示するカラムを設定します。
まずaddColumn()で表示カラムの追加をします。追加順で表示されるカラムの順番が決まります。オプションとして、ヘッダに表示される名前や、配置、この例にはありませんが表示方式などを設定します。
親クラスのメソッド内ではソート順などを設定しています。
・新規入力画面
「Add New」ボタンをクリックしたときの画面を設定します。
入力画面は以下のような構成になっています
・フォームコンテナ
app/code/local/Others/Another/Block/Adminhtml/Example/Edit.php
class Others_Another_Block_Adminhtml_Example_Edit extends Mage_Adminhtml_Block_Widget_Form_Container { public function __construct() { $this->_objectId = 'id'; ・・・ テーブルのプライマリキー $this->_blockGroup = 'others_another'; ・・・ config.xmlで設定したブロックグループ $this->_controller = 'adminhtml_example'; ・・・ フォームのブロックを作成するのに使われる $this->_mode = 'edit'; ・・・フォームのブロックを作成するのに使われる $this->_headerText = Mage::helper('others_another_hp')->__('New Examples'); ・・・ フォームタイトル parent::__construct(); } }
一覧とGridと同じような方式で$_blockGroupと$_controllerと$_modeを使用して以下のようにフォームクラスが設定されます。
(ブロックグループ)/$this->_controller.'_'.$this->_mode.'_form'
でブロックが指定されます。今回の場合は最終的に
'Others_Another_Block' + '_Adminhtml_Example' + '_Edit' + '_Form'
というクラスが指定されることになります。
親クラスのメソッド内ではテンプレートの設定と「Back」「Reset」「Save」ボタンの設定がされます。
(リクエストにIDがあった場合は「Delete」ボタンも追加されます)
・フォーム
app/code/local/Others/Another/Block/Adminhtml/Example/Edit/Form.php
class Others_Another_Block_Adminhtml_Example_Edit_Form extends Mage_Adminhtml_Block_Widget_Form { protected function _prepareForm() { $form = new Varien_Data_Form(array( 'id' => 'edit_form', ・・・ フォームのID 'action' => $this->getUrl('*/*/save', array('id' => $this->getRequest()->getParam('id'))), ・・・ フォームのアクションに設定されるURL 'method' => 'post', ・・・ フォームのメソッド 'enctype'=> 'multipart/form-data')); ・・・ フォームのエンコードタイプ $form->setUseContainer(true); ・・・ <form>タグの出力有無 $this->setForm($form); $fieldset = $form->addFieldset('example_form', array('legend' => 'example information')); $fieldset->addField( 'name', ・・・ <input>のid 'text', ・・・ タイプ array( 'label' => 'Name', ・・・ ラベルとして表示する文字列 'required' => true, ・・・ 必須設定 'name' => 'name', ・・・ <input>のname 'note' => 'The name of the example', 入力フィールド下に表示される文字列(注釈) )); $fieldset->addField('description', 'text', array( 'label' => 'Description', 'required' => true, 'name' => 'description', )); $fieldset->addField('other', 'text', array( 'label' => 'Other', 'required' => true, 'name' => 'other' )); return parent::_prepareForm(); } }
コンテナで設定した値で特定できるようにクラス名を設定します。
addField()で入力フィールドを追加します。追加順で表示の順番が決まります。
タイプはtextの他にcheckboxやradio、selectなどがあります。
‘required’をfalseにすると必須項目ではなくなります。
・レイアウト
app/design/adminhtml/default/default/layout/othres_another.xml
<adminhtml_example_new> <reference name="content"> <block type="others_another/adminhtml_example_edit" name="example_edit"/> </reference> </adminhtml_example_edit>
レイアウト定義をレイアウトファイルに追記します。
・コントローラ
app/code/local/Others/Another/controllers/Adiminhtml/ExampleController.php
class Others_Another_Adminhtml_ExampleController extends Mage_Adminhtml_Controller_Action { ...... public function newAction() { ・・・ 新規入力画面表示 $this->loadLayout(); $this->renderLayout(); } public function saveAction() { ・・・ データ保存 if ($data = $this->getRequest()->getPost()) { ・・・ 画面からのパラメータ取得 $model = Mage::getModel('others_another/example'); ・・・ 登録するテーブルのモデル $model->setData($data); ・・・ モデルにデータを設定 try { $model->save(); ・・・ モデルの保存 if (!$model->getId()) { Mage::throwException('error saving example'); ・・・ 保存失敗 } Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('others_another_hp')->__('example wasu success!')); ・・・ 成功メッセージ } catch (Exception $e) { Mage::getSingleton('adminhtml/session')->addError($e->getMessage()); ・・・ 失敗メッセージ } } else { Mage::getSingleton('adminhtml/session')->addError(Mage::helper('others_another_hp')->__('no data found to save')); } $this->_redirect('*/*/'); } ....... }
新規画面を表示するアクションと入力されたデータを保存するアクションを追加します。
取得したパラメータは名前と値の連想配列になっているので、
入力フォームのnameとカラム名が一致していれば、そのままモデルにセットすることができます。
成功メッセージ・失敗メッセージは遷移先(この場合は一覧画面)に表示されます。
データ登録が成功すれば、 一覧にデータが追加されているのが確認できると思います。
次回以降でデータの編集・削除・Massactionについてまとめようと思います。