Magento - Add button [+ Add Field] that adds/shows a new text field in system.xml -
i have custom module optional text fields (shown via standard text field option in system.xml). i'd show 1 mandatory text field , have button says [+ add field]. when button pressed, text field added. i'd up to 10 total text fields (max). can me accomplish or point me nice tutorial on how this?
edit 7/10/12 @ 11:30am
i've been working on , far can text fields show up. have few issues...
- when pressing "save config" button, none of values entered in these dynamically created textfields save.
- is there way limit how many text fields save?
- i'm unsure of correct way retrieve text field(s) data. due fact cannot save values...(i add update after #1 fixed if need this...)
my system.xml file has in (directly related this)..
<labels translate="label"> <label>this label</label> <comment>this comment.</comment> <tooltip><![cdata[this tooltip]]></tooltip> <frontend_type>text</frontend_type> <frontend_model>company_namespace/adminhtml_textfields</frontend_model> <backend_model>adminhtml/system_config_backend_serialized</backend_model> <sort_order>50</sort_order> <show_in_default>1</show_in_default> <show_in_website>1</show_in_website> <show_in_store>0</show_in_store> </labels>
my custom textfields.php (frontend_model) file contents:
<?php class company_namespace_block_adminhtml_textfields extends mage_adminhtml_block_system_config_form_field { protected $_addrowbuttonhtml = array(); protected $_removerowbuttonhtml = array(); protected function _getelementhtml(varien_data_form_element_abstract $element) { $this->setelement($element); $html = '<div id="code_label_textfields_template" style="display:none">'; $html .= $this->_getrowtemplatehtml(); $html .= '</div>'; $html .= '<ul id="code_label_textfields_container">'; if ($this->_getvalue('method')) { foreach ($this->_getvalue('method') $i=>$f) { if ($i) { $html .= $this->_getrowtemplatehtml($i); } } } $html .= '</ul>'; $html .= $this->_getaddrowbuttonhtml('code_label_textfields_container', 'code_label_textfields_template', $this->__('button label here')); return $html; } protected function _getrowtemplatehtml() { $html = '<li>'; $html .= '<div style="margin:5px 0 10px;">'; $html .= '<input class="input-text" name="'.$this->getelement()->getname().'" value="'.$this->_getvalue('price/'.$i).'" '.$this->_getdisabled().'/> '; $html .= $this->_getremoverowbuttonhtml(); $html .= '</div>'; $html .= '</li>'; return $html; } protected function _getdisabled() { return $this->getelement()->getdisabled() ? ' disabled' : ''; } protected function _getvalue($key) { return $this->getelement()->getdata('value/'.$key); } protected function _getselected($key, $value) { return $this->getelement()->getdata('value/'.$key)==$value ? 'selected="selected"' : ''; } protected function _getaddrowbuttonhtml($container, $template, $title='add') { if (!isset($this->_addrowbuttonhtml[$container])) { $this->_addrowbuttonhtml[$container] = $this->getlayout()->createblock('adminhtml/widget_button') ->settype('button') ->setclass('add '.$this->_getdisabled()) ->setlabel($this->__($title)) //$this->__('add') ->setonclick("element.insert($('".$container."'), {bottom: $('".$template."').innerhtml})") ->setdisabled($this->_getdisabled()) ->tohtml(); } return $this->_addrowbuttonhtml[$container]; } protected function _getremoverowbuttonhtml($selector='li', $title='remove') { if (!$this->_removerowbuttonhtml) { $this->_removerowbuttonhtml = $this->getlayout()->createblock('adminhtml/widget_button') ->settype('button') ->setclass('delete v-middle '.$this->_getdisabled()) ->setlabel($this->__($title)) //$this->__('remove') ->setonclick("element.remove($(this).up('".$selector."'))") ->setdisabled($this->_getdisabled()) ->tohtml(); } return $this->_removerowbuttonhtml; } }
what missing, saving values???
refer system.xml
, mage_googlecheckout_block_adminhtml_shipping_merchant
block example. it's little convoluted, works. can see in merchant calculated settings in google checkout.
Comments
Post a Comment