使用ajax的HelperList状态操作

时间:2014-10-30 18:13:04

标签: php ajax prestashop prestashop-1.6

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

'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类中没有关于如何调用函数的注释。

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

如果您为数据对象使用ObjectModel类,则只需添加一行即可自动生成切换按钮:

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

希望这会帮助你。