如何显示magento管理网格

时间:2015-11-16 05:12:14

标签: php magento

请帮忙!我在创建Magento Admin Grid时完全迷失了方向。现在已经一周了。

我已经创建了一个来自本教程http://ecommerce.eglobeits.com/blog/magento-admin-module-development-with-grid-and-tab/的模块。问题是,电网不会出现。我已经通过安装脚本成功创建了数据库表。

问题:

如何使网格正常工作?

你能发现问题吗?

这些是代码。

应用程序/代码/本地/ RTS / Pmadmin的/ etc / config.xml中

<config>
<modules>
    <Rts_Pmadmin>
        <version>0.1.0</version>
    </Rts_Pmadmin>
</modules>
<global>
    <models>
        <Rts_Pmadmin>
            <class>Rts_Pmadmin_Model</class>
            <resourceModel>pmadmin_resource</resourceModel>
        </Rts_Pmadmin>
        <pmadmin_resource>
            <class>Rts_Pmadmin_Model_Resource</class>
            <entities>
                <pricematrix>
                    <table>pricematrix</table>
                </pricematrix>
            </entities>
        </pmadmin_resource>
    </models>
    <resources>
        <pricematrix_setup>
            <setup>
                <module>Rts_Pmadmin</module>
                <class>Rts_Pmadmin_Model_Resource_Mysql4_Setup</class>
            </setup>
            <connection>
                <use>core_setup</use>
            </connection>
        </pricematrix_setup>
        <pricematrix_write>
            <connection>
                <use>core_write</use>
            </connection>
        </pricematrix_write>
        <pricematrix_read>
            <connection>
                <use>core_read</use>
            </connection>
        </pricematrix_read>
    </resources>
    <helpers>
        <Rts_Pmadmin>
            <class>Rts_Pmadmin_Helper</class>
        </Rts_Pmadmin>
    </helpers>
</global>
<admin>
    <routers>
        <adminhtml>
            <args>
                <modules>
                    <Rts_Pmadmin after="Mage_Adminhtml">Rts_Pmadmin</Rts_Pmadmin>
                </modules>
            </args>
        </adminhtml>
    </routers>
</admin>
</config>

应用程序/代码/本地/ RTS / Pmadmin的/ etc / adminhtml.xml

<config>
<menu>
    <customer>
        <children>
            <pmadmin>
                <title>Price Matrix</title>
                <sort_order>9999</sort_order>
                <action>adminhtml/pmadmin/index</action>
            </pmadmin>
        </children>
    </customer>
</menu>
<acl>
    <resources>
        <admin>
            <children>
                <customer>
                    <children>
                        <padmin>
                            <title>Price Matrix</title>
                            <sort_order>9999</sort_order>
                        </padmin>
                    </children>
                </customer>
            </children>
        </admin>
    </resources>
</acl>
<layout>
    <updates>
        <pmadmin>
            <file>rts/pricematrix.xml</file>
        </pmadmin>
    </updates>
</layout>
</config>

应用程序/设计/ adminhtml /默认/默认/布局/ RTS / pricematrix.xml

<layout version="0.1.0">
<adminhtml_pmadmin_index>
    <reference name="content">
        <block type="pmadmin/pmadmincontainer" name="pmadmincontainer"></block>
    </reference>
</adminhtml_pmadmin_index>

应用程序/代码/本地/ RTS / Pmadmin /控制器/ PmadminController.php

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

public function newAction(){
    $this->loadLayout();
    $this->_addContent($this->getLayout()->createBlock('pmadmin/adminhtml_pricematrix_edit'))
            ->_addLeft($this->getLayout()->createBlock('pmadmin/adminhtml_pricematrix_edit_tabs'));
    $this->renderLayout();
}

public function deleteAction() {
    if ($this->getRequest()->getParam('id') > 0) {
        try {
            $model = Mage::getModel('pmadmin/pricematrix');

            $model->setId($this->getRequest()->getParam('id'))
                    ->delete();

            Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('adminhtml')->__('Item was successfully deleted'));
            $this->_redirect('*/*/');
        } catch (Exception $e) {
            Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
            $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
        }
    }
    $this->_redirect('*/*/');
}

