后台的grid显示出来的功能模块步骤-----个人总结--12.28

对于在后台的grid,form等控件,在显示的时候前,一般在_prepareForm(这个比较复杂,后面再介绍!)

 

 

在RichardMason_Profile_Block_Adminhtml_Profile_Grid定义的

 

 

 protected function _prepareColumns()

    {

        $baseUrl = $this->getUrl();

 

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

            'header'    => Mage::helper('profile')->getText(Mage::registry('profile_category_id'),'content_heading'),

            'align'     => 'left',

            'index'     => 'content_heading'

        ));

 

        /*

        if(Mage::registry('profile_category_id') != RichardMason_Profile_Model_Profile::CATEGORY_RELEASES) {

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

           'header'    => Mage::helper('profile')->getText(Mage::registry('profile_category_id'),'content'),

           'align'     => 'left',

           'index'     => 'content'

       ));

        }

*/

 

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

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

                'header'        => Mage::helper('profile')->__('Store View'),

                'index'         => 'store_id',

                'type'          => 'store',

                'store_all'     => true,

                'store_view'    => true,

                'sortable'      => false,

                'filter_condition_callback'

                                => array($this, '_filterStoreCondition'),

            ));

        }

 

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

            'header'    => Mage::helper('profile')->__('Status'),

            'index'     => 'is_active',

            'type'      => 'options',

            'options'   => array(

                0 => Mage::helper('profile')->__('Disabled'),

                1 => Mage::helper('profile')->__('Enabled')

            ),

        ));

 

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

            'header'    => Mage::helper('profile')->__('Date Created'),

            'index'     => 'creation_time',

            'type'      => 'datetime',

        ));

 

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

            'header'    => Mage::helper('profile')->__('Last Modified'),

            'index'     => 'update_time',

            'type'      => 'datetime',

        ));

 

        return parent::_prepareColumns();

    }

 

$this->addColumns('store_id',array())

在这个函数里面的这个方法为Mage_Adminhtml_Block_Widget_Grid的方法,这里覆盖

目的是为了设置

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

访问url

profile/adminhtml_profile/news/key/fdsfsdfsadfds

 

 

grid的流程显示:

 

前提,各种资源,model,config.xml都配置好,

 

 

1.config.xml配置。

<adminhtml>

<menu>

<cms>

<children>

<news module="profile">

<title>News</title>

<sort_order>60</sort_order>

<action>profile/adminhtml_profile/news</action>

</news>

</children>

 

 

2 controller----newsaction方法的填写

 

在RichardMason_Profile_Adminhtml_ProfileController写一个函数newsAction()

 

 

public function newsAction(){

Mage::register('profile_category_id', RichardMason_Profile_Model_Profile::CATEGORY_NEWS);

$this->_initAction()

->renderLayout();

}

 

 

protected function _initAction() {

$this->loadLayout()

->_setActiveMenu('profile/items')

->_addBreadcrumb(Mage::helper('adminhtml')->__('Profiles Manager'), Mage::helper('adminhtml')->__('Profile Manager'));

return $this;

}

 

 

 

3.layouthandle

到profile.xml中写一个layouthandle:

 

  <profile_adminhtml_profile_news>

        <reference name="content">

            <block type="profile/adminhtml_profile" name="profile" />

        </reference>

    </profile_adminhtml_profile_news>

 

 

4.Block/Adminhtml/Profile.php

 

 

