Prestashop 的back office tab 筆記 - part 6

此篇是對應筆記3的1.5+版本, 在1.5之前要新增一個form 真係話難唔難但係話易又真係唔算易的, 因為入面包括了很多的html code 及頁面上的設定, 但來到1.5 就真係變得容易了很多呢...

在1.5, 我地只要overwrite renderForm 這個function 更可以了,

public function renderForm(){ 
 $prod = new Product($this->last_pid);
  
 $this->fields_form = array(
  'legend' => array(
   'title' => $this->l('Fast Get Discount Setting')    
  ),
  'input' => array(
   array(
    'type' => 'text',
    'label' => $this->l('Product name').":",
    'disabled' => true,
    'name' => 'product_name'    
   ),
   array(
    'type' => 'hidden',     
    'name' => 'id_product'
   ),
   array(
    'type' => 'text',
    'label' => $this->l('No. of Sold').":",
    'name' => 'position',
    'size' => 25,
    'required' => true,
    'hint' => sprintf($this->l('Allowed number only'))
   ),
   array(
    'type' => 'text',
    'label' => $this->l('Reduction').":",
    'name' => 'reduction',
    'size' => 25,
    'required' => true,
    'hint' => sprintf($this->l('Input the Amount/Percentage you want to discount.').$this->l('Allowed number only'))     
   ),
   array(
    'type' => 'select',
    'label' => $this->l('Reduction Type').":",
    'name' => 'reduction_type',
    'options' => array(      
     'query' => array(array('reduction_type' => 'amount', 'name' => $this->l('Amount')), array('reduction_type' => 'percentage', 'name' => $this->l('Percentage'))),
     'id' => 'reduction_type',
     'name' => 'name'      
    )
   ),
   array(
    'type' => 'date',
    'label' => $this->l('From').":",
    'name' => 'from',
    'size' => 20,
    'class' => 'timepicker',
    'desc' => sprintf($this->l('Leave blank to clear the time'))
   ),
   array(
    'type' => 'date',
    'label' => $this->l('To').":",
    'name' => 'to',
    'size' => 20,
    'class' => 'timepicker',
    'desc' => sprintf($this->l('Leave blank to clear the time'))
   ),    
  ),
  'submit' => array(
   'title' => $this->l('Save'),
   'class' => 'button'
  )
 );
 
 $this->fields_value = array(
  'product_name' => $prod->name[$this->context->language->id]
 );
   
 return parent::renderForm();
}

可以看到在1.5裏,系統已經提供了內置的function讓我地只需要設定變數fields_form就能建立好張form

利用fields_form['legend']可以設定form legend 的部份...
'legend' => array(
 'title' => $this->l('Fast Get Discount Setting')    
 ),
設定後的效果

fields_form['input'] 讓我們設定能提供的變數, 先看看以下的頁面


它其實是由下面的code所建立成
array(
 'type' => 'text',
 'label' => $this->l('Product name').":",
 'disabled' => true,
 'name' => 'product_name'    
),
array(
 'type' => 'hidden',     
 'name' => 'id_product'
),
array(
 'type' => 'text',
 'label' => $this->l('No. of Sold').":",
 'name' => 'position',
 'size' => 25,
 'required' => true,
 'hint' => sprintf($this->l('Allowed number only'))
),
  • 第一個item我地設定disable為true後, 頁面上這個field 就會變成為唯讀
  • 第二個item,我地設定它為hidden,所以在頁面上是看不到它的存在
  • 第三個item,我地設定required為true,所以可以看到*在後面,而且也因為設定了hint,所以在滑鼠移上去時會出現提示信息
fields_form['submit']可以讓我們更改title bar入面的儲存按鈕
'submit' => array(
 'title' => $this->l('Save'),
 'class' => 'button'
)
由於第一個item(product_name),不是直接由database中取出來的(頁面所設定的className), 所以它的內容要透過fields_value來設定
$this->fields_value = array(
 'product_name' => $prod->name[$this->context->language->id]
);
最後,當然要call 番parent的renderform, 如果唔係可是會出現白頁的情況呢!
return parent::renderForm();

0 回應:

Post a Comment