Jump to content

Admin drag and drop reorder not working


fana605
 Share

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.

 

 

 

 

 

 

Share this post


Link to post
Share on other sites

  • 4 weeks later...

Any ideas? I'm still stuck with this one...

You can check the file controllers/admin/AdminAttributesGroupsController.php for example

In the __construct there is an example on how to use position ordering

Share this post


Link to post
Share on other sites

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)

Share this post


Link to post
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?

Share this post


Link to post
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
 Share

×
×
  • Create New...

Important Information

Cookies ensure the smooth running of our services. Using these, you accept the use of cookies. Learn More