更改管理客户的网格

时间:2011-12-11 17:15:29

标签: php magento

我创建了一个自定义模块,该模块在管理配置面板中显示选项卡和部分以管理客户属性。

我已经加载了所有客户属性,每个属性都有一个复选框。

这是我将所有客户属性显示为复选框的代码。我希望从此处选择的复选框值作为“管理客户网格”中的列添加。 型号/ 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');

有人可以告诉我如何获取所选的复选框值并根据该复选框所代表的选择添加一列吗?

提前致谢。

2 个答案:

答案 0 :(得分:0)

我会在你的模块中,在config.xml中设置你用你自己的块(继承自Mage_Adminhtml_Block_Customer_Grid)覆盖块Mage_Adminhtml_Block_Customer_Grid并在你自己的块中创建一个函数

protected function _prepareColumns()
{
    $this->addColumn('mycolumn', array(
        'header'    => Mage::helper('customer')->__('My Column'),
        'index'     => 'key',
    ));
    return parent::_prepareColumns();
}

如果不了解您的数据,很难提供更好的建议,但这应该足以让您入门。

答案 1 :(得分:0)

当您使用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;
}

这种方式可能会失去像列宽等有用的格式,所以更复杂的方法是确定哪些列已经存在并留下它们,然后只删除那些尚未选中的列。