Does anyone know how to add a field containing an input type text element or a dropdown element to a magento backend product list grid?
I managed to add a new column to my custom module backend product listing grid like this:
$this->addColumn('blabla', array(
'header' => Mage::helper('customer')->__('On Hold?'),
'width' => '120',
'index' => 'bla',
'type' => 'options',
'options' => array('1' => 'Yes', '0' => 'No')
));
but this command only adds the dropdown to my grid header, while i need the dropdown to appear in the left side of every product listed on that grid (just like the checkbox appears when you go for instance in backend on a product edit page and you select related products, or upsell products)
Simple and fast solution as tip for next research - rewrite Mage_Adminhtml_Block_Catalog_Product_Grid, function _prepareColumns. Example you will create your block Module_Name_Block_Sample:
class Module_Name_Block_Sample extends Mage_Adminhtml_Block_Catalog_Product_Grid
{
protected function _prepareColumns()
{
$this->addColumn('blabla', array(
'header' => Mage::helper('customer')->__('On Hold?'),
'width' => '120',
'index' => 'bla',
'type' => 'options',
'options' => array('1' => 'Yes', '0' => 'No')
));
return parent::_prepareColumns();
}
}
You will get it as first field. And it may need rewrite _prepareCollection.
But it may be not better solution, I know.
What you need is a custom renderer, where you can display any HTML you want. Something like this:
$this->addColumn('blabla', array(
'header' => Mage::helper('customer')->__('On Hold?'),
'width' => '120',
'index' => 'bla',
'renderer' => 'module/sample_grid_renderer'
));
And then you create your renderer class, where you create HTML you need:
class Module_Name_Block_Sample_Grid_Renderer
extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract
{
public function render(Varien_Object $row)
{
$html = '<select name="row'.$row->getId().'"></select>';
return $html;
}
}
$country = $fieldset->addField('country', 'select', array(
'name' => 'country',
'label' => 'Country',
'values' => Mage::getModel('adminhtml/system_config_source_country') ->toOptionArray()
));
Try it! Have a nice day. Thank you.
Related
I'm trying to add 'stock status' column to the Admin Manage Product Grid. Stock status is either "In Stock" or "Out of Stock".
Seems like I need to edit Adminhtml/Block/Catalog/Product/Grid.php
I added this line:
$this->addColumn('stock',
array(
'header'=> Mage::helper('catalog')->__('Stock Avail.'),
'width' => '70px',
'index' => 'status',
'type' => 'options',
'options' => Mage::getSingleton('cataloginventory/source_stock')->toOptionArray()
But just prints out Array text....
Try with following code as mentioned by Marius in this answer.
In Grid.php file find $this->setCollection($collection); and before this code add following code (Join) :
$collection->joinTable(
'cataloginventory/stock_status',
'product_id=entity_id',
array("stock_status" => "stock_status"),
null ,
'left'
)->addAttributeToSelect('stock_status');
And now you can add column like :
$this->addColumn('stock_status',
array(
'header'=> 'Stock Status',
'width' => '60px',
'index' => 'stock_status',
'type' => 'options',
'options' => array('1'=>'In Stock','0'=>'Out Of Stock'),
));
I hope this will help
I've added a customer column to the Sales > Orders grid by extending prepareColumns. What determines the order the columns are rendered in? Is it possible to change a column's position on the grid?
By default columns are displayed in the order addColumn() was called. You can change this with the function addColumnAfter().
The following code would add a column with an id of "category_id" after the "entity_id" column. The 'entity_id' is the id of the column, not the "index". 99/100 times these are the same, but be aware of it.
$this->addColumnAfter('category_id',array(
'header' => 'Category ID',
'index' => 'category_id',
'type' => 'text',
'width' => 70
),
'entity_id'
);
Use $this->addColumnAfter() instead of $this->addColumn()
$this->addColumnAfter('customattribute', array(
'header'=> Mage::helper('customer')->__('customattribute'),
'index' => 'customattribute2',
'type' => 'options',
'options' => Mage::getSingleton('adminhtml/system_config_source_yesno')->toArray(),
'width' => '100px',
), 'before_grid_id');
Put $this->sortColumnsByOrder(); at last of _prepareColumns() function.
protected function _prepareColumns() {
parent::_prepareColumns();
$this->addColumnAfter('category_id', array(
'header' => $this->__('Category ID'),
'index' => 'entity_id',
'type' => 'text',
'width' => 70
), 'entity_id');
$this->addColumnAfter('name', array(
'header' => $this->__('Category name'),
'sortable' => true,
'index' => 'category_name'), 'entity_id');
$this->sortColumnsByOrder();
return $this;
}
IMHO none of the answers above answer the question about setting the order of an existing column, i.e. without adding it manually.
For example for placing the "SKU" column right after the "ID" one, use the following code:
$this->addColumnsOrder('sku', 'entity_id');
Where the first parameter is the ID/name of the column to be inserted after the column identified in the second parameter.
By using the addColumnAfter and/or addColumnOrder functions instead of addColumn.
I have a form with multi select box:
echo $this->Form->input('emp_id', array( 'options' => array( $arr),
'empty' => '(choose one)',
'div'=>'formfield',
'error' => array( 'wrap' => 'div',
'class' => 'formerror'
),
'label' => 'Team Members',
'type' => 'select', 'multiple' => true,
'style' => 'width:210px; height:125px;'
));
I selected multiple values from this list box and click the SAVE button.
But it displays the validation message.
How can i solve this?
class TravancoDSRGroup extends AppModel {
var $name = 'TravancoDSRGroup';
var $useTable = 'dsr_group'; // This model uses a database table 'exmp'
var $validate = array(
'emp_id' => array(
'rule' => 'notEmpty',
'message' => 'The employee field is required'
)
);
}
This is the model code....
If it is possible...?
You don't have to explicitly specify
'type' => 'select'
if you set 'emps' from the controller, it will allow cake's automagic to work. just add
$this->set(compact('emps'));
Post output of debug($this->request->data) for better understanding of problem.
Have you defined hasMany or HABTM relationships to the emp ? To have multiple values saved you will have to define hasMany or HABTM relations, if you wish to save it as CSV then you will have to do the processing yourself in 'beforeSave'.
I created grid in magento backend, but pagination doesn't work. No matter how many records per page I choose there are always all of them visible on the page. Now I have 41 records in database and 'stats' above the grid are ok (number of pages and records found):
Page 1 of 3 pages | View 20 per page | Total 41 records found
Which file is responsible of pagination?
There's also another problem with order by certain column. For ex. records are displayed the same way either I choose ASC or DESC order by ID...
Grid:
public function __construct() {
parent::__construct();
$this->setId('logger_grid');
$this->setUseAjax(FALSE);
$this->setDefaultSort('id');
$this->setDefaultDir(Varien_Data_Collection::SORT_ORDER_ASC);
$this->setSaveParametersInSession(TRUE);
}
public function _prepareCollection() {
$collection = Mage::getModel('logger/logger')->getCollection()->load();
$this->setCollection($collection);
return parent::_prepareCollection();
}
public function _prepareColumns() {
$this->addColumn('id', array(
'header' => Mage::helper('logger')->__('ID'),
'sortable' => TRUE,
'index' => 'log_id',
'editable' => FALSE,
));
$this->addColumn('interface', array(
'header' => Mage::helper('logger')->__('Interface'),
'sortable' => TRUE,
'index' => 'interface',
'editable' => FALSE,
));
$this->addColumn('type', array(
'header' => Mage::helper('logger')->__('Type'),
'sortable' => TRUE,
'index' => 'type',
'editable' => FALSE,
));
$this->addColumn('description', array(
'header' => Mage::helper('logger')->__('Description'),
'sortable' => TRUE,
'index' => 'description',
'editable' => FALSE,
));
$this->addColumn('message_data', array(
'header' => Mage::helper('logger')->__('Message'),
'sortable' => TRUE,
'index' => 'message_data',
'editable' => FALSE,
));
$this->addColumn('time', array(
'header' => Mage::helper('logger')->__('Time'),
'sortable' => TRUE,
'index' => 'time',
'editable' => FALSE,
'type' => 'datetime',
));
return parent::_prepareColumns();
}
public function getRowUrl($row) {
return $this->getUrl('*/*/edit', array('id' => $row->getId()));
}
Collection.php:
public function _construct(){
$this->_init("logger/logger");
}
Ok, problem solved. As always just one little thing...In _prepareCollection() function I used
$collection = Mage::getModel('logger/logger')->getCollection()->load(); and pagination didn't work because of load() function.
Thanks for replying, sparcksoft :)
If you created a custom collection resource model, it could be that you've overwritten or broken the implementation of {{_renderLimit()}} which, adds a limit to the underlying SQL query based on the current page and page size.
// Varien_Data_Collection_Db
protected function _renderLimit()
{
if($this->_pageSize){
$this->_select->limitPage($this->getCurPage(), $this->_pageSize);
}
return $this;
}
Can you post relevant portions from your collection resource model and maybe your grid block?
You can use this:
protected function _prepareCollection()
{
$collection = Mage::getModel('vendor/model')->getCollection();
$this->setCollection($collection);
//
return Mage_Adminhtml_Block_Widget_Grid::_prepareCollection();
}
In this case you can reach your custom model and display with pagination
To fix this, you need to edit one of your core file please follow the below link:
https://raisereview.com/wrong-grid-count-and-pagination-issue-in-magento-admin-grid/
i am new to magento by following this guide Custom Module with Custom Database Table
i have implemented my already existed module to the backend adminhtml. i am taking stuff from the database and sjowing ot on the adminhtml page. Everything works ok except i am getting the grid twice on the adminhtml. i am getting the same Grid two time. i have looked the code for like 2 hours cannot figure it out. if anyone one knows how to fix this problem i will be greatly thakful. cheers
thats the code from my grid.php
<?php
class Ecom_Pricenotify_Block_Adminhtml_Pricenotify_Grid extends Mage_Adminhtml_Block_Widget_Grid{
public function __construct()
{
parent::__construct();
$this->setId('pricenotifyGrid');
// This is the primary key of the database
$this->setDefaultSort('pricenotify_id');
$this->setDefaultDir('ASC');
$this->setSaveParametersInSession(true);
}
protected function _prepareCollection()
{
$collection = Mage::getModel('pricenotify/pricenotify')->getCollection();
$this->setCollection($collection);
return parent::_prepareCollection();
}
protected function _prepareColumns()
{
$this->addColumn('pricenotify_id', array(
'header' => Mage::helper('pricenotify')->__('Notification ID'),
'align' =>'left',
'width' => '50px',
'index' => 'pricenotify_id',
));
$this->addColumn('prod_id', array(
'header' => Mage::helper('pricenotify')->__('Product ID'),
'align' =>'left',
'width' => '50px',
'index' => 'prod_id',
));
$this->addColumn('prod_price', array(
'header' => Mage::helper('pricenotify')->__('Product Price'),
'align' =>'left',
'width' => '50px',
'index' => 'prod_price',
));
$this->addColumn('user_price', array(
'header' => Mage::helper('pricenotify')->__('User Price'),
'align' =>'left',
'width' => '50px',
'index' => 'user_price',
));
$this->addColumn('email', array(
'header' => Mage::helper('pricenotify')->__('E-Mail Address'),
'align' =>'left',
'width' => '150px',
'index' => 'email',
));
$this->addColumn('created_time', array(
'header' => Mage::helper('pricenotify')->__('Creation Time'),
'align' => 'left',
'width' => '120px',
'type' => 'date',
'default' => '--',
'index' => 'created_time',
));
$this->addColumn('status', array(
'header' => Mage::helper('pricenotify')->__('Status'),
'align' => 'left',
'width' => '80px',
'index' => 'status',
'type' => 'options',
'options' => array(
'success' => 'Inactive',
'pending' => 'Active',
),
));
return parent::_prepareColumns();
}
public function getRowUrl($row)
{
return $this->getUrl('*/*/edit', array('id' => $row->getId()));
}}
and this indexAction function is from the controller
public function indexAction() {
$this->_initAction();
$this->_addContent($this->getLayout()->createBlock('pricenotify/adminhtml_pricenotify'));
$this->renderLayout();
}
Maybe you're inserting it in the layout, check pricenotify.xml in
adminhtml>default>default>layout.
Such as:
<pricenotify_adminhtml_manager_pricenotify>
<block type="core/text_list" name="root" output="toHtml">
<block type="pricenotify/adminhtml_pricenotify_grid" name="pricenotify.grid"/>
</block>
</pricenotify_adminhtml_manager_pricenotify>
Remove this block or comment the line where you add the content.
I fixed it. i only had to comment out
//$this->_addContent($this->getLayout()->createBlock('pricenotify/adminhtml_pricenotify'));
from indexAction i guess iwas loading it twice.
make sure that the grid block isn't already loaded in the corresponding layout.xml file.
Well I was facing the same issue but in my case it was due to $this->setId('messages'); line (in your Grid.php construct). Because magento already has same <div id="messages"></div> in its grid page(for showing notifications) due to which my grid content was getting loaded within this 'div' tag hence showing grid twice. So lesson learned is don't give general name while setting your 'id' in Grid.php which could already be present in the grid page.
In my case, it's happened on Edit/Form, and I had duplicated unintentionally renderLayout() on my Adminhtml controller.
$this->renderLayout();