Jump to content

AdminControllers: Display additional columns with a details row action button


Recommended Posts

Hi,

 

I am unable to find any good example in order to achieve the following behavior rendering a list from a custom module in the backoffice. The Prestashop version I'm working on is 1.6.1.6

 

I don't want to bloat the post with my code, so I'll try to explain in detail what I've tried, and what I expect to happen.

 

The module's front office consist of a dynamic form, which switches fields depending of what the user chooses. For instance, if the user is a reseller, a field for an address will appear, but if the user is a instagram user, a field for its instagram account will appear.

 

All the data of this form gets stored in the same table. Not an issue, as I have control of all the "optional" fields.

 

The problem is on the backoffice. I am able to show a list with all the registers, but I have to choose whether displaying ALL the fields of my table, or just display a part of it (setting the $this->fields_list value accordingly in the constructor). 

 

What I want, is to just display a part of that list (just the common fields), and then expand further fields information when clicking on a "Details" button.

 

I've managed to add the row action, but I'm struggling with the ajaxProcess() method. I've tried all the possible examples I'm finding on the official documentation, StackOverflow and this forums, but the only thing I get is just a page reload. No error messages, no hints, no debugable XHR responses on the debugging console, absolutely nada.

 

This is how my Controller looks so far:

<?php

class JoinUsMultiformController extends ModuleAdminController
{
    public function __construct()
    {
        $this->table = 'join_us_subscriptors';
        $this->className = 'JoinUsSubscriptor';
        $this->context = Context::getContext();
        $this->orderBy = 'date_add';
        $this->_orderWay = 'DESC';

        $this->fields_list = array(
            'id_join_us_subscriptors' => array(
                'title' => $this->l('ID'),
                'align' => 'center',
                'width' => 25,
            ),
            'user_type' => array(
                'title' => $this->l('Tipo'),
                'align' => 'center',
                'width' => 25,
            ),
            'name' => array(
                'title' => $this->l('Nombre'),
                'align' => 'center',
                'width' => 25,
            ),
            'email' => array(
                'title' => $this->l('E-mail'),
                'align' => 'center',
                'width' => 25,
            ),
            'date_add' => array(
                'title' => $this->l('Fecha'),
                'align' => 'center',
                'width' => 25,
            )
        );

        $this->bootstrap = true;

        parent::__construct();
    }

    public function renderList()
    {
        $this->addRowAction('details');

        return parent::renderList();
    }

    public function ajaxProcess()
    {
        $query = 'SELECT * FROM ' . _DB_NAME_ . 'join_us_subscriptors';

        echo Tools::jsonEncode(array(
            'data'=> Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($query),
            'fields_display' => array (
                'city' => array(
                    'title' => $this->l('Ciudad'),
                    'align' => 'center',
                    'width' => 25,
                ),
            )
        ));

        die();
    }
}

I know 'city' field is not initialized on the constructor. Should I rewrite $this->fields_list in the ajaxProcess() method?

Why the page is just reloading when hitting the "Details" button?


Can anyone put me a bit on context and let me know what I'm missing? 

 

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

Are you calling the front controller via an ajax request from javascript?  If you want inline page updates without posting the form that's how you should do it.

 

Create the link when you initialise the form

$controller_url = $this->context->link->getModuleLink(
    'MODULE_NAME',
    'CONTROLLER_NAME',
    array(),
    true
);

Ajax request in js file



$.ajax({
    url: CONTROLLER_URL + '?submitAjaxRequest=1&ajax=1',
    type: 'post',
    dataType: 'json',
    data: $('#FORM').serialize(),
    success: function (json) {
    },
    error: function (data) {
    }
});

Method to handle the request in your front controller, add ajaxProcess to the front of whatever you call from the js request and it should find it automatically.

public function ajaxProcessSubmitAjaxRequest()
{
    $json = Tools::jsonEncode(array(
        'result' => 'success',
    ));
    die($json);
}
Edited by roja45 (see edit history)
Link to comment
Share on other sites

Many thanks for your time and answer Roja, but the problem is on the backOffice, not in the frontOffice. The front office story is just to put in context about how the module is working.

 

What I'm trying to do is to display in the backOffice a list of all the relevant data from users that submitted the form. I've already made a tab for it, and I'm able to show that list on that dedicated tab. BUT the point is that the're too many columns on that table, and thus should be somehow filtered. As a table must be regular, I cannot do many tricks showing/hiding columns. So, the possible options I've thought are:

 

Option 1 : Show a different table for each group (putting a button or something before the list, then via ajax refresh the table).

Option 2: Show a simplified table with only the common fields, then using an action button (View) then open additional info in another window.

 

I can have some directions to achieve option 1, but have no idea where to start with option 2.

 

Documentation is so sparse and confusing I'm cherrypicking from everywhere but I can't grab how displayList works at all into an adminController.

 

I'm asking more for references or any parts of the documentation I'm missing... 

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

sorry, should have read your question better, it makes no real difference though, you can still use the same method.  Once you've loaded your JS you can manipulate your screen to your hearts content.  I use this method to make quite complex configuration forms.

 

Not sure any documentation exists for this, best way is to attach a debugger and follow the code to see what it expects, and what it does.

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...