RichardMason_Profile_Block_Adminhtml_Profile   extends  Mage_Adminhtml_Block_Widget_Grid_Container

 public function __construct()

  {

    $this->_controller = 'adminhtml_profile';

    $this->_blockGroup = 'profile';

 

    $this->_headerText = Mage::helper('profile')->getText(Mage::registry('profile_category_id'), '_headerText');

    parent::__construct();

    parent::_addButton('add', array(

'label'     => Mage::helper('profile')->getText(Mage::registry('profile_category_id'), 'New'),

'onclick'   => 'setLocation(/''.$this->getUrl('*/*/edit', array('category_id' => Mage::registry('profile_category_id'))).'/')',

        'class'     => 'add',

    ));    

  }

5.

因为在该函数构造的时候需要用到grid,系统的默认路径为adminhtml/profile/grid.php(prifile.php的地址:adminhtml)

 

 

class RichardMason_Profile_Block_Adminhtml_Profile_Grid extends Mage_Adminhtml_Block_Widget_Grid

{

//1

    public function __construct()

    {

        parent::__construct();

        $this->setId('profileBlockGrid');

        $this->setDefaultSort('profile_id');

        $this->setDefaultDir('ASC');

    }

//2

    protected function _prepareCollection()

    {

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

$collection->addFieldToFilter('category_id', Mage::registry('profile_category_id'));

        /* @var $collection Mage_Cms_Model_Mysql4_Block_Collection */

        $this->setCollection($collection);

        return parent::_prepareCollection();

    }

//3

    protected function _prepareColumns()

    {

        $baseUrl = $this->getUrl();

 

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

            'header'    => Mage::helper('profile')->getText(Mage::registry('profile_category_id'),'content_heading'),

            'align'     => 'left',

            'index'     => 'content_heading'

        ));

 

        /*

        if(Mage::registry('profile_category_id') != RichardMason_Profile_Model_Profile::CATEGORY_RELEASES) {

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

           'header'    => Mage::helper('profile')->getText(Mage::registry('profile_category_id'),'content'),

           'align'     => 'left',

           'index'     => 'content'

       ));

        }

*/

 

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

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

                'header'        => Mage::helper('profile')->__('Store View'),

                'index'         => 'store_id',

                'type'          => 'store',

                'store_all'     => true,

                'store_view'    => true,

                'sortable'      => false,

                'filter_condition_callback'

                                => array($this, '_filterStoreCondition'),

            ));

        }

 

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

            'header'    => Mage::helper('profile')->__('Status'),

            'index'     => 'is_active',

            'type'      => 'options',

            'options'   => array(

                0 => Mage::helper('profile')->__('Disabled'),

                1 => Mage::helper('profile')->__('Enabled')

            ),

        ));

 

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

            'header'    => Mage::helper('profile')->__('Date Created'),

            'index'     => 'creation_time',

            'type'      => 'datetime',

        ));

 

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

            'header'    => Mage::helper('profile')->__('Last Modified'),

            'index'     => 'update_time',

            'type'      => 'datetime',

        ));

 

        return parent::_prepareColumns();

    }

//4

    protected function _afterLoadCollection()

    {

        $this->getCollection()->walk('afterLoad');

        parent::_afterLoadCollection();

    }

//5

    protected function _filterStoreCondition($collection, $column)

    {

        if (!$value = $column->getFilter()->getValue()) {

            return;

        }

 

        $this->getCollection()->addStoreFilter($value);

    }

 

    /**

     * Row click url

     *

     * @return string

     */

//6

    public function getRowUrl($row)

    {

        return $this->getUrl('*/*/edit', array('profile_id' => $row->getId(), 'category_id' => Mage::registry('profile_category_id') ));

    }

}

 

以上函数的讲解:

6.1

 public function __construct()

    {

        parent::__construct();  //上层构造函数调用。

        $this->setId('profileBlockGrid');   //Set object id field value

        $this->setDefaultSort('profile_id');  //以那个字段来排序

        $this->setDefaultDir('ASC');      //升序还是降序。

    }

 

6.2

 

   protected function _prepareCollection()

    {

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

$collection->addFieldToFilter('category_id', Mage::registry('profile_category_id'));

        /* @var $collection Mage_Cms_Model_Mysql4_Block_Collection */

        $this->setCollection($collection);

        return parent::_prepareCollection();

    }

 

