PrestaCraft on Facebook

Please like our page on Facebook to get notifications about new tutorials :)


Sometimes you want to make your admin controller list draggable/sortable, like you can in a few another pages in the BackOffice (for example: Categories or Products in Categories).
Good news is you don’t have to include 3rd party plugin to sort your table. All you need to do is implement additional functions to your controller and ObjectModel class.

Before any changes, please remember to do a backup of all your files! I don’t take any responsibility for presumable inconveniences or bugs which may occur after those modifications.

In this tutorial I will use following names:
AdminTestController – as your BackOffice controller
Test – as your ObjectModel class name
testmodule – as your module name

To get through this tutorial in the easiest way, you should put column called “position” in your database table (and define it in your ObjectModel class. If you don’t have such column, please add one.
Edit: testmodule/controllers/admin/AdminTestController.php

First of all you need to tell the controller what is your table ID. It will be used as key in sorting.


Add after:

Now you should add position field to the list

Find (or create):

Add inside:

Next thing to do is adding a new, special function which will be called during rows re-positioning.

Add in the end of class:

All you have to do here is replace Test with your class name – it is commented.

Now it’s time to move to the ObjectModel class.

Edit: testmodule/classes/Test.php

This file must contain 4 additional functions. I will explain one-by-one.

Add in the end of class:

This function (as its name shows) is updating current item’s position.
In the last query you can see:
– updating positions of another items in the table
– setting position of the current item
This is necessary, because when you will change position of one item, another positions must be also changed.
After that we re-position all other items to match our new order.

Add in the end of class:

This function is really simple. It just returns the highest position (if found), or -1 (if not found).

Add in the end of class:

Above function is override for or parent add() function. It adds default position before new object ID is created in database.

The last function is responsible for positions reset.
It retrieves all table rows. Then, it updates the positions of each one – making a fresh, clean position order.

Now you must replace in this file:
**table_name** with your actual database tablename (without PrestaShop prefix)
**primary_key** with column name from this table, containing primary key

And save the file!

That’s all.
If you’ve faced any issue while implementing this solution, or if you’ve found it useful – please let me know in the comments. Thank you.

Leave a comment

Your email address will not be published.


Leave a comment

Your email address will not be published.