0

在管理客户的管理区域中。

我添加了一个新的自定义列,但它没有过滤,也不能按 asc / desc 排序

这是我的代码

protected function _prepareCollection()
{
$collection = Mage::getResourceModel(’customer/customer_collection’)
->addNameToSelect()
->addAttributeToSelect(’email’)
->addAttributeToSelect(’created_at’)
->addAttributeToSelect(’group_id’)
->joinAttribute(’billing_postcode’, ‘customer_address/postcode’, ‘default_billing’, null, ‘left’)
->joinAttribute(’billing_city’, ‘customer_address/city’, ‘default_billing’, null, ‘left’)
->joinAttribute(’billing_telephone’, ‘customer_address/telephone’, ‘default_billing’, null, ‘left’)
->joinAttribute(’billing_region’, ‘customer_address/region’, ‘default_billing’, null, ‘left’)
->joinAttribute(’billing_country_id’, ‘customer_address/country_id’, ‘default_billing’, null, ‘left’);


$collection->getSelect()->columns(array(’CustomerStatus’ => new Zend_Db_Expr ("(SELECT
CustomerStatusFROM users WHERE MagentoID =e.entity_id)")));

$this->setCollection($collection);

return parent::_prepareCollection();
}

我尝试了很多不同的方法并添加了 _addColumnFilterToCollection 但似乎无法让它工作......</p>

    protected function _addColumnFilterToCollection($column)
        {


            if ($column->getId() == 'CustomerStatus' && $column->getFilter()->getValue()) {
            $val        = $column->getFilter()->getValue();


            $this->getCollection()->addFieldToFilter('CustomerStatus', array('like' => $val));

// $this->getCollection()->getSelect()->where('CustomerStatus= ?', $val);

        }

            return $this;



        }

然后这就是我在 _preparecolumns 函数中添加列的方式

$this->addColumn('CustomerStatus', array(
         'header'    => Mage::helper('customer')->__('C Status'),
         'width'     => '100px',
        'index'     => 'CustomerStatus',
        'filter_index' => 'CustomerStatus'
    ));

有任何想法吗?

4

1 回答 1

0

CustomerStatus您可以使用frame_callback填充列并filter_condition_fallback搜索 列,而不是尝试使列直接返回。

我尚未针对您的确切条件对此进行测试,但过去在将数据拉入这样的管理网格时,我已经成功地使用了相同的技术。

protected function _prepareCollection()
{

    $collection = Mage::getResourceModel('customer/customer_collection')
        ->addNameToSelect()
        ->addAttributeToSelect('email')
        ->addAttributeToSelect('created_at')
        ->addAttributeToSelect('group_id')
        ->joinAttribute('billing_postcode', 'customer_address/postcode', 'default_billing', null, 'left')
        ->joinAttribute('billing_city', 'customer_address/city', 'default_billing', null,    'left')
        ->joinAttribute('billing_telephone', 'customer_address/telephone', 'default_billing', null, 'left')
        ->joinAttribute('billing_region', 'customer_address/region', 'default_billing', null, 'left')
        ->joinAttribute('billing_country_id', 'customer_address/country_id', 'default_billing', null, 'left');

    $collection->getSelect()->columns(array('CustomerStatus' => new Zend_Db_Expr ("(SELECT CustomerStatus FROM users WHERE MagentoID = e.entity_id)")));

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

...

并像这样更改您的 addColumn:

$this->addColumn('CustomerStatus', array(
     'header'    => Mage::helper('customer')->__('C Status'),
     'width'     => '100px',
     'index'     => 'entity_id',
     'frame_callback' => array($this, 'callback_customerstatus'), // This calls the "callback_customerstatus" function below
     'filter_condition_callback' => array($this, 'filter_customerstatus'), // This calls the "filter_customerstatus" function that will modify your $collection to make searching possible
));

...

添加这个由frame_callback

public function callback_customerstatus($value, $row, $column, $isExport) {
    $entity_id = $value; // $value is passed from the Grid
    $sql = "SELECT `CustomerStatus` FROM `users` WHERE MagentoID=".$entity_id;
    $resource = Mage::getSingleton('core/resource');
    $readConnection = $resource->getConnection('core_read');
    $customer_status = $readConnection->fetchOne($sql); // Return one row with the selected value from "$sql" 

    return $customer_status;
}

...

添加一个名为 filter_customerstatus() 的新函数:

public function filter_customerstatus($collection, $column) {
    if (!$value = $column->getFilter()->getValue()) {
        return $this;
    }

    $this->getCollection()->getSelect()->where(
        "CustomerStatus like ?"
        , "%$value%");

    return $this;
}

...

请注意新的调用filter_condition_callback添加了where创建搜索功能所需的条件。

于 2013-04-04T12:42:18.627 回答