1

my code:

protected function _prepareForm()
{

    $form = new Varien_Data_Form();
    $this->setForm($form);
    $fieldset = $form->addFieldset("tuition_form", array("legend"=>Mage::helper("tuition")->__("Student information")));

    $fieldset->addField("secondary", "multiselect", array(              
        "class" => "required-entry",
        "required" => true,
        "name"=>  "subject[secondary]",
        'values'   => Example_Tuition_Block_Adminhtml_Student_Grid::getValueArray34(),
    ));
     $fieldset->addField("others", "text", array(                   
        "class" => "required-entry",
        "required" => true,
        "name"=>  "subject[others]",

    ));
    $this->setForm($form);
     $this->setChild('form_after',

        // The content of the "form_after" block is a Mage_Adminhtml_Block_Widget_Form_Element_Dependence block typ
        $this->getLayout()->createBlock('adminhtml/widget_form_element_dependence')
            ->addFieldMap('junior', 'junior')
            ->addFieldMap('others', 'others')

       ->addFieldDependence('others', 'junior', 'chinese')
       );
   return parent::_prepareForm();
}

my multiple dropdown

enter image description here

here if i m select chinese i want text field. if i m select chinese,malay,...etc than i m getting textbox but when i m also select physics or science that time i m not getting textbox how to solve this problem?

  • So, you are trying to have a multi select dropdown? and the selected values should be shown in the above text box? – Mr_Green Aug 11 '15 at 11:30
  • Nooo, i have multi select dropdown if i m select chinese i want to display textbox. not shown in textbox,its empty textbox it should be display in form if i select chinese otherwise it should be hide – ND17 Aug 11 '15 at 11:32
  • Write a click event on those list item elements. Whenever user clicks on a list item, the click event gets triggered, in that triggered function you can display a textbox based on the text inside that clicked list item. – Mr_Green Aug 11 '15 at 11:34
  • it is in admin side not in frontend – ND17 Aug 11 '15 at 11:36
  • 1
    please provide code for your form.php and edit.php files if you have to add functionality in admin section. – amit_game Aug 11 '15 at 11:48
3

Here using javascript i can show or hide textbox not using dependency:

$field = $fieldset->addField("junior", "multiselect", array(
    "class" => "required-entry",
    "required" => true,
    "name" => "subject[junior]",
    'onchange' => 'change(this.value)',
    'values' => Example_Tuition_Block_Adminhtml_Student_Grid::getValueArray35(),
));
$field->setAfterElementHtml("<script>

        function change(selectItem){

            var e = document.getElementById('junior');
            var arr = new Array();
                for (var i = 0; i < e.options.length; i++) {
                    if(e.options[i].selected){
                        var val = e.options[i].value;
                        arr.push(val);
                    }
                }
            console.log(arr);
            var values = arr.indexOf('chinese');
            if (values != -1) { 
                document.getElementById('others').style.display = '';
            } else {
                document.getElementById('others').style.display = 'none';   
            } 
        }
        </script>");

$other = $fieldset->addField("others", "text", array(
    "class" => "required-entry",
    "required" => true,
    "name" => "subject[others]",

));
$other->setAfterElementHtml("<script>
                document.getElementById('others').style.display = 'none';               
            </script>");

Using this I can show and hide textbox on change of multiple dropdown.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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