Magento网格问题 [英] Magento Grid issue

查看:43
本文介绍了Magento网格问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人能为我指出如何在Magento网格中保存可编辑列的正确方向吗?

Can anyone point me in the right direction on how to save the editable columns within the Magento Grid?

我有一个名为'sort_order'的列,其中具有'editable'=> true,它添加了一个要编辑的字段,但是我如何使其将值保存到行中?

I have a column called 'sort_order' of which has 'editable' => true , it adds a field to edit, but how do I make it save the value to the row?

预先感谢您的帮助.

这是我的grid.php代码,

here is my grid.php code,

class ***_Imagegallery_Block_Manage_Imagegallery_Grid extends Mage_Adminhtml_Block_Widget_Grid
{
    public function __construct()
    {
        parent::__construct();
        $this->setId('imagegalleryGrid');
        $this->setDefaultSort('sort_order');
        $this->setDefaultDir('ASC');
        $this->setSaveParametersInSession(true);
    }

    protected function _getStore()
    {
        $storeId = (int) $this->getRequest()->getParam('store', 0);
        return Mage::app()->getStore($storeId);
    }

    protected function _prepareCollection()
    {

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

        $store = $this->_getStore();
        if ($store->getId()) {
            $collection->addStoreFilter($store);
        }

        $filter = $this->getParam('filter');


        $filter_data = Mage::helper('adminhtml')->prepareFilterString($filter);

        if(!isset($filter_data['status']))
        {
            $collection->addFieldToFilter('status', array('eq' => 1));
        }

        $this->setCollection($collection);

        return parent::_prepareCollection();
    }

    protected function _prepareColumns()
    {

        $this->addColumn('sort_order', array(
          'header'    => Mage::helper('imagegallery')->__('Sort'),
          'align'     =>'right',
          'width'     => '50px',
          'index'     => 'sort_order',
        'type'      => 'number',
    'width'     =>  '1',
    'sortable'  => true,
    'editable'  => true
        ));

        $this->addColumn('post_id', array(
          'header'    => Mage::helper('imagegallery')->__('ID'),
          'align'     =>'right',
          'width'     => '50px',
          'index'     => 'post_id',
        ));

        $this->addColumn('nfile', array(
            'header'    => Mage::helper('cms')->__('Image File'),
            'align'     => 'left',
            'index'     => 'nfile',
            'type'      => 'image',
            'width'     => '100',
        ));



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

        /*$this->addColumn('identifier', array(
          'header'    => Mage::helper('imagegallery')->__('Identifier'),
          'align'     => 'left',
          'index'     => 'identifier',
        ));

        $this->addColumn('user', array(
            'header'    => Mage::helper('imagegallery')->__('Poster'),
            'width'     => '150px',
            'index'     => 'user',
        ));*/


        $this->addColumn('created_time', array(
            'header'    => Mage::helper('imagegallery')->__('Created'),
            'align'     => 'left',
            'width'     => '120px',
            'type'      => 'date',
            'default'   => '--',
            'index'     => 'created_time',
        ));

        $this->addColumn('update_time', array(
            'header'    => Mage::helper('imagegallery')->__('Updated'),
            'align'     => 'left',
            'width'     => '120px',
            'type'      => 'date',
            'default'   => '--',
            'index'     => 'update_time',
        ));

        $this->addColumn('status', array(
          'header'    => Mage::helper('imagegallery')->__('Status'),
          'align'     => 'left',
          'width'     => '80px',
          'index'     => 'status',
          'type'      => 'options',
          'options'   => array(
              1 => Mage::helper('imagegallery')->__('Enabled'),
              2 => Mage::helper('imagegallery')->__('Disabled'),
              3 => Mage::helper('imagegallery')->__('Hidden'),
          ),
        ));

        $this->addColumn('action',
            array(
            'header'    =>  Mage::helper('imagegallery')->__('Action'),
            'width'     => '100',
            'type'      => 'action',
            'getter'    => 'getId',
            'actions'   => array(
                    array(
                            'caption'   => Mage::helper('imagegallery')->__('Edit'),
                            'url'       => array('base'=> '*/*/edit'),
                            'field'     => 'id'
                    )
            ),
            'filter'    => false,
            'sortable'  => false,
            'index'     => 'stores',
            'is_system' => true,
    ));

        return parent::_prepareColumns();
    }

