Prestashop 自定义管理模块可拖动排序/顺序不起作用 [英] Prestashop custom admin module draggable sort/order not working

查看:58
本文介绍了Prestashop 自定义管理模块可拖动排序/顺序不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为 Prestashop 1.6 构建一个非常简单的模块,我添加了一个管理界面,允许列出我的记录,一个用于添加、编辑和删除的表单.

I'm building a very simple module for Prestashop 1.6 and I added an admin interface that allows to list my records, a form to add and edit and delete.

正如您在此处看到的那样,这工作正常:

This is working fine as you can see here:

问题是可拖动的重新排序按钮不可拖动.因此重新排序不起作用...

The problem is that draggable reorder button is not draggable. Hence the reordering is not working...

根据官方文档,如果您设置控制器中的位置选项,您将获得可拖动功能:

According to the official docs, if you set the position option in your controller, you get the draggable functionality:

['position'] => 'position', // If set to position, the field will display arrows and be drag and droppable, which will update position in db (optional).

这是我的模块控制器的相关部分:

This is the relevant part of my module controller:

$this->fields_list = array(
        'id_quicklinks' => array(
            'title' => $this->l('ID'),
            'align' => 'center',
            'width' => 25
        ),
        'titulo' => array(
            'title' => $this->l('Titulo'),
            'width' => 'auto'
        )
        , 'lead' => array(
            'title' => $this->l('Subtitulo'),
            'width' => 'auto'
        ), 
        'position' => array(
            'title' => $this->l('Ordem'),
            'filter_key' => 'a!position',
            'position' => 'position',
            'align' => 'center',
            'class' => 'fixed-width-md'
        ),
        'active' => array(
            'title' => $this->l('Publicado'),
            'width' => '25',
            'active' => 'status'
        )
    );

正如您在打印屏幕中看到的那样,它显示了手柄,但拖放不起作用.控制台上没有 javascript 错误,什么也没有……我可以在源代码中看到 jQuery 和 jQueryUI 已加载.其他具有重新排序功能的管理页面工作正常...

As you can see in the print screen, is shows the handles but the drag-an-drop doesn't work. No javascript errors on the console, no nothing... And I can see in the source code that jQuery and jQueryUI are loaded. Other admin pages with reorder feature are working fine...

有什么想法吗?谢谢.

推荐答案

好吧,如果有人感兴趣,我设法让它起作用.

Well, if anyone is interested, I managed to get this to work.

在我的模块管理控制器中,我添加了(在 __construct 方法之前,在打开类之后):

In my module admin controller, I've added (before the __construct method, right after opening the Class):

protected $position_identifier = 'id_quicklinks';

  • id_quicklinks 是该模块使用的数据库表的主键.
    • id_quicklinks is the primary key of the database table this module uses.
    • 这启用了我正在寻找的拖放功能,但尽管我现在可以拖放,但订单并未保存在数据库中.

      This enabled the drag and drop feature that I was looking for, but altough I could now drag and drop, the order was not saved in the database.

      为此,我添加了另外两个改编自 controllers/admin/AdminCarriersController.phpclasses/Carrier.php 的方法:

      For that to work I've added two more methods adapted from the controllers/admin/AdminCarriersController.php and classes/Carrier.php:

      public function ajaxProcessUpdatePositions()
          {
              $way = (int)Tools::getValue('way');
              $id_quicklinks = (int)Tools::getValue('id');
              $positions = Tools::getValue('quicklinks');
      
              if (is_array($positions))
                  foreach ($positions as $position => $value)
                  {
                      $pos = explode('_', $value);
      
                      if (isset($pos[2]) && (int)$pos[2] === $id_velcroquicklinks)
                      {
                              if (isset($position) && $this->updatePosition($way, $position, $id_quicklinks))
                                  echo 'ok position '.(int)$position.' for id '.(int)$pos[1].'\r\n';
                              else
                                  echo '{"hasError" : true, "errors" : "Can not update id '.(int)$id_quicklinks.' to position '.(int)$position.' "}';
      
                          break;
                      }
                  }
      
          }
      

      还有:

      public function updatePosition($way, $position, $id)
          {
      
              if (!$res = Db::getInstance()->executeS('
                  SELECT `id_quicklinks`, `position`
                  FROM `'._DB_PREFIX_.'quicklinks`
                  ORDER BY `position` ASC'
              ))
                  return false;
      
              foreach ($res as $quicklinks)
                  if ((int)$quicklinks['id_quicklinks'] == (int)$id)
                      $moved_quicklinks = $quicklinks;
      
              if (!isset($moved_quicklinks) || !isset($position))
                  return false;
              var_dump($moved_quicklinks['position']);
              // < and > statements rather than BETWEEN operator
              // since BETWEEN is treated differently according to databases
              return (Db::getInstance()->execute('
                  UPDATE `'._DB_PREFIX_.'quicklinks`
                  SET `position`= `position` '.($way ? '- 1' : '+ 1').'
                  WHERE `position`
                  '.($way
                      ? '> '.(int)$moved_quicklinks['position'].' AND `position` <= '.(int)$position
                      : '< '.(int)$moved_quicklinks['position'].' AND `position` >= '.(int)$position.'
                  '))
              && Db::getInstance()->execute('
                  UPDATE `'._DB_PREFIX_.'quicklinks`
                  SET `position` = '.(int)$position.'
                  WHERE `id_quicklinks` = '.(int)$moved_quicklinks['id_quicklinks']));
          }
      

      我希望这对有同样问题的人有所帮助.

      I hope this helps someone with the same problem.

      这篇关于Prestashop 自定义管理模块可拖动排序/顺序不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