2012-07-06 15 views
1

我有可選的文本字段定製模塊(經由標準文本字段選項的system.xml示出) 。我想要做的是顯示1個必需的文本字段,並有一個按鈕,說明如[+添加字段]。當按下該按鈕時,將添加另一個文本字段。我想這樣做高達總共10個文本字段(最大)。有人可以幫助我完成這個任務,或者指出我有關如何做到這一點的不錯教程?的Magento - 添加按鈕[+添加領域,增加/示出的system.xml的新文本字段

編輯7/10/12 @ 11:30

我一直工作在這一點,到目前爲止,我可以得到的文本字段中顯示出來。然而,我有幾個問題...

  1. 當按下「保存配置」按鈕,這些動態創建的文本字段中輸入的值沒有實際保存。
  2. 有沒有辦法限制多少文本字段實際保存?
  3. 我不確定檢索文本字段數據的正確方法。這主要是由於這樣的事實,我無法保存的值...(我會添加後,如果我需要這方面的幫助#1固定另一個更新中...)

的System.Xml文件有這它(直接相關的)..

<labels translate="label"> 
    <label>This is some label</label> 
    <comment>This is some comment.</comment> 
    <tooltip><![CDATA[This is some 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> 

我定製Textfields.php(frontend_model)文件內容:

<?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') as $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; 
} 
} 

我錯過了什麼,特別是爲了保存值?

回答

1

舉例來說,請參考system.xmlMage_GoogleCheckout_Block_Adminhtml_Shipping_Merchant塊。這有點複雜,但它的工作原理。您可以在Google Checkout的商家計算設置中查看此信息。

+0

這是工作,但沒有辦法限制填充多少字段?我正在處理這一部分,但似乎沒有任何限制... $ html。= $ this - > _ getAddRowButtonHtml('some_container','some_template',$ this - > __('Some Label')); 。哦,你將如何從文本框中檢索數據? – Geoff 2012-07-09 23:36:47

+0

它們存儲在序列化數組中的core_config_data中。 – benmarks 2012-07-10 14:30:01

+0

再次感謝您的持續支持。我添加了更多的細節。在GoogleCheckout模塊中,在Merchant.php中,它有一個getShippingMethods()函數,似乎可以節省?你能否幫助闡述一下,因爲我一直在保存值時遇到問題(他們不保存)。我已經在上面輸入了更多細節。謝謝! – Geoff 2012-07-10 18:48:07