得到过滤条件后的collection,然后将_data[collection],赋值。

 

 

6.3

//3

    protected function _prepareColumns()

    {

        $baseUrl = $this->getUrl();

 

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

            'header'    => Mage::helper('profile')->getText(Mage::registry('profile_category_id'),'content_heading'),

            'align'     => 'left',

            'index'     => 'content_heading'

        ));

 

        /*

        if(Mage::registry('profile_category_id') != RichardMason_Profile_Model_Profile::CATEGORY_RELEASES) {

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

           'header'    => Mage::helper('profile')->getText(Mage::registry('profile_category_id'),'content'),

           'align'     => 'left',

           'index'     => 'content'

       ));

        }

*/

 

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

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

                'header'        => Mage::helper('profile')->__('Store View'),

                'index'         => 'store_id',

                'type'          => 'store',

                'store_all'     => true,

                'store_view'    => true,

                'sortable'      => false,

                'filter_condition_callback'

                                => array($this, '_filterStoreCondition'),

            ));

        }

 

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

            'header'    => Mage::helper('profile')->__('Status'),

            'index'     => 'is_active',

            'type'      => 'options',

            'options'   => array(

                0 => Mage::helper('profile')->__('Disabled'),

                1 => Mage::helper('profile')->__('Enabled')

            ),

        ));

 

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

            'header'    => Mage::helper('profile')->__('Date Created'),

            'index'     => 'creation_time',

            'type'      => 'datetime',

        ));

 

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

            'header'    => Mage::helper('profile')->__('Last Modified'),

            'index'     => 'update_time',

            'type'      => 'datetime',

        ));

 

        return parent::_prepareColumns();

    }

 

_prepareColumns()

主要应用addColumns方法:增加grid的列!

$this->addColumns("此处为在数据库中的字段名称".array())

array(

header-----在页面显示的列名

index------数据库中的字段名称

type ------在前台显示使用的小控件类型,如options(select下拉条),datetime(会显示时间控件)

store_all--是否显示所有商店。

sortable---是否可以排序,也就是在页面的字段名称处点击,然后以此字段排序的功能是否开启,flase/true.

type--------store类型,应该是根据值去对应相应的key,得到相应的value吧。

filter_condition_callback---array($this,'_filterStoreCondition')

是store_id的,对于这个,还是很模糊,先这样吧。遇到store,直接把这个赋值过去就可以了。

 

6.4

    protected function _afterLoadCollection()

    {

        $this->getCollection()->walk('afterLoad');

        parent::_afterLoadCollection();

    }

还是不是很懂。看来想做grid,还是得摸索着做,以后再回来看奥秘。

 

6.5

//5

    protected function _filterStoreCondition($collection, $column)

    {

        if (!$value = $column->getFilter()->getValue()) {

            return;

        }

 

        $this->getCollection()->addStoreFilter($value);

    }

为addcolumns store_id处的filter_condition_callback---array($this,'_filterStoreCondition')

对应的方法。

 

 

 

//6

    public function getRowUrl($row)

    {

        return $this->getUrl('*/*/edit', array('profile_id' => $row->getId(), 'category_id' => Mage::registry('profile_category_id') ));

    }

}

得到URL,也就是grid中每行可以通过点击该行进入编辑的URL获得,就是通过这个函数得到的!

 

 

 

 

 

到此,吧一个grid显示出来编写结束!主要难点为block/Profile.php文件和block/profile/Grid.php文件的编写。

 

 

 

 

 

附加:

 parent::_addButton('add', array(

'label'     => Mage::helper('profile')->getText(Mage::registry('profile_category_id'), 'New'),

'onclick'   => 'setLocation(/''.$this->getUrl('*/*/edit', array('category_id' => Mage::registry('profile_category_id'))).'/')',

        'class'     => 'add',

    ));

add按钮,编写的时候模仿就可以,一切可以山寨!懂原理就可以!!

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值