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

来源:互联网 发布:批发部出货软件 编辑:程序博客网 时间:2024/05/20 06:30

对于在后台的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按钮,编写的时候模仿就可以,一切可以山寨!懂原理就可以!!

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 吃鸡使用改名卡改名符号怎么办 爱派平板电脑密码忘了怎么办 电脑优酷下载总显示未知错误怎么办 文本文档打开时显字符丢失怎么办 系统文件过大无法放进u盘怎么办 淘宝上买的密钥激活不了怎么办 苹果手机玩游戏屏幕卡住不动怎么办 电脑系统安装好一排英文字怎么办 赴日签证申请表写错了怎么办 不知道自己想要做什么工作怎么办 三星note4微信出现闪退怎么办 魅蓝note6手机自动闪退怎么办 苹果6s系统内存占用量过大怎么办 想在一年通过会计初级和中级怎么办 特殊岗位退休档察写的力工怎么办 面试时期望工资说低了。怎么办 面试时期望薪资写低了怎么办 高考后比一模差了80分怎么办 戒了烟我不习惯没有你我怎么办 没有你我不习惯没有你我怎么办 做什么都没兴趣嫌麻烦怎么办 快递还在路上就确认收货了怎么办 微信显示时间与手机不符怎么办 微信提示银行卡预留手机不符怎么办 得了湿疹后吃了海鲜严重了怎么办 看到小区街道乱扔的垃圾你会怎么办 去韩国干服务员不会讲韩语怎么办 华为手机键盘变英文字母大了怎么办 淘宝申请售后卖家余额不足怎么办 发票名称少写了一个字怎么办 微博数量与实际数量不一致怎么办 在淘宝中要买的商品卖完了怎么办 病因写错了保险不报销怎么办? 上学期间保险名字写错了怎么办 塑料盆上的商标纸撕了胶怎么办 川航买机票名字错了两个字怎么办 买机票护照号码填错了怎么办 换旅行证给孩子改名字怎么办 浦发信用卡卡片名字印错了怎么办 公主工作很辛苦坚持不下去怎么办 在表格里怎么办名字转换成拼音