0

I have a admin grid in admin panel, which I created using Ultimate module creator.

This Module has following columns in database table:

1. id
2. ref_id
3. ref_type
4. position

Thus, the admin grid also has the same columns.

But my requirement is to show the admin grid differently i.e

1. name
2. email
3. location
4. position

I can change the columns by visiting {namespace}/{module_name}/Block/Adminhtml/{module_name}/Grid.php

But how to fill the values in newly added columns based on the old column values for each row?

I tried like this which didn't work:

    $this->addColumn('name', array(
        'header' => Mage::helper('modulename')->__('Name'),
        'index' => 'name',
        'type' => 'text',
        'value' => 'hello' //Here I am trying to manipulate this column using  "ref_id" and "ref_type" 
    ));

Please help

PS: The user can edit only "position" field (which is the only visible field), when he/she clicks edit action.

3

If you just want to change the names that appear in the grid the fix is easy. On the other hand, if you want to show in the grid a value that does not exist in your DB, it's a bit more complicated.

You said that there are 4 fields in your DB. id, ref_id, ref_type, position. These are the only 4 columns you can have in your grid. If you want more grid columns you must have either more DB fields or a joined table or a custom/expression field.

Let's take the addColumn function

   $this->addColumn('name', array(
        'header' => Mage::helper('modulename')->__('Name'), //The name that appears in the header of the grid
        'index' => 'name', //The reference to the DB field (or  custom field)
        'type' => 'text',  //The type of the column
        'value' => 'hello' //Here I am trying to manipulate the columns  "id" and "ref_id" // This you cannot do this way
    ));

So you are trying to get a field 'name' from the DB but that doesn't exist. Neither do email and location so where will these data be stored?

From what I understand 'name' is supposed to be an expression of 'ref_id and 'ref_type'. You can read this and this for starters.

EDIT:

Let's say you want a custom column 'name' that will show 'ref_id' and 'ref_type' separated by a space.

your _prepareCollection() function should look something like the following

protected function _prepareCollection() {
    $collection = Mage::getModel('modulename/modelname')->getCollection();
    $collection->getSelect()->columns(new Zend_Db_Expr("CONCAT(`ref_id`, ' ',`ref_type`) AS name"));
    //add the above line before setCollection
    $this->setCollection($collection);
    return parent::_prepareCollection();
}

Then in your _prepareColumns() function add

$this->addColumn('name', array(
    'header' => Mage::helper('modulename')->__('Name'),
    'index' => 'name', //this index points to the column you created in prepareCollection
    'type' => 'text',
));
  • I don't know how to assign a value to each row of the new columns. I mean to which attribute should I add? and how? I am showing value attribute just for example. I am not sure whether I should use value attribute or not. Lets say value attribute is right attribute. Then How should I use ref_id and ref_name of that row to manipulate the value? I am looking for something like this: 'attributeName' : if($refName == "somename"){ return $this->getId($refId)->getName()}else{return "none"; }. I hope you got my point now.. – Mr_Green Apr 1 '14 at 9:18
  • 1
    There is no way to do this in your prepareColumn function. You have to add a custom field to your collection in the prepareCollection function. I'll update the answer – Yiorgos Moschovitis Apr 1 '14 at 9:24
  • Thanks.. I hadn't thought about _prepareCollection() :) – Mr_Green Apr 1 '14 at 9:41
  • You're welcome. – Yiorgos Moschovitis Apr 1 '14 at 9:44

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.