1

 

首先写controller

public function kunAction(){

$this->loadLayout();

$this->_addContent($this->getLayout()->createBlock('gao/manage_gao'));

 

$this->renderLayout();

 

}

2

编写block

app/code/community/AQ/Gao/Block/Manage/Gao.php

app/code/community/AQ/Gao/Block/Manage/Gao/Grid.php

2.1

app/code/community/AQ/Gao/Block/Manage/Gao.php

 

<?php 

 

class AQ_Gao_Block_Manage_Gao extends Mage_Adminhtml_Block_Widget_Grid_Container{

public function __construct(){

//生成路径使用

$this->_controller = 'manage_gao';

//模块名字

$this->_blockGroup = 'gao';

//头标题

$this->_headerText = 'Blog Post Manager';

parent::__construct();

}

protected function _prepareLayout(){

$this->setChild('add_new_button',

$this->getLayout()->createBlock('adminhtml/widget_button')

->setData(array(

'label' => 'Add Post',

//按钮按下后执行的路径。

'onclick'=>"setLocation('".$this->getUrl('*/*/new')."') ",

'class' =>'add'

))

 

 

);

//多商店情况下。

if (!Mage::app()->isSingleStoreMode()) {

            $this->setChild('store_switcher',

                $this->getLayout()->createBlock('adminhtml/store_switcher')

                    ->setUseConfirm(false)

                    ->setSwitchUrl($this->getUrl('*/*/*', array('store'=>null)))

            );

        }

//子block指定。这里指定的就是app/code/community/AQ/Gao/Block/Manage/Gao/Grid.php

 

        $this->setChild('grid', $this->getLayout()->createBlock('gao/manage_gao_grid', 'gao.grid'));

        return parent::_prepareLayout();

}

 

 

}

2.2

app/code/community/AQ/Gao/Block/Manage/Gao/Grid.php

 

此block为2.1的子block

 

<?php

 

class AQ_Gao_Block_Manage_Gao_Grid extends Mage_Adminhtml_Block_Widget_Grid{

public function __construct(){

 

parent::__construct();

//

$this->setId('gaoGrid');

//按时间排序

$this->setDefaultSort('created_time');

//排序方式

$this->setDefaultDir('DESC');

$this->setSaveParametersInSession(true);

 

}

protected function _getStore()

    {

        $storeId = (int) $this->getRequest()->getParam('store', 0);

        return Mage::app()->getStore($storeId);

    }

 

 

protected function _prepareCollection()

{

//数据的加载

$collection = Mage::getModel('blog/blog')->getCollection();

$store = $this->_getStore();

if ($store->getId()) {

            $collection->addStoreFilter($store);

}

//

$this->setCollection($collection);

return parent::_prepareCollection();

}

 

 

protected function _prepareColumns(){

$this->addColumn('post_id', array(

 'header'    => 'ID',

 'align'     =>'right',

 'width'     => '50px',

 'index'     => 'post_id',

));

$this->addColumn('title', array(

 'header'    => 'Title',

 'align'     =>'left',

 'index'     => 'title',

));

//echo "fdfd";

return parent::_prepareColumns();

 

 

}

//点击其中的每行访问的路径地址。并指定传出的ID值。

public function getRowUrl($row)

{

return $this->getUrl('*/*/edit', array('id' => $row->getId()));

}

 

 

}