Redeem Now
Read More
Read now
Menu Close
    Start a Project Request quote Reading list Switch to dark mode
    hyperlocal-marketplace-guide

    Add Position column in the render list in Prestashop/QloApps

    In this blog, We will learn the process to implement position in the render list column.

    Prestashop has predefined steps to implement the position column and you can manage positions from the render list with the drag and drop feature.

    First of all, see in the below image what we are going to implement.

    So we will implement the position column in the above image and learn the process to manage it.

    Keywords used in blog :

    We will use some keywords in the pieces of code used for the explanations in this blog. I am describing them below –

    1. your_table_name: This the name of the database table in which the position column is created for the learning of the positioning process.
    2. id_primary_key: This the primary key of table your_table_name.
    3. YourClassName: This the name of the class which is created for the table for which we are implementing the render list.

    Lets start the process step by step.

    Steps 1 :

    You have to create a column with name “position” in the table for which render list is created.

    Steps 2 :

    Specify the Identifier in admin controller to use for changing positions in list.

    In this step, you have to specify the identifier variable ($position_identifier) according to which positions in the render list will be changed with the drag and drop feature. So we have used the primary key of the table as the position identifier.

    You can skip this step if you don’t wat to change the positions with drag and drop feature in render list.

    Steps 3 :

    In your admin controller, In the __construct() method write the position column information array in the $this->fields_list property

    You have to declare special key position for the position type column. see the position array carefully.

    So, After these above steps, Your position column will be visible in your render list with the drag and drop feature.

    Now, we have to manage two test cases for the proper management of the position column working.

    • When we change position from the render list then update the position in the database.
    • When we delete a row from render list (or through any way) then reset the positions in the database

    So lets move further for implementing above points.

    Steps 4 :

    Update the position in the database when we change position from render list

    Whenever you change the position of a row in the render list. Prestashop internally fires an ajax for updating the position of the row. This sends you

    • The value of id (table’s id which you are dragging)
    • The way (up/down) of positioning
    • The array of positions in the render list

    For this, you have to implement the ajaxProcessUpdatePositions() function in the admin controller in which you are implementing the position column in the render list.

    In the above code, we are getting positions in the variable $positions.

    Here primary_key is used only for an example. In your controller will get it from different key.

    For example : If your $this->identifier in the admin controller is id_primary_key then this key will be primary_key (after removing _id from the identifier).

    In the above code at line-15, we have called function updatePosition() which you have to implement in your class which is created for the table we implementing render list for.

    So in the class, YourClassName write below function –

    Steps 5 :

    Reset the positions in the database When we delete a row from render list (or through any way)

    For this, you have to override the delete function in the class created for the table in which the position the field is implemented.

    So in the delete() function call the function which you have created for Reinitializing positions in the table. In our case, we have created function cleanPositions(). So we will call this function created in the same class.

    Now lets reinitialize the positions in the function cleanPositions().

    So these are few simple steps through which you can implement and manage the position field/column working in your render list.

    So I hope it will help you somewhere while coding in Prestashop.

    Happy coding. 🙂

    . . .

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    Be the first to comment.

    Back to Top
    css.php
    Hire Us!
    Brief us about your requirements and we'll get back to you.
    Woo! Hooy!
    We have just recieved your project brief and our expert will contact you shortly.
    Send Again
    Close