开发者

whitespace problem magento admin html grid renderer

开发者 https://www.devze.com 2023-03-11 04:22 出处:网络
I have rewritten the app/code/core/Mage/Adminhtml/Block/Sales/Order/Grid.php with app/code/local/Mage/Adminhtml/Block/Sales/Order/Grid.php

I have rewritten the app/code/core/Mage/Adminhtml/Block/Sales/Order/Grid.php with app/code/local/Mage/Adminhtml/Block/Sales/Order/Grid.php & have created a renderer to display customer's email column in grid.

Here is my renderer file:

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();

 }

}

& here is my Grid changes (I just added a column, & I intend to make it search-able)

$this->addColumn('billing_name', array(
        'header' => Mage::helper('sales')->__('Bill to Name'),
        'index' => 'billing_name',
    ));
       // this is n开发者_如何转开发ew col.
    $this->addColumn('customer_email', array(
        'header' => Mage::helper('sales')->__('Customer Email'),
        'renderer' => 'adminhtml/renderer_customer',

    ));

I am getting what I want. But this col/ has a lot of whitespace both leading & trailing due to this I think this col. is not search-able. Can Anybody suggest what can be done in order to remove these white spaces

Many thanks in advance

EDIT After few days I have figured out that these white spaces are common in the grid & it has nothing to do with the search-able option. Can anybody suggest that how to make a custom column in search-able that has been added to a grid by using a renderer ??? Thanks

2 EDIT Guys According to the clockworkgeek I have customized my _prepareCollection() method of the overwritten grid as follows

 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();
}

I also have investigated that for Grid Magento obtains data from sales_flat_order_grid table not from sales_flat_order this is the reason it was reporting error of unknow column as per the clockworkgeek first solution

THe issue with current implementation is Magento reports an error Fatal error: Call to undefined method Mage_Sales_Model_Mysql4_Order_Collection::addExpressionAttributeToSelect()

as Mage_Sales_Model_Mysql4_Order_Collection does not have addExpressionAttributeToSelect method instead it has addExpressionFieldToSelect method Now I need help to write a proper syntax for addExpressionAttributeToSelect method. Changing the method name only is not helping me. I also have referred the docs


Add 'index' => 'email' to your addColumn() in the Grid.php and then try something like this:

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

That way you strip the whitespace and also provide a clickable link for your admin users :)


In response to the second part of your question may I offer this little trick.

Adminhtml grid columns can take an extra filter_condition_callback parameter which takes a standard PHP callback type. In your case you might modify the grid like this:

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());
}

But all that still feels a bit messy, especially if the attribute is not a first class column. For those unusual cases you can combine the output processing and searching in the collection class...

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

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

    return $this;
}

The addExpressionAttributeToSelect() method temporarily stores the SQL expression as a mapped field so that when a grid tries to search for customer_email it gets substituted by the expression instead.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号