[英]Change Manage Customer's Grid
我创建了一个自定义模块,该模块在管理配置面板中显示一个标签和一个部分来管理客户属性。
我已经为所有客户属性都加载了一个复选框。
这是我的代码,用于将所有客户属性显示为复选框。 我希望从此处选择的复选框值被添加为“管理客户网格”中的一列。 Model / Attributes.php
$attributes = Mage::getModel('customer/entity_address_attribute_collection');
$result = array();
foreach ($attributes as $attribute)
{
if (($label = $attribute->getFrontendLabel()))
$result[$attribute->getId()] = $label;
}
$attributes1 = Mage::getModel('customer/entity_attribute_collection');
$result1 = array();
foreach ($attributes1 as $attribute1)
{
if (($label1 = $attribute1->getFrontendLabel()))
$result1[$attribute1->getId()] = $label1;
}
$final = array_merge($result, $result1);
return $final;
现在,基于对这些复选框的选择,我想在“管理客户”网格中添加一个额外的列。
我尝试检索选定复选框的值,但是我只是获得数组索引号。
Mage::getStoreConfig('sectionname/groupname/fieldname');
有人可以告诉我如何获取选定的复选框值并根据该复选框表示的选择添加一列吗?
提前致谢。
我将在您的模块中设置config.xml,以使用自己的块(继承自Mage_Adminhtml_Block_Customer_Grid
)覆盖Block Mage_Adminhtml_Block_Customer_Grid
并在您自己的块中创建一个函数
protected function _prepareColumns()
{
$this->addColumn('mycolumn', array(
'header' => Mage::helper('customer')->__('My Column'),
'index' => 'key',
));
return parent::_prepareColumns();
}
在不了解更多数据的情况下,很难提供更好的建议,但这足以让您入门。
当使用array_merge
您正在破坏正确的索引,这些索引应该是属性ID。 最好给变量赋予有意义的名称。
$result = array();
$addressAttributes = Mage::getModel('customer/entity_address_attribute_collection');
foreach ($addressAttributes as $addressAttribute)
{
if (($addrlabel = $addressAttribute->getFrontendLabel()))
$result[$addressAttribute->getId()] = $addrlabel;
}
$customerAttributes = Mage::getModel('customer/entity_attribute_collection');
foreach ($customerAttributes as $customerAttribute)
{
if (($custlabel = $customerAttribute->getFrontendLabel()))
$result[$customerAttribute->getId()] = $custlabel;
}
return $result;
我想下一步是删除网格的父级将添加的列,这些列存储在网格的受保护的_columns
属性中。 并非所有列都将被删除,例如massaction列。 然后重新添加您选择的列。
protected function _prepareColumns()
{
parent::_prepareColumns();
// remove the excess columns here
$attributeIds = Mage::getStoreConfig('sectionname/groupname/fieldname');
$attributes = Mage::getModel('eav/entity_attribute')->getCollection()
->addFieldToFilter('attribute_id', array('in' => $attributeIds));
foreach ($attributes as $attribute)
{
$options = array();
if ($attribute->getFrontendInput() == 'select') {
foreach ($attribute->getSource()->getAllOptions() as $value) {
$options[$value['value']] = $value['label'];
}
}
$this->addColumn($attribute->getCode(), array(
'index' => $attribute->getCode(),
'header' => $attribute->getFrontendLabel(),
'type' => $attribute->getFrontendInput(),
'options' => $options
));
}
return $this;
}
这种方法可能会丢失有用的格式,例如列宽等。因此,一种更复杂的方法是找出已经存在的列并将其保留,然后仅删除那些尚未选择的列。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.