public function saveAction() {

    if ($data = $this->getRequest()->getPost())
    {
        $model = Mage::getModel('pmadmin/pricematrix');
        $id = $this->getRequest()->getParam('id');
        foreach ($data as $key => $value)
        {
            if (is_array($value))
            {
                    $data[$key] = implode(',',$this->getRequest()->getParam($key));
            }
        }

        if ($id) {
            $model->load($id);
        }
        $model->setData($data);

        Mage::getSingleton('adminhtml/session')->setFormData($data);
        try {
            if ($id) {
                $model->setId($id);
            }

            $model->save();

            if (!$model->getId()) {
                Mage::throwException(Mage::helper('pricematrix')->__('Error saving news details'));
            }

            Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('pricematrix')->__('Details was successfully saved.'));

            Mage::getSingleton('adminhtml/session')->setFormData(false);

            // The following line decides if it is a "save" or "save and continue"
            if ($this->getRequest()->getParam('back')) {
                $this->_redirect('*/*/edit', array('id' => $model->getId()));
            } else {
                $this->_redirect('*/*/');
            }

        } catch (Exception $e) {
            Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
            if ($model && $model->getId()) {
                $this->_redirect('*/*/edit', array('id' => $model->getId()));
            } else {
                $this->_redirect('*/*/');
            }
        }

      return;
    }
    Mage::getSingleton('adminhtml/session')->addError(Mage::helper('pricematrix')->__('No data found to save'));
    $this->_redirect('*/*/');
}


public function editAction() {

    $id = $this->getRequest()->getParam('pricematrix_id', null);

    $model = Mage::getModel('pmadmin/pricematrix');
    if ($id) {
        $model->load((int) $id);
        if ($model->getId()) {
            $data = Mage::getSingleton('adminhtml/session')->getFormData(true);
            if ($data) {
                $model->setData($data)->setId($id);
            }
        } else {
            Mage::getSingleton('adminhtml/session')->addError(Mage::helper('pricematrix')->__('matrix does not exist'));
            $this->_redirect('*/*/');
        }
    }
    Mage::register('pricematrix_data', $model);

    $this->_title($this->__('pricematrix'))->_title($this->__('Edit Matrix'));
    $this->loadLayout();
    $this->getLayout()->getBlock('head')->setCanLoadExtJs(true);

    $this->_addContent($this->getLayout()->createBlock('pmadmin/adminhtml_pricematrix_edit'))
            ->_addLeft($this->getLayout()->createBlock('pmadmin/adminhtml_pricematrix_edit_tabs'));
    $this->renderLayout();
}    
} 

应用程序/代码/本地/ RTS / Pmadmin /砌块/ Adminhtml / Pmadmincontainer.php

class Rts_Pmadmin_Block_Adminhtml_Pmadmincontainer extends Mage_Adminhtml_Block_Widget_Grid_Container {

public function __construct() {
    $this->_controller = 'pmadmin_pmadmin';
    $this->_blockGroup = 'pmadmin';
    $this->_headerText = Mage::helper('pricematrix')->__('Price Matrix Manager');
    $this->_addButtonLabel = Mage::helper('pricematrix')->__('Add New Matrix');
    parent::__construct();
}
}

应用程序/代码/本地/ RTS / Pmadmin /砌块/ Adminhtml / Pricematrix / Grid.php

