Jump to content

Admin drag and drop reorder not working


fana605

Recommended Posts

Hi,

 

I'm building a very simple module for PS1.6, mostly for learning purposes. It has an admin interface like you can see in this picture:

post-970586-0-88253500-1427045863_thumb.jpg

 

I've added the position field to the list table and it shows perfectly, but the drag and drop feature doesn't work. 

 

In my module admin controller, I have this code (just the fields list part):

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

This makes the listing table show the position field in the table like you can see in the printscreen above, but I can't drag and drop to reorder... What is missing here? Some javascript? Some option?

 

Appreciate any help.

 

 

 

 

 

 

Link to comment
Share on other sites

  • 4 weeks later...

Hey Tuni-Soft, thanks for your help! This actually led me into to right direction. With a few tweaks I managed to activate the drag and drop reorder feature of my custom admin module.

 

For anyone interested, this is what I've done, besides the code in the first post:

 

Before the __contruct function, right after opening the class add this:

protected $position_identifier = 'id_quicklinks';

* id_quicklinks is the primary key of the database table this module uses.

 

This activates the drag and drop but it's not enough. It still doesn't save the order in the database.

 

For that to work I added two more functions 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;
                }
            }

    }

And:

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

And that's it. Works fine in my case. I'm not sure this will work for every case, but at least it should help to get into the right direction. 

 

I may be wrong but I didn't find anything in the docs that made reference to this...

 

Thanks again Tuni-Soft!

Edited by fana605 (see edit history)
Link to comment
Share on other sites

  • 4 weeks later...

Under ajaxProcessUpdatePositions function the line:

 

$positions = Tools::getValue('quicklinks');

The 'quicklinks' where is pointing to?, to the list name? to element of the list? where are you defined this?

Link to comment
Share on other sites

  • 8 years later...

It's the name of the array generated when submitting the ajax.

From what I saw it removes uses the table identifier as the base, but removing the 'id_' part if it exists

 

So probably in this example his identifier was id_quicklinks, then when submitting the rows information was sent to the variable called quicklinks.

Hope that helps just in case anyone have the same doubts as Sundbox

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...