2014-10-30 53 views
0

我负责创建一个小型支付模块。必须使用简单的CRUD管理配置,并使用HelperList类来显示包含存储在数据库中的记录的表。 其中一个表的数据库结构的类似于本HelperList状态动作与AJAX

'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;' 

所以,list_fields值是这样

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 
) 
); 

正如我打算启用或通过按钮我使用“活性禁用项'和'ajax'选项,并且在模块配置页面中呈现时,为相关列生成的链接如下所示:index.php?controller=AdminModules&configure=Example&item_id=4&statusItem&action=statusItem&ajax=1&(...)。请注意,statusItem是动作的名称。

另一方面,我在模块主文件中写了这个函数,它应该改变项目状态。

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(); 
} 

我一直在使用this文章正式文件的创建列表,但是不管我用什么名字(“ajaxProcess”,“ajaxProcessSatusItem”,“statusItem”,和每一个瓶盖的变化我能想到的)我得到的只是一个空白页面,没有改变状态。我查看了源代码,并且在HelperList类中没有关于如何调用函数的评论。

任何帮助将不胜感激。

回答

0

如果使用ObjectModel类为您的数据对象,您可以autmatically产生切换按钮,只需添加一行:

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, 
) 

'active' => 'status',行并未提及任何字段名。将此行添加到列表定义中(如果您要在Admin {YourObjectModel} Controller中定义列表字段属性或从其他位置调用HelperList)。

从我ObjectModel的摘录:

ProductTab.php 

class ProductTab extends ObjectModel { 
....... 
    public static $definition = array(
    .......... 
    'active' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool',), 

我抬头一看我的代码,我发现我居然叫一个特殊的处理功能:

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); 
    } 
} 

希望这将帮助你。

+0

'active'字段是否必须是布尔值?作为替代方案,我使用了一个类似于AdminStatusesController中显示的ajaxProcessStatusItem,但这也不起作用。 – Trickylastname 2014-11-06 17:36:59

+0

我更新了答案 – gskema 2014-11-06 18:30:57