0

I have created custom extension in Magento 1.9.

The idea is to add dropdown select question field form the input value and upon saving, store it within database.

enter image description here

    public function getFieldOptionField()
    {
        $field = new Varien_Data_Form_Element_Text();
        $field->setName($this->getFieldName().'[{{index}}]['.$this->getFieldTextString().']')
            ->setId($this->getFieldId().'_{{index}}_'.$this->getFieldTextString())
            ->setStyle('width:300px!important;')
            ->setForm(new Varien_Data_Form())
            ->setAfterElementHtml($this->getIdHiddenField().$this->getDeleteHiddenField());

        return $this->toJSTmplHtml($field);
    }
8
  • whats your question here? what problem are you facing here?
    – Piyush
    Nov 2, 2017 at 5:22
  • now edit my question so please check getFieldOptionField() function add option field. Nov 2, 2017 at 5:27
  • same way i create getQuestionField() function select dropdown field. @Piyush Nov 2, 2017 at 5:31
  • If I understood right, do you want to add select field instead of text?
    – Piyush
    Nov 2, 2017 at 5:32
  • Yes any idea how to create.@Piyush Nov 2, 2017 at 5:34

2 Answers 2

1

For dropdown instead of text you have to try something like below code

public function getFieldOptionField()
{
    $field = new Varien_Data_Form_Element_Select();
    $field->setName($this->getFieldName().'[{{index}}]['.$this->getFieldTextString().']')
        ->setId($this->getFieldId().'_{{index}}_'.$this->getFieldTextString())
        ->setStyle('width:300px!important;')
        ->setForm(new Varien_Data_Form())
        ->setValues(Mage::getSingleton('your_module/your_model')->toOptionArray());
        ->setAfterElementHtml($this->getIdHiddenField().$this->getDeleteHiddenField());

    return $this->toJSTmplHtml($field);
}

In you model's toOptionArray method you can add your options like this

public function toOptionArray()
{
    return array(
        array('value' => 1, 'label' => Mage::helper('your_module')->__('Option 1')),
        array('value' => 0, 'label' => Mage::helper('your_module')->__('Option 2')),
    );
}
3
  • already accept your answer.@Piyush Nov 3, 2017 at 3:58
  • Please accept my question and answer both are useful text and select field.@Piyush Nov 3, 2017 at 4:06
  • your answer is useful but i have changes your answer and updated my answer below so check now. Nov 3, 2017 at 4:49
2
  1. For dropdown instead of select you have to try something like below code.

    public function getQuestionField()
    {
        $Collection = Mage::getModel('your_module/your_model')->getCollection();
    
        $quearray = array(array('value'=>0, 'label'=>'Please Select'));
    
        $currentqid = $this->getRequest()->getParam('question_id');
    
        foreach ($Collection as $question){
    
            if($currentqid != $question['question_id']) {
                $quearray[] = array(
                    'value'=>$question['question_id'],
                    'label'=>$question['question_id']
                );
            }
    }
    
    $field = new Varien_Data_Form_Element_Select();
    $field->setName($this->getFieldName().'[{{index}}][next_que_id]')
        ->setId($this->getFieldId().'_{{index}}_next_que_id')
        ->setValues($quearray)
        ->setForm(new Varien_Data_Form())
        ->setAfterElementHtml($this->getIdHiddenField().$this->getDeleteHiddenField());
    
    return $this->toJSTmplHtml($field);
    }
    

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.