    protected function _prepareMassaction()
    {
        $this->setMassactionIdField('post_id');
        $this->getMassactionBlock()->setFormFieldName('imagegallery');

        $this->getMassactionBlock()->addItem('delete', array(
             'label'    => Mage::helper('imagegallery')->__('Delete'),
             'url'      => $this->getUrl('*/*/massDelete'),
             'confirm'  => Mage::helper('imagegallery')->__('Are you sure?')
        ));

        $statuses = Mage::getSingleton('imagegallery/status')->getOptionArray();

        array_unshift($statuses, array('label'=>'', 'value'=>''));
        $this->getMassactionBlock()->addItem('status', array(
             'label'=> Mage::helper('imagegallery')->__('Change status'),
             'url'  => $this->getUrl('*/*/massStatus', array('_current'=>true)),
             'additional' => array(
                    'visibility' => array(
                         'name' => 'status',
                         'type' => 'select',
                         'class' => 'required-entry',
                         'label' => Mage::helper('imagegallery')->__('Status'),
                         'values' => $statuses
                     )
             )
        ));
        return $this;
    }

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

}

推荐答案

这实际上非常复杂;理想情况下,您的网格需要成为实例化表单的容器的一部分,并通过选项卡"部分进行调用.保存按钮通常是自定义phtml模板的一部分,该模板与通过Adminhtml中的控制器操作调用的布局块一起调用.

This is actually quite complex; ideally your grid needs to be part of a Container which instantiates a Form, and is called via a Tab section. The save buttons are usually some part of a custom phtml template that is invoked along with the layout block that is called via the controller action in your Adminhtml.

这个form.phtml可能非常简单,但通常包含一些可格式化保存url(请参阅下文)并收集提交数据的javascript.

This form.phtml can be very simple but usually contains some javascript that formats the save url (see below) and gathers the submission data.

Form.php

构造函数:

public function __construct()
{
    parent::__construct();
    $this->setTemplate('your/adminhtml/edit/form.phtml');
}

和_prepareLayout方法:

And the _prepareLayout method:

protected function _prepareLayout()
{
    // Save button
        $this->setChild('save_button',
            $this->getLayout()->createBlock('adminhtml/widget_button')
                ->setData(array(
                    'label'     => Mage::helper('catalog')->__('Save Category'),
                    'onclick'   => "formSubmit('" . $this->getSaveUrl() . "', true)",
                    'class' => 'save'
                ))
        );

您的保存按钮是将提交定向到正确的控制器的按钮.在上面的示例中,它们使用getSaveUrl作为这些块类的方法.您也可以对此进行硬编码或像在其他地方一样使用$this->getUrl('*/*/save').单击此保存按钮将序列化表格:

Your save button is what directs the submission to the correct controller. In this example above they use getSaveUrl as a method of these block classes. You can also hard code this or use $this->getUrl('*/*/save') as you probably do elsewhere. Clicking this save button will serialize the form:

form.phtml

默认文件仅具有:

<div class="entry-edit">
    <?php echo $this->getFormHtml();?>
</div>
<?php echo $this->getChildHtml('form_after');?>

包含可编辑的帖子数据的扩展form.phtml文件将执行以下操作:

An extended form.phtml file that includes editible post data does something like this:

<div class="entry-edit">
    <?php echo $this->getFormHtml();?>
</div>
<?php echo $this->getChildHtml('form_after');?>
<script>
    function formSubmit(url){
        var elements = $('[yourformid]').select('input');
        var serialized = Form.serializeElements(elements, true);
        window.location(url + '?' + serilized.[element_name].join(','));
    }
</script>

我还没有测试上面代码的每个部分,但是理论是扎实的,这就是我在这些情况下要做的事情.

I haven't tested every part of the above code, but the theory is solid, and it's what I do in these situations.

干杯.

这篇关于Magento网格问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