2

In Tabs.php

 $this->addTab('form', array(
        'label' => Mage::helper('example')->__('Select Person'),
        'url' => $this->getUrl('*/*/person', array('_current' => true)),
        'class' => 'ajax',
    ));

Controller

public function personAction(){
    $this->loadLayout();
    $this->getLayout()->getBlock('person.grid')
    ->setProducts($this->getRequest()->getPost('persons', null));
    $this->renderLayout();
}


public function persongridAction(){
    $this->loadLayout();
    $this->getLayout()->getBlock('person.grid')
    ->setProducts($this->getRequest()->getPost('persons', null));
    $this->renderLayout();
}

in xml file

<example_adminhtml_select_person>

   <block type="core/text_list" name="root" output="toHtml">
       <block type="tuition/adminhtml_student_edit_tab_grid" name="person.grid"/>
       <block type="adminhtml/widget_grid_serializer" name="grid_serializer">
           <reference name="grid_serializer">
               <action method="initSerializerBlock">
                   <grid_block_name>person.grid</grid_block_name>
                   <data_callback>getSelectedPersons</data_callback>
                   <hidden_input_name>persons</hidden_input_name>
                   <reload_param_name>persons</reload_param_name>
               </action>

           </reference>
       </block>
   </block>

<example_adminhtml_select_persongrid>
    <block type="core/text_list" name="root" output="toHtml">
        <block type="example/adminhtml_example_edit_tab_grid" name="person.grid"/>
    </block>
</example_adminhtml_select_persongrid>

Grid.php

<?php
 class Company_Example_Block_Adminhtml_Example_Edit_Tab_Grid extends     Mage_Adminhtml_Block_Widget_Grid
{
 public function __construct()
 {
    parent::__construct();
    $this->setId('personGrid');
    $this->setUseAjax(false); // Using ajax grid is important
    $this->setDefaultSort('person_id');
    $this->setDefaultFilter(array('person'=>'')); // By default we have added a filter for the rows, that in_products value to be 1
    $this->setSaveParametersInSession(false);  //Dont save paramters in session or else it creates problems
}

protected function _prepareColumns() {

    $this->addColumn('person_id', array(
        'header_css_class'  => 'a-center',
        'header'    => $this->__('Select'),
        'required'          => 1,
        'type'              => 'checkbox',
        'html_name'         => 'person_id',
        'values'            => $this->_getSelectedPersons(),
        'align'             => 'center',
        'index'             => 'person_id'
    ));


    $this->addColumn('person_name', array(
        'header'=> Mage::helper('example')->__('Name'),
        'align' => 'left',
        'index' => 'name',
    ));



    return parent::_prepareColumns();
}

protected function _getSelectedPersons()   // Used in grid to return selected customers values.
{
    $person = array_keys($this->getSelectedPersons());
    return $person;
}
protected function _prepareCollection() {
    $currentStudentId = $this->getRequest()->getParam('id');
    $collection = Mage::getModel('example/person')->getCollection()
                ->addFieldToFilter('student_ids',array('finset' => $currentStudentId));
    $this->setCollection($collection);
    return parent::_prepareCollection();
}
public function getSelectedTutors()
{
    // Customer Data
    $tm_id = $this->getRequest()->getParam('id');
    if(!isset($tm_id)) {
        $tm_id = 0;
    }
    $conn = Mage::getModel('example/person')->load($tm_id);
    $data =  $conn->getData('person_id');
    $datas = explode(',',$data);


    $persons = $datas; 

    // This is hard-coded right now, but should actually get values from database.
    $tutIds = array();

    foreach($persons as $person) {

            $tutIds[$person] = array('id'=>$person);

    }

    return $tutIds;
}


public function getGridUrl()
{
    return $this->_getData('grid_url') ? $this->_getData('grid_url') : $this->getUrl('*/*/persongrid', array('_current'=>true));
}

protected function _addColumnFilterToCollection($column)
{

    if ($column->getId() == 'person') {
        $personIds = $this->_getSelectedPersons();
        if (empty($personIds)) {
            $personIds = 0;
        }
        if ($column->getFilter()->getValue()) {
            $this->getCollection()->addFieldToFilter('person_id', array('in'=>$personIds));
        } else {
            if($personIds) {
                $this->getCollection()->addFieldToFilter('person_id', array('nin'=>$personIds));
            }
        }
    } else {
        parent::_addColumnFilterToCollection($column);
    }
    return $this;
}

}?>

This is my code for grid serializer. My problem is When i am select grid and save in database and if i m edit that form that time checkbox is selected but if i m save that edit form i m getting null value of that because in hidden input i m not getting database value

See here

enter image description here

If i want that value again i have to unselect that checkbox and again i want to select that checkbox than i m getting value but i dont want to do this. I want database value in that input hidden

input hidden

<input type="hidden" name="persons" value="" id="id_6109c7e4877a7d469e1cc8b962055949">

How to solve this problem

2 Answers 2

3

I Solved this problem.

But its different way I am just set checkbox name like this

$this->addColumn('person_id', array(
    'header_css_class'  => 'a-center',
    'header'    => $this->__('Select'),
    'required'          => 1,
    'type'              => 'checkbox',
    'field_name'         => 'person_id[]',
    'values'            => $this->_getSelectedPersons(),
    'align'             => 'center',
    'index'             => 'person_id'
));

Use field_name instead of html_name and in post i am getting selected check-box array and save data using checkbox not hidden input

and remove ajax like this in Tabs.php

$this->addTab('form', array(
        'label' => Mage::helper('example')->__('Select Person'),
       "content" => $this->getLayout()->createBlock("example/adminhtml_example_edit_tab_grid")->toHtml(),
    ));
0

I have another way to solve this problem still use ajax grid. In function

public function personAction(){
$default_selected_persion = [] //your code logic to define default selected person here
$this->loadLayout();
$this->getLayout()->getBlock('person.grid')
   ->setProducts($default_selected_persion);
// add another set:
$this->getLayout()->getBlock('grid_serializer')
   ->setProducts($default_selected_persion);
$this->renderLayout();

}

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.