在管理客户的管理区域。
我添加了一个新的自定义列,但它没有过滤,也不能对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,但似乎无法让它工作......
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'
));
有什么想法吗?
答案 0 :(得分: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
条件。