1

我已经重写了app/code/core/Mage/Adminhtml/Block/Sales/Order/Grid.php &app/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();

 }

}

& 这是我的 Grid 更改(我刚刚添加了一个列,我打算让它可以搜索)

$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 EDIT Guys 根据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();
}

我还调查了对于 Grid 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 方法编写正确的语法。仅更改方法名称对我没有帮助。我也参考了文档

4

2 回答 2

0

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

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

这样你就可以去掉空格,并为你的管理员用户提供一个可点击的链接:)

于 2011-06-08T13:43:32.027 回答
0

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

Adminhtml 网格列可以采用一个额外的filter_condition_callback参数,该参数采用标准PHP 回调类型。在您的情况下,您可以像这样修改网格:

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它时,它会被表达式替换。

于 2011-06-13T12:57:37.740 回答