开发者

Display multi-option customer attribute within customer management admin grid

Okay so with Customer Attributes I have a multi-option selection that I have added to the Manage Customers Grid.

    $prodCode = Mage::getSingleton('eav/config')->getAttribute('customer','prod_codes');
    $prodCodeOptions = $prodCode->getSource()->getAllOptions(false);
    $prodOptions = array();

    foreach($prodCodeOptions as $k)
        $prodOptions[$k['value']] = $k['label'];

    $this->addColumn('prod_codes', array(
        'header'    =>  Mage::helper('customer')->__('Product Code'),
        'width'     =>  '100',
        'index'     =>  'prod_codes',
        'type'      =>  'options',
        'options'   =>  $prodOptions,
        'filter_condition_callback'
                    => array($this, '_filterProdOptionsCondition'),
    ));

I do have my attribute added to the collection at the top of my Grid.php:

->addAttributeToSelect('prod_codes')

Here is my _filterProdOptionsCondition method:

protected function _filterProdOpt开发者_Python百科ionsCondition($collection, $column) {
    if(!$value = $column->getFilter()->getValue()) {
        return;
    }
    $this->getCollection()->addFieldToFilter('prod_codes', array('finset' => $value));
    #print($collection->getSelectSql());
}

Now this work fine and dandy if I only have ONE of the options selected, once I apply more than one option to the customers attribute I will get a blank results within the admin grid, however it IS still searchable.

A close look with the print($collection->getSelectSql()); uncommented I see that the attribute ID values are being returned in an comma delimited list.

Now onto my question with that background laid out, is there a method or "Magento" way to display these multi-options within the admin grid I'm just unaware of? Or do I need to simply get the comma values exploded and call for a new collection to build out the display values? Any help appreciated!


Appears I had to extend the Column renderer to anticipate comma values and simply render them, I'm amazed this isn't built in since the functionality exists to create the multioptions attributes but no grid display option for it.

app/code/local/Mage/Adminhtml/Block/Widget/Grid/Column/Renderer/Options.php

public function render(Varien_Object $row)
{
    $options = $this->getColumn()->getOptions();
    $showMissingOptionValues = (bool)$this->getColumn()->getShowMissingOptionValues();
    if (!empty($options) && is_array($options)) {
        $value = $row->getData($this->getColumn()->getIndex());
        if (is_array($value)) {
            $res = array();
            foreach ($value as $item) {
                if (isset($options[$item])) {
                    $res[] = $options[$item];
                }
                elseif ($showMissingOptionValues) {
                    $res[] = $item;
                }
            }
            return implode(', ', $res);
        }
        elseif (isset($options[$value])) {
            return $options[$value];
        } elseif (is_string($value)) { // <--- MY CHANGES HERE
            $values = explode(',', $value);
            $returnOptions = "";
            foreach($values as $k=>$v) {
                $returnOptions .= $options[$v]. ", ";
            }
            return substr($returnOptions, 0, -2);
        }
        return '';
    }
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