如何使用观察者向订购网格添加新列?

Nic*_*ick 4 mysql collections magento adminhtml

你可以帮帮我吗?我正在尝试使用观察者在admin中向订单网格添加新列.有我的config.xml

    <adminhtml>
        <events>
            <adminhtml_block_html_before>
                <observers>
                    <order_grid>
                        <type>model</type>
                        <class>Order_Grid_Model_Observer</class>
                        <method>addItemsColumn</method>
                    </order_grid>
                </observers>
            </adminhtml_block_html_before>
        </events>
    </adminhtml>
Run Code Online (Sandbox Code Playgroud)

有我的观察者代码:

class Order_Grid_Model_Observer
{
    public function addItemsColumn($observer)
    {
        $_block = $observer->getBlock();
        $_type = $_block->getType();
        if ($_type == 'adminhtml/sales_order_grid') {
            $_block->addColumn('total_item_count', array(
                'header'=> Mage::helper('sales')->__('Items'),
                'width' => '80px',
                'type'  => 'text',
                'index' => 'total_item_count',
                'sortable' => false,
                'filter' => false
            ));
            $_block->getColumn('real_order_id')
                ->setData('filter_index', 'main_table.increment_id');

            $collection = $_block->getCollection();
            $collection->clear();
            $collection->getSelect()
                ->joinLeft(array('o' => 'sales_flat_order'), 'o.entity_id = main_table.entity_id', array('total_item_count'));
            $_block->setCollection($collection);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我几乎做到了,但是当我尝试按某个字段对网格进行排序时,我得到错误"字段列表中的列'increment_id'是不明确的".这很奇怪,因为我已经为'increment_id'字段更新了'filter_index'.为什么收集这个块的任何想法没有更新?谢谢.

Fab*_*idt 5

"我们"(另一组)在Magento Hackathon上实现了扩展,允许轻松添加新列,重新排列它们并从网格中删除列.此外,您可以将表连接到集合以添加列.

https://github.com/magento-hackathon/GridControl