Magento – How to create an admin grid with non auto increment field

adminhtmlgridmagento-1.9

I want to create an admin grid which is editable. The problem is that the id column of this grid will be a non auto increment value. Is this possible to implement?

Can any one suggest tutorials or articles related to this?

UPDATE: I'm using a collection which is gained by joining the magento
attribute collection with my custom table. Butin the provided code below it contains only product attribute collection. ID column will contain the attribute_id

Grid.php

<?php
class BalanceAP21Connector_ProductSync_Block_Adminhtml_Attributemapping_Edit_Tab_Attribute
    extends Mage_Adminhtml_Block_Widget_Grid
{
    /**
     * Constructor, ensures pagination is hidden
     */
    public function _construct()
    { 
        $this->setId('attribute_edit');
        $this->setUseAjax(true);
        $this->setDefaultSort('attribute_id');
        $this->setPagerVisibility(true);       
        $this->setSaveParametersInSession(true);         
        parent::_construct();
    }

    /**
     * Prepare grid collection object. Collection object populated
     * based on Apparel 21 module settings in admin configuration section.
     */
    public function _prepareCollection()
    {
        $collection = Mage::getResourceModel('catalog/product_attribute_collection');
        $collection->addFieldToFilter('frontend_label', array('notnull' => true));
        //echo $collection->getSelectSql();exit;
        $this->setCollection($collection);
        parent::_prepareCollection();
        return $this;
    }

    /**
     * Prepare columns for the grid
     */
    public function _prepareColumns()
    {
        $helper = Mage::helper('productsync');
        $this->addColumn('attribute_id', array(
                'header' => $helper->__('ID'),
                'index'  => 'attribute_id'
        ));

        $this->addColumn(
            'frontend_label',
            array(
                'header' => $helper->__('Reference Type Name'),
                'index'  => 'frontend_label'
            )
        );

        $this->addColumn(
            'attribute_ids',
            array(
                'header'   => $helper->__('Attribute'),
                'width'    => '1',
                'type'     => 'options',
                'index'    => 'attribute_ids',
                'editable' => true,
                'options'  => Mage::getModel('productsync/attributemapping_system_config_source_attributes')
                    ->toOptionArray(),
                'renderer' => 'productsync/adminhtml_attributemapping_widget_grid_column_renderer_options',
            )
        );

        $this->addColumn(
            'note_code',
            array(
                'header' => $helper->__('AP21 note code'),
                'index'  => 'note_code',
                'type'   => 'input'
            )
        );

        $this->addColumn(
            'note_regexp',
            array(
                'header' => $helper->__('Note regexp'),
                'index'  => 'note_regexp',
                'type'   => 'input'
            )
        );
        return parent::_prepareColumns();
    }

    public function getGridUrl()
    {
        //return $this->getUrl("*/*/edit", array("attribute_id" => $row->getId()));
        return $this->getUrl('*/*/grid', array('_current'=>true));
    }
}

My grid is as below.
enter image description here

Best Answer

To add an edit action and pass some value along with that action, you can use the below code

public function getRowUrl($row)
{
    return $this->getUrl('*/*/edit', array(
        'store'=>$this->getRequest()->getParam('store'),
        'id'=>$row->getId())
    );
}

This will attach a row url to js event handlers and thus when you click on a grid item, it will trigger editAction in your controller file. You can get those parameters like this.

<?php
class [Namespace]_[Module]_[Some]Controller extends Mage_Adminhtml_Controller_Action
{

    public function editAction()
    {
         $id = (int) $this->getRequest()->getParam('id');
    }
}
Related Topic