I'm in charge of creating a small payment module. The configuration has to be managed with a simple CRUD, and I use the HelperList class to display a table with the records stored in the database. One of the tables database structure is similar to this
'CREATE TABLE IF NOT EXISTS '._DB_PREFIX_.'MODULE_ITEM
(
`id` INT(11) NOT NULL AUTO_INCREMENT,
`name` VARCHAR(100) NOT NULL,
`active` VARCHAR(3) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=utf8;'
So, the list_fields value is like this
array(
'id' => array(
title' => $this->l('Id'),
//'width' => 140,
'type' => 'text',
'align' => 'center'
),
'name' => array(
'title' => $this->l('Name'),
//'width' => 140,
'type' => 'text',
'align' => 'center'
),
'active' => array(
'title' => $this->l('Status'),
//'width' => 140,
'active' => 'statusItem',
'type' => 'boolean',
'align' => 'center',
'ajax'=> true
)
);
As I intend to enable or disable the item via a button I use the 'active' and 'ajax' options for this specific field, and when rendered in the module configuration page the link generated for the column in question is something like: index.php?controller=AdminModules&configure=Example&item_id=4&statusItem&action=statusItem&ajax=1&(...)
. Please notice that statusItem is the name of the action.
On the other hand, I wrote this function in the module main file, which should change the item status.
public function ajaxProcessStatusItem()
{
$id=(int)Tools::getValue('item_id');
$value=(int) Db::getInstance()->executeS($this->createSelectQuery('module_item','item_id',$id))[active];
Db::getInstance()->update('module_item', array('active' => !$value), 'item_id='.$id);
die();
}
I've been using this article of the official documentation to create the list, but no matter what name I use ('ajaxProcess', 'ajaxProcessSatusItem', 'statusItem', and every caps variation I could think of) all I get is a blank page in response, and no change in the status. I had a look at the source code and there is no comment in the HelperList class regarding how the function should be called.
Any help will be appreciated.
If you use ObjectModel class for your data object, you can autmatically generate toggle button just by adding one line:
AdminProductTabController.php or when defining fields somwehre else
and calling HelperList->generate()
'active' => array(
'title' => 'Active',
'active' => 'status',
'filter_key' => '!active',
'type' => 'bool',
'width' => 'auto',
'orderby' => false,
'search' => false,
)
Line 'active' => 'status',
doesn't refer to any field names. Add this line to your list definition (if you're defining list field properties in Admin{YourObjectModel}Controller or calling HelperList from somewhere else).
An excerpt from my ObjectModel:
ProductTab.php
class ProductTab extends ObjectModel {
.......
public static $definition = array(
..........
'active' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool',),
I looked up my code and I noticed that I actually called a special processing function:
AdminProductTabController.php
public function initProcess()
{
$id_product_tab = (int)Tools::getValue('id_product_tab');
$product_tab = new ProductTab($id_product_tab);
$isStatusAction = Tools::getIsset('status'.$this->table);
if ($isStatusAction)
{
$product_tab->toggleStatus();
Tools::redirectAdmin($this->href_back);
}
}
Hope this will help you out.