Magento – To fetch product id from product grid under custom module tab

gridgrid-serlizationproduct

I have created one custom module for slider. I want to add product to the slider manually. so i have decided to show product grid under select products tab. I got product grid. But i can't get product id value.

I got following value,when i use var_dump()

array(14) { ["form_key"]=> string(16) "mdfqCbKxuebW87wT" ["title"]=> string(4) "TEST"
["status"]=> string(0) "" ["c_id"]=> string(2) "12" ["store"]=> string(1) "5" 
["page"]=> string(1) "1" ["limit"]=> string(3) "100" ["in_category"]=> string(0) ""
["entity_id"]=> string(1) "," ["name"]=> string(0) "" ["type"]=> string(0) "" ["sku"]=> 
string(0) "" ["websites"]=> string(0) "" ["links"]=> string(0) "" }

product grid returns an empty value

enter image description here

its describe, product grid under select products tab.

https://docs.google.com/file/d/0B9pmknBzwZOuQ3k2M1BLQmhzMFE/edit

Here is my complete code

I can't understand where is problem arise! If you found any thing wrong here, please save me guys!

Thank you

Best Answer

The normal behaviour for the grid checkbox is to bind a js event to it, and action the checkbox check/uncheck. Normally this would be to populate a hidden field with the selected values, and then that field is what gets posted to your controller action.

See varienGrid.prototype /js/mage/adminhtml/grid.js : (this.checkboxCheckCallback) and also setCheckboxChecked method in that class.

For this reason the checkbox element generally do not get a name assigned to it, which you can see if you view your element properties.

enter image description here

This is thus the reason why you cannot get the selected options in your post action, since elements with no name will not post in the form, and you most likely do not have the required callback set to deal with the check/uncheck of the box. Using the callback js, you could also effect other actions in your gui, to enhance the user experience.

However, looking at your usage requirements, you may just get away with setting a name for the checkbox element, which will then post the data to your action.

Thus if you adjust your element definition to this (adding in the 'field_name' property), you will get your data in an array called 'selectedproducts'

$this->addColumn('in_category', array(
                'header_css_class' => 'a-center',
                'type'      => 'checkbox',
                'values'    => $this->_getSelectedProducts(),
                'align'     => 'center',
                'index'     => 'entity_id',
        'name'      => 'in_category',
                'field_name' => 'selectedproducts[]'
            ));

Hope that helps.