magento :如何在后台的form中添加内容可变的select filed

来源:互联网 发布:电脑怎么升级windows 编辑:程序博客网 时间:2024/05/19 12:29

如下图:在product form中添加了一个attribute “spot_spotter”,这个attribute可以选择某个已注册的用户。



注意了,由于这个attribute 的内容是动态变化的,所以这个product attribute 的type是select,但却不能通过常规的方法指定option值。


最后研究出以下的解决方案:

(1)在setup文件中 给attribute添加新的属性 “frontend_input_renderer“, 指定render的block


$eav = new Mage_Eav_Model_Entity_Setup('sales_setup');$eav->updateAttribute('catalog_product', 'spot_spotter', 'frontend_input_renderer', 'spot/adminhtml_catalog_spot_htmlselect');


(2) 然后在mdoule spot中创建 block “Xxx_Spot_Block_Adminhtml_Catalog_Spot_Htmlselect” ,内容如下:


<?phpclass Xxx_Spot_Block_Adminhtml_Catalog_Spot_Htmlselect extends Varien_Data_Form_Element_Select{    public function getValues()    {        return $this->toCustomerOptionArray();    }        public function toCustomerOptionArray(){    $customerSelect=array();        //add empty value    $customerSelect[]=array('label'=>"","value"=>"" );        $collection=Mage::getModel('customer/customer')->getCollection()               ->addAttributeToSelect("firstname")               ->addAttributeToSelect("lastname");                   foreach( $collection as $key=>$customer ){    $id=(int)$customer->getId();    $name=$customer->getData("firstname")." ".$customer->getData("lastname");    $customerSelect[]=array('label'=>$name,"value"=>$id);    }        return $customerSelect;    } }

ok。搞定。

	
				
		
原创粉丝点击