class Rts_Pmadmin_Block_Adminhtml_Pricematrix_Grid extends Mage_Adminhtml_Block_Widget_Grid {

public function __construct() {
    parent::__construct();
    $this->setId('pricematrix_grid');
    $this->setDefaultSort('pricematrix_id');
    $this->setDefaultDir('ASC');
    $this->setSaveParametersInSession(true);
}

protected function _prepareCollection() {
    $collection = Mage::getModel('pmadmin/pricematrixcollection')->getCollection();
    $this->setCollection($collection);
    return parent::_prepareCollection();
}

protected function _prepareColumns() {
    $this->addColumn('pricematrix_id', array(
        'header' => Mage::helper('pricematrix')->__('ID'),
        'align' => 'right',
        'width' => '10px',
        'index' => 'pricematrix_id',
    ));

    $this->addColumn('title', array(
        'header' => Mage::helper('pricematrix')->__('Title'),
        'align' => 'left',
        'index' => 'title',
        'width' => '50px',
    ));


    $this->addColumn('short_description', array(
        'header' => Mage::helper('pricematrix')->__('Description'),
        'width' => '150px',
        'index' => 'short_description',
    ));

    $this->addColumn('file_path', array(
        'header' => Mage::helper('pricematrix')->__('File Path'),
        'width' => '150px',
        'index' => 'file_path',
    ));


    $this->addColumn('customer_group', array(
        'header' => Mage::helper('pricematrix')->__('Customer Group'),
        'width' => '150px',
        'index' => 'customer_group',
    ));


    $this->addColumn('creation_time', array(
        'header' => Mage::helper('pricematrix')->__('Posted On'),
        'width' => '150px',
        'index' => 'creation_time',
    ));

    return parent::_prepareColumns();
}


public function getRowUrl($row) {
    return $this->getUrl('*/*/edit', array('id' => $row->getId()));
     }

应用程序/代码/本地/ RTS / Pmadmin /砌块/ Adminhtml / Pricematrix / Edit.php

class Rts_Pmadmin_Model_Pricematrixcollection extends Mage_Core_Model_Mysql4_Collection_Abstract {
public function _construct()
{
    parent::_construct();
    $this->_init('pmadmin/pricematrix');
}
}

应用程序/代码/本地/ RTS / Pmadmin /砌块/ Adminhtml / Pricematrix /编辑/ form.php的

