Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento Disable Filter Field to Admin Grid

Tags:

magento

I have a custom module with a backend page. In the grid, I show the customer email as the user name. By default, Magento adds a filter to every column in the grid. Now, when I try to filter by the customer's email, I get an exception saying that my custom table doesn't have an email column. Magento is trying to find that in my custom table. How can I fix this problem, or how can I remove the field of that column so that the admin can't filter by that field. Thanks.

like image 721
Castro Roy Avatar asked Sep 07 '10 14:09

Castro Roy


3 Answers

Add the option

    'filter' => false

to the column you want to remove the filter from in the grid view (e.g. app/code/core/Mage/Adminhtml/Block/Sales/Order/Grid.php)

    $this->addColumn('email', array(
       'header'    => Mage::helper('module')->__('Email'),
       'align'     =>'left',
       'index'     => 'email',
       'filter' => false,
   ));
like image 57
Charly Avatar answered Nov 03 '22 21:11

Charly


I guess you meant "how can I remove the field of that column so that the admin can ' t filter by that field"

If so, I can tell you how to remove the email field.

Open /app/code/local/Namespace/Module/Block/Adminthml/Module/Grid.php

Somewhere in the protected function _prepareColumns() you should find something like :

  $this->addColumn('email', array(
      'header'    => Mage::helper('module')->__('Email'),
      'align'     =>'left',
      'index'     => 'email',
  ));

Just comment this lines.

And watch out that, in the __construct method at the very beginning of the whole class, you don't have

  $this->setDefaultSort('email');

If so, change it to

$this->setDefaultSort('id'); // if you have an id field in your module.
like image 42
Hervé Guétin Avatar answered Nov 03 '22 21:11

Hervé Guétin


If you don't have an email column in your custom table, then I assume you're creating your grid by joining your custom table to a core table that contains the users email address, such as customer_entity.

When you filter on a column, Magento uses the column index to produce the where clause. Which in your case will give something like WHERE email LIKE '%filter value%', but it wont find email in your custom table.

You might be able to fix this by using filter_index to explicitly tell Magento which table and column to use when building the where clause. Try something like this

$this->addColumn('email', array(
    'header'    => Mage::helper('module')->__('Email'),
    'index'     => 'email',
    'filter_index'          => 'core_table.email',
));

where core_table is the table you are joining with.

like image 1
Dom Avatar answered Nov 03 '22 21:11

Dom