1

I have created a custom admin module and in my Model/Nic.php, I have created below function

public function toOptionArray()
{
    $nicCollection = Mage::getModel('nic/nic')->getCollection();

    $nicarray = array();
    $i = 0;
    foreach ($nicCollection as $nicList) {
        $nicarray[] = array('-1'=>'Please Select','value'=>$nicList['nic_id'],'label'=>$nicList['nic_name'],'nic_cost'=>$nicList['nic_cost']);
    }

     return $nicarray;
}

which gives me below array values when printed $nicarray,

Array
(
    [0] => Array
        (
            [-1] => Please Select
            [value] => 1
            [label] => America
            [nic_cost] => 3.4
        )

    [1] => Array
        (
            [-1] => Please Select
            [value] => 2
            [label] => China
            [nic_cost] => 2.3
        )

    [2] => Array
        (
            [-1] => Please Select
            [value] => 3
            [label] => India
            [nic_cost] => 4.5
        )

    [3] => Array
        (
            [-1] => Please Select
            [value] => 4
            [label] => Japan
            [nic_cost] => 2.5
        )

)

Now in my admin form, I am able to fetch values in dropdown format usign toOptionArray.

$fieldset->addField('nic_id', 'select', array(
    'label'     => Mage::helper('nic')->__('Nic'),
    'class'     => 'required-entry',
    'values'    => Mage::getModel('nic/nic')->toOptionArray(),
    'name'      => 'nic_id',
));


$fieldset->addField('nic_cost', 'text', array(
    'label'     => Mage::helper('coffeecomponents')->__('Nic Cost'),
    'class'     => 'required-entry',
    'required'  => true,
    'name'      => 'nic_cost',
));

What I need to do is to fetch the value of nic_cost in Nic Cost input field based on what is selected from dropdown.

So if admin selects America from dropdown, then in Nic Cost input field the value should be populated with 3.4 (as shown in array) and if China then value should be 2.3

How can I change value dynamically when option is changed/selected from dropdown ?

4

Make your method look like this:

public function toOptionArray()
{
    $nicCollection = Mage::getModel('nic/nic')->getCollection();

    $nicarray = array(
         array(
            'value'=>-1, 
            'label'=>'Please Select'
         )
    );
    foreach ($nicCollection as $nicList){
        $nicarray[] = array(
               'value'=>$nicList['nic_cost'],
               'label'=>$nicList['nic_name']
        );
    }
    return $nicarray;
}
| improve this answer | |
  • The value has been replaced to cost. But I need to save the cost as well as the value i.e. [value] => 1 and [nic_cost] => 3.4 – Mark Jun 10 '14 at 9:15
  • what difference it would make If I change my method to what you have posted ? – Mark Jun 10 '14 at 10:22
  • @Mark Your cost will be saved in the db. If you want to save only the id then replace $nicList['nic_cost'] with $nicList['nic_id']. You cannot save both. but you don't need to. Once you save the id, you will have a reference to your nic entity, and you can retrieve the cost from there, – Marius Jun 10 '14 at 10:35
  • Okay Marius, lets say I save nic_id in my DB, then how can I fetch value for Nic Cost i.e. nic_cost DB field using onchange event in admin form ? – Mark Jun 10 '14 at 10:43
  • @Mark. I wasn't talking about js. I was refering to the frontend...if you needed the value then you could have make an other select to get the cost. If you want to use it in an onchage js event, I think the best approach would be to have a JSON object will all the ids, names and costs, and when changing the select then look in the JSON to see if there is a value for your ID for cost. If so, do with it what you need. SOrry but I have no code for it. The only thing I no is that magento select values support only value and label. – Marius Jun 10 '14 at 10:46

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.