 class Rts_Pmadmin_Block_Adminhtml_Pricematrix_Edit extends Mage_Adminhtml_Block_Widget_Form_Container{
    public function __construct()
  {
    parent::__construct();
    //vwe assign the same blockGroup as the Grid Container
    $this->_blockGroup = 'pmadmin';
    //and the same controller
    $this->_controller = 'Pmadmin_Pmadmin';
    //define the label for the save and delete button
    $this->_updateButton('save', 'label','save reference');
    $this->_updateButton('delete', 'label', 'delete reference');
}
   /* Here, we're looking if we have transmitted a form object,
      to update the good text in the header of the page (edit or add) */
public function getHeaderText()
{
    if( Mage::registry('matrix_data')&&Mage::registry('matrix_data')->getId())
     {
          return 'Edit reference '.$this->htmlEscape(
          Mage::registry('matrix_data')->getTitle()).'<br />';
     }
     else
     {
         return 'Add a contact';
     }
}
 }

应用程序/代码/本地/ RTS / Pmadmin /砌块/ Adminhtml / Pricematrix /编辑/ Tabs.php

class Rts_Pmadmin_Block_Adminhtml_Pricematrix_Edit_Tabs extends Mage_Adminhtml_Block_Widget_Tabs {

public function __construct() {
    parent::__construct();
    $this->setId('pricematrix_grid');
    $this->setDestElementId('edit_form'); // this should be same as the form id define above
    $this->setTitle(Mage::helper('pricematrix')->__('Matrix Information'));
}

protected function _beforeToHtml() {
    $this->addTab('form_section', array(
        'label' => Mage::helper('pricematrix')->__('Matrix Information'),
        'title' => Mage::helper('pricematrix')->__('Matrix Information'),
        'content' => $this->getLayout()->createBlock('pmadmin/adminhtml_pricematrix_edit_tab_form')->toHtml(),
    ));

    $this->addTab('form_section1', array(
        'label' => Mage::helper('pricematrix')->__('Content'),
        'title' => Mage::helper('pricematrix')->__('Content'),
        'content' => $this->getLayout()->createBlock('pmadmin/adminhtml_pricematrix_edit_tab_content')->toHtml(),
    ));

    return parent::_beforeToHtml();
}
}

应用程序/代码/本地/ RTS / Pmadmin /砌块/ Adminhtml / Pricematrix /编辑/标签/ form.php的

class Rts_Pmadmin_Block_Adminhtml_Pricematrix_Edit_Tab_Form extends Mage_Adminhtml_Block_Widget_Form {

protected function _prepareForm() {

    if (Mage::registry('matrix_data')) {
        $data = Mage::registry('matrix_data')->getData();
    } else {
        $data = array();
    }

    $form = new Varien_Data_Form();
    $this->setForm($form);
    $fieldset = $form->addFieldset('news_news', array('legend' => Mage::helper('pricematrix')->__('news information')));

    $fieldset->addField('title', 'text', array(
        'label' => Mage::helper('pricematrix')->__('Matrix Title'),
        'class' => 'required-entry',
        'required' => true,
        'name' => 'title',
    ));

    $fieldset->addField('tag', 'text', array(
        'label' => Mage::helper('pricematrix')->__('Tag'),
        'class' => 'required-entry',
        'required' => true,
        'name' => 'tag',
    ));

    $form->setValues($data);

    return parent::_prepareForm();
}
}

应用程序/代码/本地/ RTS / Pmadmin /砌块/ Adminhtml / Pricematrix /编辑/标签/ Content.php

class Rts_Pmadmin_Block_Adminhtml_Pricematrix_Edit_Tab_Content extends Mage_Adminhtml_Block_Widget_Form {

protected function _prepareForm() {

    if (Mage::registry('matrix_data')) {
        $data = Mage::registry('matrix_data')->getData();
    } else {
        $data = array();
    }

    $form = new Varien_Data_Form();
    $this->setForm($form);
    $fieldset = $form->addFieldset('news_news', array('legend' => Mage::helper('news')->__('More information')));

    /*
     * Editing the form field in wysiwyg editor.
     */

    $wysiwygConfig = Mage::getSingleton('cms/wysiwyg_config')->getConfig();
    $wysiwygConfig->addData(array('add_variables' => false,
        'add_widgets' => true,
        'add_images' => true,
        'directives_url' => Mage::getSingleton('adminhtml/url')->getUrl('adminhtml/cms_wysiwyg/directive'),
        'directives_url_quoted' => preg_quote(Mage::getSingleton('adminhtml/url')->getUrl('adminhtml/cms_wysiwyg/directive')),
        'widget_window_url' => Mage::getSingleton('adminhtml/url')->getUrl('adminhtml/widget/index'),
        'files_browser_window_url' => Mage::getSingleton('adminhtml/url')->getUrl('adminhtml/cms_wysiwyg_images/index'),
        'files_browser_window_width' => (int) Mage::getConfig()->getNode('adminhtml/cms/browser/window_width'),
        'files_browser_window_height' => (int) Mage::getConfig()->getNode('adminhtml/cms/browser/window_height')
    ));


    $fieldset->addField('description', 'editor', array(
        'name' => 'description',
        'label' => Mage::helper('news')->__('Description'),
        'title' => Mage::helper('news')->__('Description'),
        'style' => 'width:800px; height:500px;',
        'config' => $wysiwygConfig,
        'required' => false,
        'wysiwyg' => true
    ));

    $form->setValues($data);
}
}

我不知道问题是什么。

请帮忙!

由于

更新:屏幕截图

页面显示标题,更新消息和页脚,但没有网格。

screen shot

2 个答案:

答案 0 :(得分:1)

在您的config.xml中,您尚未在<blocks> </blocks>标记内定义<global> </global>标记。

有关详细信息,请参阅以下教程:

http://navaneeth.me/creating-magento-extension-with-custom-database-table/#.VklzCHwrLOR

答案 1 :(得分:0)

管理员路由器的问题在你调用它时将它改为pmadmin。

<admin>
    <routers>
        <adminhtml>
            <args>
                <modules>
                    <Rts_Pmadmin after="Mage_Adminhtml">pmadmin</Rts_Pmadmin>
                </modules>
            </args>
        </adminhtml>
    </routers>
</admin>