magento admin module -- part-1

首先在你自定义模块的Block文件夹下创建Adminhtml文件夹,然后创建Employee.php文件(具体的根据自己定义的模块来)

Employee.php文件是grid容器文件,内容如下:

class Excellence_Employee_Block_Adminhtml_Employee extends Mage_Adminhtml_Block_Widget_Grid_Container
{
  public function __construct()
  {
    $this->_controller = 'adminhtml_employee';
    $this->_blockGroup = 'employee';   //module name
    $this->_headerText = Mage::helper('employee')->__('Employee Manager');
    $this->_addButtonLabel = Mage::helper('employee')->__('Add Employee');
    parent::__construct();
  }
}

这里我们的类继承自 Mage_Adminhtml_Block_Widget_Grid_Container类,对于我们接下来的Grid是否能正常工作,这个是很重要的

接下来看构造函数里的两个变量

$this->_controller = 'adminhtml_employee';
$this->_blockGroup = 'employee';

这两行是很重要的,它们主要告诉在magento中Grid.php的位置,我们打开 Mage_Adminhtml_Block_Widget_Grid_Container类文件,找到_prepareLayout()方法,我们将会看到

protected function _prepareLayout()
    {
        $this->setChild( 'grid',
            $this->getLayout()->createBlock( $this->_blockGroup.'/' . $this->_controller . '_grid',
            $this->_controller . '.grid')->setSaveParametersInSession(true) );
        return parent::_prepareLayout();
    }

在这段代码中定义了Grid文件的位置,和一部分的类名,因此我们可以推断出Grid文件一部分的类名应该是

$this->_blockGroup.'/' . $this->_controller . '_grid'
//or
employee/adminhtml_employee_grid

接下来,创建我们的Grid.php文件,根据上面的分析,我们应该在 Excellence/Employee/Block/Adminhtml/Employee/Grid.php 下创建

class Excellence_Employee_Block_Adminhtml_Employee_Grid extends Mage_Adminhtml_Block_Widget_Grid
{
    public function __construct()
    {
        parent::__construct();
        $this->setId('employeeGrid');
        $this->setDefaultSort('id');
        $this->setDefaultDir('ASC');
        $this->setSaveParametersInSession(true);
    }
 
    protected function _prepareCollection()
    {
        $collection = Mage::getModel('employee/employee')->getCollection();
        $this->setCollection($collection);
        return parent::_prepareCollection();
    }
 
    protected function _prepareColumns()
    {
        $this->addColumn('id', array(
          'header'    => Mage::helper('employee')->__('ID'),
          'align'     =>'right',
          'width'     => '10px',
          'index'     => 'id',
        ));
 
        $this->addColumn('name', array(
          'header'    => Mage::helper('employee')->__('Name'),
          'align'     =>'left',
          'index'     => 'name',
          'width'     => '50px',
        ));
 
          
        $this->addColumn('content', array(
            'header'    => Mage::helper('employee')->__('Description'),
            'width'     => '150px',
            'index'     => 'content',
        ));
        return parent::_prepareColumns();
    }
}

现在,你会发现我们的类需要继承自 Mage_Adminhtml_Block_Widget_Grid类,这是很重要的

简要的分析下代码:

$this->setId(‘employeeGrid’) ==>这个是设置Grid的ID,例如在html <div>下面的id属性,如果你在页面当中使用了多个grids,那么这个id必须是唯一的

$this->setDefaultSort(‘id’) ==> 此处顾名思义,告诉我们根据哪个列来进行默认的排序

$this->setDefaultDir(‘ASC’) ==> 默认的排序规则,是升序还是降序

$this->setSaveParametersInSession(true) ==> 这个语句主要是把你的Grid设置到session里去操作,例如,当我们在第二页的时候(比如在此页进行了多选操作),然后我们进行搜索操作,当刷新或者返回到第二页的时候,我们在第二页进行的所有操作(多选操作)仍然不变,它不会恢复到默认的form

接下来,我们分析_prepareCollection()方法,这个方法其实很简单,就是返回一个我们想在grid模板里面显示的collection 

接下来,我们看看 _prepareColumns() 方法. 这里我们增加列到grid里面

$this->addColumn('id', array(
          'header'    => Mage::helper('employee')->__('ID'),
          'align'     =>'right',
          'width'     => '10px',
          'index'     => 'id',
        ));

‘id’ 在这个列里面是唯一的

‘header’ 是这个列的名字

‘index’ 是从model中得到的collection里面的字段. 这个id列必须存在于我们的collection中

这些都是我们要在Grid里面显示的内容,现在我们来创建控制器,这样我们就能在admin中看到grid

Admin Controller and Admin Menu

首先,我们在config.xml文件中加入以下内容

<adminhtml>
        <menu>
            <employee module="employee">
                <title>Employee</title>
                <sort_order>71</sort_order>              
                <children>
                    <items module="employee">
                        <title>Manage Employees</title>
                        <sort_order>0</sort_order>
                    <action>employee/adminhtml_employee</action>
                    </items>
                </children>
            </employee>
        </menu>
        <acl>
            <resources>
                <all>
                    <title>Allow Everything</title>
                </all>
                <admin>
                    <children>
                        <Excellence_Employee>
                            <title>Employee Module</title>
                            <sort_order>10</sort_order>
                        </Excellence_Employee>
                    </children>
                </admin>
            </resources>
        </acl>
        <layout>
            <updates>
                <employee>
                    <file>employee.xml</file>
                </employee>
            </updates>
        </layout>
    </adminhtml>
我将在后面讲解acl节点的含义,现在我们把焦点放到menu节点下。action节点包含的内容是我们后台访问的控制器的URL,假设我们要增加两个menu items,我们只需要
<children>
                    <items module="employee">
                        <title>Manage Employees</title>
                        <sort_order>0</sort_order>
                    <action>employee/adminhtml_employee</action>
                    </items>
                                         <items2 module="employee">
                        <title>Manage Employees</title>
                        <sort_order>0</sort_order>
                    <action>employee/adminhtml_employee2</action>
                    </items2>
                </children>

现在,我们来创建控制器,在Adminhtml文件夹下,接下来我们创建EmployeeController.php文件

class Excellence_Employee_Adminhtml_EmployeeController extends Mage_Adminhtml_Controller_action
{
 
    public function indexAction() {
        $this->loadLayout();
                $this->renderLayout();
    }

这和我们创建前台控制器一样,我们只需要继承不同的类,接下来在 design/adminhtml/default/default/layout/employee.xml 创建一个 employee.xml的布局文件,在config.xml里面的adminhtml节点下,我们已经定义了一个新的布局文件,所以magento知道从employee.xml文件中读取布局信息,内容如下

<?xml version="1.0"?>
<layout version="0.1.0">
    <employee_adminhtml_employee_index>
        <reference name="content">
            <block type="employee/adminhtml_employee" name="employee" />
        </reference>
    </employee_adminhtml_employee_index>
</layout>
因此,现在我们进入admin点击 menu item,我们就能看到我们创建的grid了。

原文地址:http://excellencemagentoblog.com/admin-part1-series-magento-admin-forms-grids-controllers-tabs


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值