空白问题magento admin html网格渲染器

时间:2011-06-07 11:53:33

标签: php magento adminhtml

我重写了app/code/core/Mage/Adminhtml/Block/Sales/Order/Grid.phpapp/code/local/Mage/Adminhtml/Block/Sales/Order/Grid.php &安培;创建了一个渲染器,用于在网格中显示客户的电子邮件列。

这是我的渲染器文件:

class Mage_Adminhtml_Block_Renderer_Customer extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract 
{

public function render(Varien_Object $row)
{   
    $model = Mage::getModel('customer/customer')->load($row->getCustomerId());

    return  $model->getEmail();

 }

}

&安培;这是我的网格更改(我刚添加了一个列,并且我打算使其可搜索)

$this->addColumn('billing_name', array(
        'header' => Mage::helper('sales')->__('Bill to Name'),
        'index' => 'billing_name',
    ));
       // this is new col.
    $this->addColumn('customer_email', array(
        'header' => Mage::helper('sales')->__('Customer Email'),
        'renderer' => 'adminhtml/renderer_customer',

    ));

我得到了我想要的东西。但这个col /有很多空白领先&尾随 由于这个我认为这个col。不可搜索。 可以任何人建议可以做些什么来删除这些空格

非常感谢提前

修改 几天之后,我发现这些白色空间在网格中很常见。它与可搜索选项无关。 任何人都可以建议如何使用渲染器在可搜索中创建自定义列已添加到网格中??? 感谢

2编辑 伙计们根据 clockworkgeek 我定制了 覆盖网格的_prepareCollection()方法如下

 protected function _prepareCollection()
  { 
    // 'sales/order_collection' is changed from 'sales/order_grid_collection'
    $collection = Mage::getResourceModel('sales/order_collection');  

    $collection->addAttributeToSelect('*')
    ->joinAttribute('billing_firstname', 'order_address/firstname', 'billing_address_id', null, 'left')
    ->joinAttribute('billing_lastname', 'order_address/lastname', 'billing_address_id', null, 'left')
    ->joinAttribute('shipping_firstname', 'order_address/firstname', 'shipping_address_id', null, 'left')
    ->joinAttribute('shipping_lastname', 'order_address/lastname', 'shipping_address_id', null, 'left')
    ->joinAttribute('billing_fax', 'order_address/fax', 'billing_address_id', null, 'left')
    ->joinAttribute('billing_telephone', 'order_address/telephone', 'billing_address_id', null, '')

    ->addExpressionAttributeToSelect('billing_name',
    'CONCAT({{billing_firstname}}, " ", {{billing_lastname}})',
    array('billing_firstname', 'billing_lastname'))


    ->addExpressionAttributeToSelect('shipping_name',
    'CONCAT({{shipping_firstname}}, " ", {{shipping_lastname}})',
    array('shipping_firstname', 'shipping_lastname'));

    $this->setCollection($collection);
    return parent::_prepareCollection();
}

我还调查过,对于网格Magento从 sales_flat_order_grid 表中获取数据而不是从 sales_flat_order 获取数据,这就是根据报告未知列错误的原因clockworkgeek 第一个解决方案

当前实施的问题是Magento报告错误致命错误:调用未定义的方法Mage_Sales_Model_Mysql4_Order_Collection :: addExpressionAttributeToSelect()

由于Mage_Sales_Model_Mysql4_Order_Collection没有addExpressionAttributeToSelect方法,而是具有 addExpressionFieldToSelect 方法 现在我需要帮助为addExpressionAttributeToSelect方法编写正确的语法。仅更改方法名称对我没有帮助。我也推荐了文档

2 个答案:

答案 0 :(得分:0)

'index' => 'email'添加到Grid.php中的addColumn(),然后尝试以下操作:

$emailaddress = trim($row->getData($this->getColumn()->getIndex()));
return '<a href="mailto:'.$emailaddress.'">'.$emailaddress.'</a>';

通过这种方式,您可以删除空白,并为管理员用户提供可点击的链接:)

答案 1 :(得分:0)

在回答问题的第二部分时,我可以提供这个小技巧。

Adminhtml网格列可以采用额外的filter_condition_callback参数,该参数采用标准PHP callback type。在您的情况下,您可以像这样修改网格:

protected function _prepareColumns() {
    // ...
    $this->addColumn('customer_email', array(
        'header' => Mage::helper('sales')->__('Customer Email'),
        'renderer' => 'adminhtml/renderer_customer',
        'filter_condition_callback' => array($this, 'addCustomerEmailFilter'),
    ));
}

public function addCustomerEmailFilter(Mage_Eav_Model_Entity_Collection_Abstract $collection, Mage_Adminhtml_Block_Widget_Grid_Column $column) {
    $collection->addAttributeToFilter('customer_email', $column->getFilter()->getValue());
}

但是所有这些仍然感觉有点混乱,特别是如果属性不是第一类列。对于那些不寻常的情况,您可以将输出处理和搜索结合在集合类中......

protected function _initSelect() {
    parent::_initSelect();

    // email is existing column, customer_email is generated column
    $this->addExpressionAttributeToSelect(
        'customer_email',
        'TRIM({{email}})',
        array('email')
    );

    return $this;
}

addExpressionAttributeToSelect()方法将SQL表达式临时存储为映射字段,以便在网格尝试搜索customer_email时,它会被表达式替换。