doujin8476 2014-10-30 18:13
浏览 29
已采纳

使用ajax的HelperList状态操作

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.

  • 写回答

1条回答 默认 最新

  • douduiwei2831 2014-11-02 21:00
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度